-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.ts
More file actions
66 lines (55 loc) · 2.15 KB
/
sw.ts
File metadata and controls
66 lines (55 loc) · 2.15 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
/* Knotes Service Worker powered by Serwist (TypeScript) */
// We use the UMD build of Serwist via importScripts to avoid bundling.
// This file is compiled to public/sw.ts during build.
export {};
// Serwist type helpers (optional)
declare global {
interface WorkerGlobalScope {
__SW_MANIFEST: (any | string)[] | undefined;
serwist: any;
}
}
declare const self: WorkerGlobalScope & {
importScripts: (...args: string[]) => void;
serwist?: any;
__SW_MANIFEST?: (any | string)[] | undefined;
};
// Load Serwist UMD bundle at runtime (from CDN)
// You can self-host this later if preferred.
self.importScripts("https://unpkg.com/serwist@9.2.1/dist/serwist.umd.js");
(function () {
if (!self.serwist || !self.serwist.Serwist) {
// Fallback: no Serwist available; bail out gracefully.
// The app will continue to work online without SW features.
// eslint-disable-next-line no-console
console.warn("Serwist UMD failed to load in Service Worker.");
return;
}
const { Serwist } = self.serwist as { Serwist: new (opts: any) => any };
// Optional global manifest injected by build tools. Not used here, but
// we keep the declaration to be compatible with Serwist's precache.
// In our setup, this will be undefined and that's okay.
self.__SW_MANIFEST = self.__SW_MANIFEST || undefined;
// Initialize Serwist per docs with sensible defaults for a PWA.
const serwist = new Serwist({
precacheEntries: self.__SW_MANIFEST,
precacheOptions: {
cleanupOutdatedCaches: true,
concurrency: 10,
ignoreURLParametersMatching: [],
},
skipWaiting: true,
clientsClaim: true,
navigationPreload: false,
disableDevLogs: true,
// No explicit runtimeCaching here to keep things minimal.
// You can register custom routes at runtime if needed.
});
// Hook Serwist event listeners (install/activate/fetch/message)
serwist.addEventListeners();
// Interop with native SW API — minimal example
(self as any).addEventListener("message", (event: any) => {
// Allow pages to ask SW to skip waiting immediately
if (event && event.data === "SKIP_WAITING") (self as any).skipWaiting();
});
})();