-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
51 lines (48 loc) · 1.18 KB
/
sw.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
const STATIC_VERSION = 1;
const DYNAMIC_VERSION = 2;
const STATIC_CACHE = `static-${STATIC_VERSION}://covid19cue.netlify.app`;
const DYNAMIC_CACHE = `dynamic-${DYNAMIC_VERSION}://covid19cue.netlify.app`;
const APP_SHELL = [
`.`,
`index.html`,
`countries.json`,
`css/base.css`,
`js/base.js`,
`js/AsyncRequest.js`,
`img/icons/ic_192.png`,
`img/icons/ic_512.png`
];
// install event
self.oninstall = e => {
e.waitUntil(
caches.open(STATIC_CACHE).then(cache => {
cache.addAll(APP_SHELL);
})
);
};
// activate event
self.onactivate = e => {
e.waitUntil(
caches.keys().then(keys => {
return Promise.all(keys
.filter(key => key !== STATIC_CACHE & key !== DYNAMIC_CACHE)
.map(key => caches.delete(key))
);
})
);
};
// fetch event
self.onfetch = e => {
e.respondWith(
caches.match(e.request).then(cachedResponse => {
return cachedResponse || fetch(e.request).then(fetchResponse => {
return caches.open(`${DYNAMIC_CACHE}`).then(cache => {
// if (e.request.url.lastIndexOf(`.php`)===-1) {
// cache.put(e.request.url, fetchResponse.clone());
// }
return fetchResponse;
});
});
})
);
};