-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
50 lines (44 loc) · 1.3 KB
/
service-worker.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 CACHE_NAME = 'static-cache-v2';
const FILES_TO_CACHE = [
'./index.html',
'./0.index.js',
'./1.index.js',
'./ba809633fed94322bc52.module.wasm',
'./index.js',
'./styles.css',
'./icon-152.png'
];
self.addEventListener('install', (evt) => {
console.log('[Service Worker] install');
evt.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[Service Worker] Pre-cache');
return cache.addAll(FILES_TO_CACHE);
})
);
self.skipWaiting();
});
self.addEventListener('activate', (evt) => {
console.log('[Service Worker] Activate');
evt.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== CACHE_NAME) {
console.log('[Service Worker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
self.clients.claim();
});
self.addEventListener('fetch', (evt) => {
console.log('[Service Worker] Fetch', evt.request.url);
evt.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(evt.request).then((response) => {
return response || fetch(evt.request);
});
})
);
});