forked from hngi/WikiPoli-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw_pages.js
49 lines (44 loc) · 1.03 KB
/
sw_pages.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
const cacheName = 'version1';
const cacheAssets = [
'/',
'index.html',
'contact.html',
'./styles/contact.css',
'./styles/index.css',
'./js/sw.js'
]
self.addEventListener('install', (e) => {
console.log('service worker installed');
e.waitUntil(
caches
.open(cacheName)
.then(cache => {
console.log('service worker: caching files');
cache.addAll(cacheAssets);
})
.then(() => self.skipWaiting())
)
})
self.addEventListener('activate', (e) => {
console.log('service worker activated');
//remove unwanted caches
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== cacheName) {
console.log('service worker: clearing old cache');
return caches.delete(cache);
}
})
)
})
)
})
//call fetch
self.addEventListener('fetch', (e => {
console.log('Service worker: fetching');
e.respondWith(
fetch(e.request).catch(() => caches.match(e.request))
)
}))