-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
69 lines (65 loc) · 2.21 KB
/
Copy pathsw.js
File metadata and controls
69 lines (65 loc) · 2.21 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Service worker — cache shell + content for offline use
const CACHE = 'aplus-study-v126';
const ASSETS = [
'./',
'./index.html',
'./styles.css',
'./app.js',
'./core.mjs',
'./confetti.mjs',
'./focus-sound.mjs',
'./image-zoom.mjs',
'./wake-lock.mjs',
'./read-aloud.mjs',
'./shake.mjs',
'./storage.mjs',
'./scratchpad.mjs',
'./sync.mjs',
'./lib.mjs',
'./crypto.mjs',
'./manifest.json',
'./data/core2/questions.json',
'./data/core2/concept-fixes.json',
'./icons/icon-192.png',
'./icons/icon-512.png',
'./icons/share-qr.svg',
];
self.addEventListener('install', (event) => {
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS)));
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
// Only cache same-origin GETs. Cross-origin opaque responses (e.g. the
// pdf.js CDN) used to be persisted forever in the versioned cache —
// poisoning vector if a CDN response was ever tampered with, and
// unbounded cache growth. Pass them through to the network/browser
// cache without storing them ourselves.
const sameOrigin = new URL(event.request.url).origin === self.location.origin;
event.respondWith(
caches.match(event.request).then((cached) => {
if (cached) return cached;
return fetch(event.request).then((res) => {
// Skip caching for opaque, non-OK, and cross-origin responses.
if (sameOrigin && res && res.ok && res.type === 'basic') {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(event.request, copy).catch(() => {}));
}
return res;
}).catch(() => cached);
})
);
});
// Tell open clients when a new service worker has taken over so they can
// reload without waiting for the user to delete + reinstall the icon.
// Pairs with the registration code in app.js that listens for 'waiting'
// or 'controllerchange'.
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') self.skipWaiting();
});