-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
44 lines (39 loc) · 1.01 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
const CACHE_NAME = 'webplatformapis';
const FILES_TO_CACHE = [
'/offline.html',
];
self.addEventListener('install', event => {
console.log("sw.js installed");
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
console.log('[ServiceWorker] Pre-caching offline page');
return cache.addAll(FILES_TO_CACHE);
})
);
});
self.addEventListener('activate', event => {
console.log("sw.js activated");
event.waitUntil(
caches.keys().then(keyList => {
return Promise.all(keyList.map(key => {
if (key != CACHE_NAME) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
});
self.addEventListener('fetch', event => {
if (event.request.mode != 'navigate')
return;
event.respondWith(
fetch(event.request)
.catch(async () => {
return caches.open(CACHE_NAME)
.then(cache => {
return cache.match('offline.html');
});
})
);
});