-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
61 lines (57 loc) · 1.73 KB
/
Copy pathsw.js
File metadata and controls
61 lines (57 loc) · 1.73 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
// Cache-first service worker. Bump CACHE on every release.
const CACHE = "workoutnotes-v1";
const SHELL = [
".",
"index.html",
"manifest.json",
"styles.css",
"js/db.js",
"js/bridge.js",
"js/bootstrap.js",
"py/app.py",
"py/models.py",
"py/parser.py",
"py/storage.py",
"py/templates.py",
"py/views/__init__.py",
"py/views/common.py",
"py/views/home.py",
"py/views/new_workout.py",
"py/views/exercise.py",
"py/views/picker.py",
"py/views/workout.py",
"py/views/history.py",
"py/views/paste.py",
"py/views/settings.py",
"icons/icon.svg",
];
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).then(() => self.skipWaiting()));
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
).then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
// Cache-first for everything we can serve offline.
// Pyodide CDN files are cached opportunistically once they succeed.
event.respondWith(
caches.match(event.request).then((hit) => {
if (hit) return hit;
return fetch(event.request).then((resp) => {
if (!resp || resp.status !== 200) return resp;
const isSameOrigin = url.origin === self.location.origin;
const isPyodide = url.hostname === "cdn.jsdelivr.net";
if (isSameOrigin || isPyodide) {
const copy = resp.clone();
caches.open(CACHE).then((c) => c.put(event.request, copy)).catch(() => {});
}
return resp;
}).catch(() => caches.match("index.html"));
})
);
});