-
Notifications
You must be signed in to change notification settings - Fork 4
/
sw.js
40 lines (34 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
const cacheName='v2';
self.addEventListener('install',function(event){
console.log('{service worker} installing it...',event);
});
self.addEventListener('activate',function(event){
console.log('{service worker} activating it...',event);
event.waitUntil(
caches.keys().then(cacheNames =>{
return Promise.all(
cacheNames.map(cache=>{
if(cache!==cacheName){
console.log("deleting old one");
return caches.delete(cache);
}
})
);
})
);
return self.clients.claim();
});
self.addEventListener('fetch',function(event) {
console.log('{sw} fetching',event);
event.respondWith(fetch(event.request)
.then(res=>{
const resClone= res.clone();
caches
.open(cacheName)
.then(cache =>{
cache.put(event.request,resClone);
});
return res;
}).catch(err=>caches.match(event.request).then(res=>res))
);
});