-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
41 lines (36 loc) · 1.03 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
const version = '1.0.1';
const cacheName = `v${version}::static`;
const fileList = ['/'];
self.addEventListener('install', e => {
// once the SW is installed, go ahead and fetch the resources
// to make this work offline
e.waitUntil(
caches
.open(cacheName)
.then(cache => {
return cache.addAll(fileList).then(() => {
self.skipWaiting();
});
})
.then(() => {
console.log(`offline ${version} ready`);
})
);
});
// when the browser fetches a url, either response with
// the cached object or go ahead and fetch the actual url
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(res => res || fetch(event.request))
);
});
const clearOldCaches = () => {
return caches.keys().then(keys => {
return Promise.all(
keys.filter(key => key !== cacheName).map(key => caches.delete(key))
);
});
};
self.addEventListener('activate', event => {
event.waitUntil(clearOldCaches().then(() => self.clients.claim()));
});