forked from publiclab/community-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
51 lines (44 loc) · 1.25 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
51
var STATIC_CACHE = "static-v1";
var DYNAMIC_CACHE = "dynamic-v1";
self.addEventListener('install', (e) => {
console.log("[Service Worker] installing...");
});
self.addEventListener('activate', (e) => {
console.log("[Service Worker] activating...",e);
e.waitUntil(
caches.keys()
.then((keyList) => {
return Promise.all(keyList.map((key) => {
if(key !== STATIC_CACHE && key !== DYNAMIC_CACHE) {
return caches.delete(key);
}
}))
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function (e) {
let urlReq = e.request;
e.respondWith(
fetch(urlReq)
.then((res) => {
var resClone = res.clone();
return caches.open(DYNAMIC_CACHE)
.then((cache) => {
cache.delete(urlReq.url)
.then((bool) => {
cache.add(urlReq.url, resClone);
})
return res;
})
})
.catch((err) => {
return caches.match(urlReq.url)
.then((data) => {
if(data!=undefined) {
return data;
}
})
})
);
});