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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
import { build, files, version } from "$service-worker";
import { setupServiceWorker } from "@arkade-os/sdk";
setupServiceWorker(false).catch(console.error);
const CACHE_BUILD = `coinos-build-${version}`;
const CACHE_PAGES = `coinos-pages-${version}`;
const CACHE_PUBLIC = `coinos-public-${version}`;
const ESSENTIAL_STATIC = files.filter(
(f) =>
f.endsWith(".woff2") ||
f.endsWith(".woff") ||
f.endsWith(".png") ||
f.endsWith(".webp") ||
f.endsWith(".webmanifest") ||
f === "/favicon.ico",
);
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE_BUILD)
.then((cache) =>
Promise.allSettled(
[...build, ...ESSENTIAL_STATIC].map((url) =>
cache.add(url).catch(() => {}),
),
),
)
.then(() => self.skipWaiting()),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((k) => k.startsWith("coinos-") && k !== CACHE_BUILD && k !== CACHE_PAGES && k !== CACHE_PUBLIC)
.map((k) => caches.delete(k)),
),
)
.then(() => self.clients.claim()),
);
});
self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);
if (request.method !== "GET") return;
if (url.origin !== self.location.origin) return;
// Build assets — cache-first (fingerprinted/immutable)
if (build.includes(url.pathname)) {
event.respondWith(caches.match(request).then((cached) => cached || fetch(request)));
return;
}
// Navigation HTML — network-first
if (request.mode === "navigate") {
event.respondWith(
fetch(request)
.then((response) => {
const clone = response.clone();
caches.open(CACHE_PAGES).then((cache) => cache.put(request, clone));
return response;
})
.catch(() => caches.match(request).then((cached) => cached || caches.match("/"))),
);
return;
}
// __data.json (SvelteKit client-side navigation) — network-first
if (url.pathname.endsWith("__data.json")) {
event.respondWith(
fetch(request)
.then((response) => {
const clone = response.clone();
caches.open(CACHE_PAGES).then((cache) => cache.put(request, clone));
return response;
})
.catch(() => caches.match(request)),
);
return;
}
// Public API (avatars etc) — cache-first
if (url.pathname.startsWith("/api/public/")) {
event.respondWith(
caches.match(request).then(
(cached) =>
cached ||
fetch(request).then((response) => {
const clone = response.clone();
caches.open(CACHE_PUBLIC).then((cache) => cache.put(request, clone));
return response;
}),
),
);
return;
}
// Static files from ESSENTIAL_STATIC — cache-first
if (ESSENTIAL_STATIC.includes(url.pathname)) {
event.respondWith(caches.match(request).then((cached) => cached || fetch(request)));
return;
}
});
self.addEventListener("push", (event) => {
const data = event.data.json();
const { title, body, url } = data;
event.waitUntil(
clients.matchAll({ type: "window" }).then((clientList) => {
// Forward raw event to all open clients for instant processing
if (data.event) {
for (const client of clientList) {
client.postMessage({ type: "mls-event", event: data.event });
}
}
const onPage = clientList.some(
(c) => c.visibilityState === "visible" && new URL(c.url).pathname.startsWith(url),
);
if (onPage) return;
return self.registration.showNotification(title, {
body,
icon: "/images/icon.png",
badge: "/images/badge.png",
data: { url },
});
}),
);
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const path = event.notification.data.url || "/";
const target = new URL(path, self.location.origin).href;
event.waitUntil(
clients.matchAll({ type: "window" }).then((clientList) => {
for (const client of clientList) {
return client.navigate(target).then(() => client.focus());
}
return clients.openWindow(target);
}),
);
});
|