-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
53 lines (44 loc) · 1.32 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
45
46
47
48
49
50
// Service worker SCCSCSCS
const cacheName = 'v1';
// Installing Service Worker
self.addEventListener('install', (e) => {
console.log('Service Worker: Installed');
})
// Activating Service Worker
self.addEventListener('activate', (e) => {
console.log('Service Worker: Activated');
// Remove unwanted caches
e.waitUntil(
caches.keys().then(cacheNames =>{
return Promise.all(
cacheNames.map(cache=>{
if(cache!= cacheName){
console.log('Service Worker: Clearing old cache');
return caches.delete(cache);
}
})
)
})
)
});
// Fetching resources from cache
self.addEventListener('fetch',e=>{
console.log('Service Worker: Fetching');
e.respondWith(
fetch(e.request)
.then(res => {
// Clone response from Server
const resClone = res.clone();
// Open cache
caches
.open(cacheName)
.then(cache=>{
// Add response to cache
cache
.put(e.request, resClone)
.catch((e)=>{/* Do nothing */})
});
return res;
}). catch(err => caches.match(e.request).then(res => res))
)
})