-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsw.js
More file actions
71 lines (68 loc) · 1.74 KB
/
Copy pathsw.js
File metadata and controls
71 lines (68 loc) · 1.74 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
70
71
const CACHE = "mandala-maker-v7";
const ASSETS = [
"./",
"index.html",
"about.html",
"css/about.css",
"css/common.css",
"css/core.css",
"css/densities.css",
"css/mandala.css",
"css/overrides.css",
"css/themes/dark/bg0.png",
"css/themes/dark/dark.css",
"css/themes/evol-blue/evol-blue.css",
"css/themes/evol-blue/spirals.png",
"css/themes/light/bg0.png",
"css/themes/light/light.css",
"favicon.png",
"icon-192.png",
"icon-512.png",
"js/mandala.js",
"js/omg.js",
"manifest.json",
];
self.addEventListener("install", (e) => {
// "reload" bypasses the HTTP cache, so an update never caches stale files
e.waitUntil(
caches
.open(CACHE)
.then((c) => c.addAll(ASSETS.map((u) => new Request(u, { cache: "reload" })))),
);
self.skipWaiting();
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))),
),
);
self.clients.claim();
});
self.addEventListener("fetch", (e) => {
const url = new URL(e.request.url);
const cacheable =
url.origin === location.origin ||
url.hostname === "fonts.googleapis.com" ||
url.hostname === "fonts.gstatic.com";
if (e.request.method !== "GET" || !cacheable) return;
e.respondWith(
caches.match(e.request).then(
(cached) =>
cached ||
fetch(e.request)
.then((res) => {
if (res.ok) {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(e.request, copy));
}
return res;
})
.catch(() =>
e.request.mode === "navigate" ? caches.match("index.html") : undefined,
),
),
);
});