-
Notifications
You must be signed in to change notification settings - Fork 25
/
sw.js
43 lines (36 loc) · 1.02 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
const CACHE_NAME = 'static-v1';
console.log('im the service worker ok!');
self.addEventListener('activate', (event) => {
console.log('activate event', event);
});
self.addEventListener('install', (event) => {
console.log('install', event);
});
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') {
return;
}
const cacheRequest = request.clone();
const inspectRequest = request.clone();
if (!request.url.startsWith(request.referrer)) {
return;
}
console.log('inspectRequest', inspectRequest);
event.respondWith(
fetch(event.request)
.then((response) => {
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then((cache) => {
console.log('caching', event.request);
cache.put(event.request, responseToCache);
});
return response;
})
.catch((err) => {
console.log('couldnt fetch', err);
return caches.match(cacheRequest);
}),
);
});