Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
75 lines (69 loc) · 2.6 KB
/
Copy pathsw.js
File metadata and controls
75 lines (69 loc) · 2.6 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
72
73
74
75
const CACHE_NAME = "kk-cache-v4";
const ASSETS = [
"./",
"./index.html",
"./styles.css",
"./app.js",
"./manifest.json",
"./icons/icon-180.png",
"./icons/icon-192.png",
"./icons/icon-512.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)));
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) => Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))))
);
self.clients.claim();
});
// Network-first: this app gets iterated on often, so a deploy should show up
// the moment the phone is next online, not on some later reload. The cache
// only kicks in as a fallback when there's no connection at all.
self.addEventListener("fetch", (event) => {
event.respondWith(
// A plain fetch() still honors the browser's ordinary HTTP cache, so a
// deploy landing within an asset's Cache-Control max-age window could
// silently keep serving the previous version despite this being
// "network-first" — no-store forces an actual round-trip every time.
fetch(event.request, { cache: "no-store" })
.then((response) => {
if (response.ok) {
// Keep the worker alive until the cache write lands — respondWith's
// promise already resolved with `response` by the time this runs,
// so without waitUntil the browser is free to kill the worker
// mid-write, silently leaving the offline fallback on a stale asset.
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.put(event.request, response.clone())));
}
return response;
})
.catch(() => caches.match(event.request))
);
});
self.addEventListener("push", (event) => {
const data = event.data ? event.data.json() : {};
event.waitUntil(
Promise.all([
self.registration.showNotification(data.title || "Kinder Kalender", { body: data.body || "" }),
updateBadge(data.badge),
])
);
});
function updateBadge(badge) {
if (badge === undefined || !("setAppBadge" in navigator)) return Promise.resolve();
return badge > 0 ? navigator.setAppBadge(badge) : navigator.clearAppBadge();
}
self.addEventListener("notificationclick", (event) => {
event.notification.close();
event.waitUntil(
self.clients.matchAll({ type: "window" }).then((clients) => {
const existing = clients.find((client) => client.url.includes(self.registration.scope));
if (existing) return existing.focus();
return self.clients.openWindow("./");
})
);
});