-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathserviceWorker.js
46 lines (41 loc) · 1 KB
/
serviceWorker.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
const staticPromiViz = "promiviz"
const expectedCaches = [staticPromiViz];
const assets = [
"/",
"/index.html",
"/main.css",
"/app.js",
"/images/blog.png",
"/images/github.png",
"/images/twitter.png",
"/social/landing.png"
]
self.addEventListener("install", installEvent => {
installEvent.waitUntil(
caches.open(staticPromiViz).then(cache => {
cache.addAll(assets)
})
)
});
self.addEventListener('activate', event => {
// delete any caches that aren't in expectedCaches
// which will get rid of promiviz
event.waitUntil(
caches.keys().then(keys => Promise.all(
keys.map(key => {
if (!expectedCaches.includes(key)) {
return caches.delete(key);
}
})
)).then(() => {
console.log('promiviz now ready to handle fetches!');
})
);
});
self.addEventListener("fetch", fetchEvent => {
fetchEvent.respondWith(
caches.match(fetchEvent.request).then(res => {
return res || fetch(fetchEvent.request)
})
)
});