/** * Service-worker configuration for `@serwist/next`. Extracted from * `sw.ts` so unit tests can pin the choices without booting up a * service-worker scope. * * The choices we care about — and why they're the way they are: * * skipWaiting + clientsClaim * A new worker takes over open tabs on the next navigation * instead of waiting for every tab to close. Operators tend to * live in one tab; faster updates win. * * navigationPreload * Tells the browser it can race the network fetch for navigations * alongside the worker boot, cutting first-paint when the worker * is cold. * * runtimeCaching * Serwist's stock recipe — HTML network-first with offline * fallback, static assets cache-first, image / font caches with * sensible TTLs. Easy to swap if we want bespoke strategies later. * * precacheEntries * Whatever `__SW_MANIFEST` the build pipeline injected. We pass * it through unchanged. */ type PrecacheEntry = string | { url: string; revision: string | null }; /** * The runtime-caching shape is generic on purpose. Serwist's own * `RuntimeCaching` type pulls in WebWorker DOM lib refs that * vitest's plain Node env doesn't ship; using a generic preserves * the type at the call site (sw.ts passes Serwist's * `RuntimeCaching[]` and gets it back unchanged) without forcing * the lib import here. */ export interface ServiceWorkerConfig { precacheEntries: PrecacheEntry[]; skipWaiting: boolean; clientsClaim: boolean; navigationPreload: boolean; runtimeCaching: R; } export function serviceWorkerConfig( precacheEntries: PrecacheEntry[], runtimeCaching: R, ): ServiceWorkerConfig { return { precacheEntries, skipWaiting: true, clientsClaim: true, navigationPreload: true, runtimeCaching, }; }