-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
64 lines (59 loc) · 1.73 KB
/
serviceworker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const currentCache = "v1"
const offlineFallbackFiles = [
location.origin,
"/sound/countdown.mp3",
"/sound/gunfire.mp3",
"/PixelOperator-Bold.ttf",
"/img/sprites.webp",
"/img/CowBoyShoot.gif",
"/unavailable/"
]
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(currentCache)
.then((cache) => {
return cache.addAll(offlineFallbackFiles)
})
)
})
this.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keyList) =>
Promise.all(
keyList.map((key) => {
if (![currentCache].includes(key)) {
return caches.delete(key);
}
})
)
)
);
});
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
//Intercepts every fetch event
self.addEventListener('fetch', (event) => {
event.respondWith(
//Tries toget the resouce
fetch(event.request).then(
//If the resource is downloaded sucessfully put into cache and resolves the first fetch with it
(res) => {
if(event.request.method == "GET" && res.status == "200"){
caches.open(currentCache).then(function(cache) {
cache.put(event.request, res)
})
}
return res.clone()
},
//If its unable to get the resource show the unvailable page
() => {
return caches.match(event.request).then((res) =>{
return (res || caches.match('/unavailble'))
})
}
)
)
})