-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
62 lines (55 loc) · 1.67 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
52
53
54
55
56
57
58
59
60
61
62
"use strict";
const cacheNamespace = "planning-poker";
const version = "v1.0.0";
const cacheName = `${cacheNamespace}-${version}-static`;
function updateStaticCache() {
return caches.open(cacheName).then(cache => {
return cache.addAll([
"./style.css",
"./",
"./manifest.json",
"images/planning-poker.png",
"images/planning-poker.svg"
]);
});
}
function clearOldCaches() {
return caches.keys().then(keys => {
return Promise.all(
keys
.filter(key => key.indexOf(cacheNamespace) === 0)
.filter(key => key.indexOf(cacheName) !== 0)
.map(key => caches.delete(key))
);
});
}
self.addEventListener("install", event => {
event.waitUntil(updateStaticCache().then(() => self.skipWaiting()));
});
self.addEventListener("activate", event => {
event.waitUntil(clearOldCaches().then(() => self.clients.claim()));
});
self.addEventListener("fetch", event => {
const request = event.request;
const url = new URL(request.url);
if (url.origin !== location.origin) {
return;
}
// try cache firts, fall back to network
event.respondWith(
caches.match(request).then(response => {
return (
response ||
fetch(request)
.then(response => {
const copy = response.clone();
stashInCache(cacheName, request, copy);
return response;
})
.catch(() => {
return;
})
);
})
);
});