-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
29 lines (25 loc) · 942 Bytes
/
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
const CACHE = 'kmap-solver-cache';
const ASSETS = ['.','index.html','styles.css','kmap-interface.js','kmap-solver.js','info.md'];
self.addEventListener('install', e => {
self.skipWaiting();
e.waitUntil(caches.open(CACHE).then(c => Promise.all(ASSETS.map(a => c.add(new Request(a))))));
});
self.addEventListener('activate', e => e.waitUntil(clients.claim()));
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET' || !['http:', 'https:'].includes(new URL(e.request.url).protocol)) return;
e.respondWith(
caches.match(e.request).then(cached => {
const networked = fetch(e.request, {cache: 'no-store'})
.then(r => {
if (r && r.status === 200) {
const clone = r.clone();
caches.open(CACHE)
.then(c => c.put(e.request, clone));
}
return r;
})
.catch(() => cached);
return cached || networked;
})
);
});