-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_service_worker.js
42 lines (36 loc) · 1.14 KB
/
web_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
const CACHE_NAME = 'offline';
const OFFLINE_URL = '/index.html';
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('v1').then((cache) => {
return cache.addAll([
]);
})
);
});
self.addEventListener('activate', (event) => {
console.log('[ServiceWorker] Activate');
event.waitUntil((async () => {
// Enable navigation preload if it's supported.
// See https://developers.google.com/web/updates/2017/02/navigation-preload
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable();
}
})());
// Tell the active service worker to take control of the page immediately.
self.clients.claim();
});
self.addEventListener('fetch', event => {
event.respondWith(
fetch(event.request).then(res => {
const resClone = res.clone();
caches
.open("v1")
.then(cache => {
console.log(cache)
cache.put(event.request, resClone);
});
return res;
}).catch(err => caches.match(event.request).then(res => res))
);
});