diff options
| author | Fuwn <[email protected]> | 2026-02-07 23:54:57 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-07 23:54:57 -0800 |
| commit | 737063805cc521d32e2e3d3321b90d8b14d5e72e (patch) | |
| tree | da2e59af2d6f73872234ed404b441cd35b5dc6fb /apps/web/app/sw.ts | |
| parent | fix: use dynamic icon as favicon, center text with lineHeight (diff) | |
| download | asa.news-737063805cc521d32e2e3d3321b90d8b14d5e72e.tar.xz asa.news-737063805cc521d32e2e3d3321b90d8b14d5e72e.zip | |
fix: enforce same-origin on all service worker cache routes
Wraps every defaultCache matcher to check sameOrigin first, preventing
the SW from intercepting cross-origin requests (Google favicons,
external images, fonts) which caused no-response errors on redirects.
Diffstat (limited to 'apps/web/app/sw.ts')
| -rw-r--r-- | apps/web/app/sw.ts | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/apps/web/app/sw.ts b/apps/web/app/sw.ts index fa2a8b7..0469377 100644 --- a/apps/web/app/sw.ts +++ b/apps/web/app/sw.ts @@ -1,7 +1,7 @@ /// <reference lib="webworker" /> import { defaultCache } from "@serwist/next/worker" import type { PrecacheEntry, SerwistGlobalConfig } from "serwist" -import { Serwist, NetworkOnly } from "serwist" +import { Serwist } from "serwist" declare global { interface WorkerGlobalScope extends SerwistGlobalConfig { @@ -11,18 +11,23 @@ declare global { declare const self: ServiceWorkerGlobalScope +const sameOriginCache = defaultCache.map((entry) => ({ + ...entry, + matcher: (parameters: Parameters<Exclude<(typeof defaultCache)[number]["matcher"], RegExp | string | undefined>>[0]) => { + if (!parameters.sameOrigin) return false + const original = entry.matcher + if (typeof original === "function") return original(parameters) + if (original instanceof RegExp) return original.test(parameters.url.href) + return false + }, +})) + const serwist = new Serwist({ precacheEntries: self.__SW_MANIFEST, skipWaiting: true, clientsClaim: true, navigationPreload: true, - runtimeCaching: [ - { - matcher: ({ url }) => url.origin !== self.location.origin, - handler: new NetworkOnly(), - }, - ...defaultCache, - ], + runtimeCaching: sameOriginCache, }) serwist.addEventListeners() |