| Name | Message | Date |
|---|---|---|
| 📁 lib | 1 month ago | |
| 📁 routes | 1 month ago | |
| 📄 app.d.ts | 1 month ago | |
| 📄 app.html | 1 month ago | |
| 📄 service-worker.ts | 1 month ago |
📄
src/service-worker.ts
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
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />
/// <reference types="@sveltejs/kit" />
import { build, files, version } from "$service-worker";
const sw = self as unknown as ServiceWorkerGlobalScope;
const SHELL_CACHE = `shell-${version}`;
const MODEL_CACHE = "birdnet-models";
// Precache the app shell and lightweight static files.
// Model binaries and label JSON are excluded — they're too large to cache at
// install time and are instead cached on first use via the fetch handler.
const SHELL_ASSETS = [
...build,
...files.filter((f) => !f.startsWith("/models/") && f !== "/robots.txt"),
"/",
];
sw.addEventListener("install", (event) => {
event.waitUntil(
caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL_ASSETS)),
);
sw.skipWaiting();
});
sw.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((k) => k !== SHELL_CACHE && k !== MODEL_CACHE)
.map((k) => caches.delete(k)),
),
),
);
sw.clients.claim();
});
sw.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);
if (request.method !== "GET" || url.origin !== sw.location.origin) return;
// Model files: cache-first with a stable cache that outlives app updates.
if (url.pathname.startsWith("/models/")) {
event.respondWith(
caches.open(MODEL_CACHE).then(async (cache) => {
const cached = await cache.match(request);
if (cached) return cached;
const response = await fetch(request);
cache.put(request, response.clone());
return response;
}),
);
return;
}
// Everything else: serve from shell cache when available, fall back to
// network, and for navigation failures return the cached root as SPA shell.
event.respondWith(
caches.match(request).then(async (cached) => {
if (cached) return cached;
try {
return await fetch(request);
} catch {
if (request.mode === "navigate") {
return (
(await caches.match("/")) ??
new Response("Offline", { status: 503 })
);
}
return new Response("Offline", { status: 503 });
}
}),
);
});