-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
311 lines (286 loc) · 10.3 KB
/
Copy pathsw.js
File metadata and controls
311 lines (286 loc) · 10.3 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Copyright (c) 2026 HOOX · AXIS · jango_blockchained
* SPDX-License-Identifier: AGPL-3.0-only
*/
/* AXIS — service worker (shipping classic SW; Vite copies public/ → dist/).
*
* Pure helpers mirrored in `src/sw/strategy.ts` (keep in sync).
*
* Strategy:
* - Navigation (HTML) → network-first, shell cache fallback
* - Same-origin static → cache-first with fetch retry (shell for precache
* paths, else runtime); a transient reset must not fail an uncached asset
* includes /pyodide/* + /vendor/* for offline pyodide engine
* - CDN (esm.sh, jsdelivr, unpkg, cdnjs) → cache-first runtime
* - Same-origin /api/* → network-first; cache only HTTP 200 basic;
* offline miss → 503 JSON (never cache opaque/errors as success)
* - Same-origin /version.json → do not intercept (update poll must hit network)
* - Non-GET / other cross-origin → do not intercept
*
* Version bump (VERSION) when precache list or strategy semantics change.
* Activate deletes old `axis-*` caches only; current shell/runtime kept.
*/
const VERSION = 'v7';
const CACHE_PREFIX = 'axis-';
const SHELL_CACHE = `${CACHE_PREFIX}shell-${VERSION}`;
const RUNTIME_CACHE = `${CACHE_PREFIX}runtime-${VERSION}`;
/** Soft cap on runtime cache entries (hashed assets + pyodide + CDN). Keep in sync with src/sw/strategy.ts. */
const RUNTIME_CACHE_MAX_ENTRIES = 96;
/** Uncached static/CDN fetch retries. Keep in sync with src/sw/strategy.ts. */
const FETCH_RETRY_ATTEMPTS = 3;
const FETCH_RETRY_TIMEOUT_MS = 8000;
/** Stable shell assets present in Vite dist and legacy root trees. */
const SHELL_ASSETS = [
'./',
'./index.html',
'./manifest.webmanifest',
'./assets/icon-192.png',
'./assets/icon-512.png',
'./assets/icon-maskable-512.png',
];
const CDN_HOST_RE =
/(?:^|\.)(esm\.sh|jsdelivr\.net|unpkg\.com|cdnjs\.cloudflare\.com)$/i;
function isCdnHost(host) {
return CDN_HOST_RE.test(host);
}
function isApiPath(pathname) {
return pathname === '/api' || pathname.startsWith('/api/');
}
function isVersionProbe(pathname) {
return pathname === '/version.json' || pathname.endsWith('/version.json');
}
/** Opaque / error must never be stored as a successful cache entry. */
function shouldCacheStaticResponse(res) {
if (!res) return false;
if (res.type === 'opaque' || res.type === 'error' || res.type === 'opaqueredirect') {
return false;
}
if (res.type !== 'basic' && res.type !== 'cors' && res.type !== 'default') {
return false;
}
return res.ok === true && res.status >= 200 && res.status < 300;
}
function shouldCacheApiResponse(res) {
if (!res) return false;
if (res.type === 'opaque' || res.type === 'error' || res.type === 'opaqueredirect') {
return false;
}
if (res.type !== 'basic' && res.type !== 'default') return false;
return res.status === 200;
}
function offlineApiResponse() {
return new Response(
JSON.stringify({
status: 'error',
code: 'OFFLINE',
message: 'No network and no cached response.',
}),
{ status: 503, headers: { 'Content-Type': 'application/json' } },
);
}
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(SHELL_CACHE);
// Resilient precache: one missing asset must not fail the whole install.
await Promise.all(
SHELL_ASSETS.map(async (asset) => {
try {
const req = new Request(asset, { cache: 'reload' });
const res = await fetch(req);
if (shouldCacheStaticResponse(res)) {
await cache.put(req, res);
}
} catch {
/* ignore missing legacy/optional shell files */
}
}),
);
self.skipWaiting();
})());
});
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
const names = await caches.keys();
const keep = new Set([SHELL_CACHE, RUNTIME_CACHE]);
await Promise.all(
names
.filter((n) => n.startsWith(CACHE_PREFIX) && !keep.has(n))
.map((n) => caches.delete(n)),
);
await self.clients.claim();
})());
});
/** After put into runtime cache, drop oldest entries past the soft cap. */
async function trimRuntimeCache(cache) {
try {
const keys = await cache.keys();
const drop = keys.length - RUNTIME_CACHE_MAX_ENTRIES;
if (drop <= 0) return;
for (let i = 0; i < drop; i++) {
try {
await cache.delete(keys[i]);
} catch {
/* ignore */
}
}
} catch {
/* ignore */
}
}
async function putRuntime(cache, req, res) {
await cache.put(req, res);
await trimRuntimeCache(cache);
}
/**
* Retry thrown network errors (not HTTP 4xx). Clone each attempt so a
* consumed body cannot poison later tries; abort hung sockets.
*/
async function fetchWithRetry(req, attempts = FETCH_RETRY_ATTEMPTS) {
let lastErr;
for (let i = 0; i < attempts; i++) {
const ac = new AbortController();
const timer = setTimeout(() => ac.abort(), FETCH_RETRY_TIMEOUT_MS);
try {
return await fetch(req.clone(), { signal: ac.signal });
} catch (err) {
lastErr = err;
} finally {
clearTimeout(timer);
}
if (i < attempts - 1) {
await new Promise((r) => setTimeout(r, i === 0 ? 50 : 100));
}
}
throw lastErr;
}
async function cacheFirst(req, cacheName) {
const cache = await caches.open(cacheName);
const cached = await cache.match(req);
if (cached) return cached;
let res;
try {
res = await fetchWithRetry(req);
} catch (err) {
// Still offline/broken after retries — serve a raced cache hit if one
// landed meanwhile, else let the browser report the failure.
const raced = await cache.match(req);
if (raced) return raced;
throw err;
}
if (shouldCacheStaticResponse(res)) {
try {
if (cacheName === RUNTIME_CACHE) {
await putRuntime(cache, req, res.clone());
} else {
await cache.put(req, res.clone());
}
} catch {
/* quota / opaque clone edge */
}
}
return res;
}
/** Minimal offline shell when network + cache both miss (never reject respondWith). */
function offlineShellResponse() {
const html =
'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/>' +
'<meta name="viewport" content="width=device-width,initial-scale=1"/>' +
'<title>AXIS offline</title>' +
'<style>body{margin:0;font:15px/1.45 system-ui,sans-serif;background:#0a0b10;color:#e8eaed;' +
'display:grid;place-items:center;min-height:100vh;padding:1.5rem;box-sizing:border-box}' +
'main{max-width:28rem}a{color:#8ab4ff}</style></head><body><main>' +
'<h1>AXIS is offline</h1>' +
'<p>Network unavailable and no cached shell. Reconnect, then reload.</p>' +
'<p><a href="./">Retry</a></p></main></body></html>';
return new Response(html, {
status: 503,
headers: { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' },
});
}
async function networkFirstStatic(req, cacheName) {
const cache = await caches.open(cacheName);
try {
const res = await fetch(req);
if (shouldCacheStaticResponse(res)) {
try {
if (cacheName === RUNTIME_CACHE) {
await putRuntime(cache, req, res.clone());
} else {
await cache.put(req, res.clone());
}
} catch {
/* ignore */
}
}
return res;
} catch {
const cached = await cache.match(req);
if (cached) return cached;
// index.html fallback for navigations
const shell = await cache.match('./index.html') || await cache.match('./');
if (shell) return shell;
// Never reject respondWith — a thrown NetworkError surfaces as SW failure in the console
return offlineShellResponse();
}
}
async function networkFirstApi(req) {
const cache = await caches.open(RUNTIME_CACHE);
try {
const res = await fetch(req);
if (shouldCacheApiResponse(res)) {
try {
await putRuntime(cache, req, res.clone());
} catch {
/* ignore */
}
}
// Return network result even when non-200 (do not mask API errors with stale).
return res;
} catch {
const cached = await cache.match(req);
if (cached) return cached;
return offlineApiResponse();
}
}
/**
* @returns {'api'|'navigate'|'cdn'|'static'|null} null = do not intercept
*/
function classify(req, url) {
const method = (req.method || 'GET').toUpperCase();
if (method !== 'GET' && method !== 'HEAD') return null;
if (url.origin === self.location.origin && isApiPath(url.pathname)) return 'api';
if (req.mode === 'navigate' || req.destination === 'document') return 'navigate';
if (isCdnHost(url.host)) return 'cdn';
if (url.origin === self.location.origin && isVersionProbe(url.pathname)) return null;
if (url.origin === self.location.origin) return 'static';
return null;
}
self.addEventListener('fetch', (event) => {
const req = event.request;
let url;
try {
url = new URL(req.url);
} catch {
return;
}
const kind = classify(req, url);
if (!kind) return; // non-GET or unhandled cross-origin — browser default
if (kind === 'api') {
event.respondWith(networkFirstApi(req));
return;
}
if (kind === 'navigate') {
event.respondWith(networkFirstStatic(req, SHELL_CACHE));
return;
}
if (kind === 'cdn') {
event.respondWith(cacheFirst(req, RUNTIME_CACHE));
return;
}
// same-origin static (JS/CSS/wasm/whl/py/icons/plugins/…)
event.respondWith(cacheFirst(req, RUNTIME_CACHE));
});
// Allow the page to trigger an immediate skip-waiting via postMessage.
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') self.skipWaiting();
});