Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
44 lines (40 loc) · 1.46 KB
/
Copy pathsw.js
File metadata and controls
44 lines (40 loc) · 1.46 KB
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
// Offline support. Same-origin requests are served from cache while a
// fresh copy is fetched in the background, so the app opens instantly
// (and on a plane), and picks up a deploy one load later.
// Bump VERSION with any deploy so old caches get swept.
const VERSION = 'studyflex-v9';
const CORE = [
'.',
'index.html',
'css/style.css',
'vendor/katex.css',
'vendor/katex.min.js',
'js/app.js', 'js/fsrs.js', 'js/expr.js', 'js/rng.js', 'js/template.js',
'js/store.js', 'js/session.js', 'js/fx.js', 'js/ai.js', 'js/gamify.js', 'js/sync.js',
'js/decks/calc1.js', 'js/decks/techniques.js', 'js/decks/mech1.js',
'manifest.webmanifest',
];
self.addEventListener('install', ev => {
ev.waitUntil(caches.open(VERSION).then(c => c.addAll(CORE)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', ev => {
ev.waitUntil(
caches.keys()
.then(keys => Promise.all(keys.filter(k => k !== VERSION).map(k => caches.delete(k))))
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', ev => {
const url = new URL(ev.request.url);
if (ev.request.method !== 'GET' || url.origin !== location.origin) return;
ev.respondWith(
caches.open(VERSION).then(async cache => {
const cached = await cache.match(ev.request);
const fresh = fetch(ev.request).then(res => {
if (res.ok) cache.put(ev.request, res.clone());
return res;
}).catch(() => cached);
return cached || fresh;
})
);
});