diff --git a/config/hosting/cache-policy.json b/config/hosting/cache-policy.json index 45b79bff..1bf46db8 100644 --- a/config/hosting/cache-policy.json +++ b/config/hosting/cache-policy.json @@ -27,9 +27,6 @@ }, "sourceMap": { "public": false - }, - "serviceWorker": { - "enabled": false } } } diff --git a/docs/operations/release-cache-rollback.md b/docs/operations/release-cache-rollback.md index 6dde83c7..8237b3d2 100644 --- a/docs/operations/release-cache-rollback.md +++ b/docs/operations/release-cache-rollback.md @@ -18,7 +18,9 @@ The provider-independent cache defaults are: - HTML: `no-cache` - runtime config and release manifest: `no-store` - public source maps: disabled -- service worker/offline cache: disabled +- service worker/offline cache: disabled, unless the static selection in + `src/features/installed-runtime-capabilities.ts` is `ACTIVE`; then `dist` + holds exactly the selected `service-worker.js` HTML, JSON config/manifest, and hashed JavaScript MIME types are also compared to the declared allowlist; a cache-correct response with a mismatched diff --git a/scripts/lib/hosting-probe.ts b/scripts/lib/hosting-probe.ts index ab934be2..5f6ec9c2 100644 --- a/scripts/lib/hosting-probe.ts +++ b/scripts/lib/hosting-probe.ts @@ -1,4 +1,31 @@ +import type { InstalledServiceWorkerSelection } from "../../src/contracts/service-worker.ts"; + const LOOPBACK_IPV4 = /^127(?:\.\d{1,3}){3}$/; +const PUBLIC_SERVICE_WORKER = /(?:^|\/)(?:service-worker|sw)(?:[.-][^/]*)?\.js$/i; + +/** + * A built `dist` may ship a worker script only when the static selection is + * `ACTIVE`, and then only the selected script at the app root. + */ +export function checkPublicServiceWorkers( + distFiles: readonly string[], + selection: InstalledServiceWorkerSelection | null, +) { + const found = distFiles.filter((file) => PUBLIC_SERVICE_WORKER.test(file)); + const allowed: string[] = + selection?.mode === "ACTIVE" ? [selection.scriptPath] : []; + const passed = + found.length === allowed.length && + found.every((file) => allowed.includes(file)); + return { + surface: "serviceWorker", + header: "enabled", + expected: allowed.length > 0, + observed: found.length > 0, + passed, + ...(passed ? {} : { reason: `dist worker scripts: ${found.join(", ") || "none"}` }), + }; +} /** * A release gate must not promote a local preview server as live hosting diff --git a/scripts/verify-hosting-headers.ts b/scripts/verify-hosting-headers.ts index 26efc861..afcfdf53 100644 --- a/scripts/verify-hosting-headers.ts +++ b/scripts/verify-hosting-headers.ts @@ -1,7 +1,11 @@ import { mkdir, readFile, readdir } from "node:fs/promises"; +import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts"; import { hostingHeadersArtifactSchema } from "./contracts/release-artifacts.ts"; -import { classifyLiveHostingBaseUrl } from "./lib/hosting-probe.ts"; +import { + checkPublicServiceWorkers, + classifyLiveHostingBaseUrl, +} from "./lib/hosting-probe.ts"; import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts"; type Document = Record; @@ -63,9 +67,6 @@ const baseUrl = process.env.HOSTING_BASE_URL; const liveTarget = baseUrl ? classifyLiveHostingBaseUrl(baseUrl) : null; const distFiles = (await readdir("dist", { recursive: true })).map(String); const publicSourceMaps = distFiles.filter((file) => file.endsWith(".map")); -const publicServiceWorkers = distFiles.filter((file) => - /(?:^|\/)(?:service-worker|sw)(?:[.-][^/]*)?\.js$/i.test(file), -); let responses: ResponseHeaders = {}; let mode: HostingMode; @@ -184,15 +185,12 @@ results.push({ observed: publicSourceMaps.length > 0, passed: sourceMapPolicy.public === false && publicSourceMaps.length === 0, }); -const serviceWorkerPolicy = recordValue(cacheSurfaces.serviceWorker); -results.push({ - surface: "serviceWorker", - header: "enabled", - expected: false, - observed: publicServiceWorkers.length > 0, - passed: - serviceWorkerPolicy.enabled === false && publicServiceWorkers.length === 0, -}); +results.push( + checkPublicServiceWorkers( + distFiles, + INSTALLED_RUNTIME_CAPABILITIES.serviceWorker, + ), +); const passed = results.every((result) => result.passed); await mkdir("artifacts/release", { recursive: true }); diff --git a/tests/unit/hosting-probe.test.ts b/tests/unit/hosting-probe.test.ts index dc122b96..87c74296 100644 --- a/tests/unit/hosting-probe.test.ts +++ b/tests/unit/hosting-probe.test.ts @@ -1,6 +1,45 @@ import { describe, expect, it } from "vitest"; -import { classifyLiveHostingBaseUrl } from "../../scripts/lib/hosting-probe.ts"; +import { + checkPublicServiceWorkers, + classifyLiveHostingBaseUrl, +} from "../../scripts/lib/hosting-probe.ts"; + +describe("public service worker in dist", () => { + const app = ["index.html", "config.json", "assets/index-abc.js"]; + const active = { + mode: "ACTIVE", + scriptPath: "service-worker.js", + handlers: ["PWA_STATIC_ASSETS"], + } as const; + const removal = { mode: "REMOVE_REGISTRATION", scriptPath: "service-worker.js" } as const; + + it("accepts the selected worker when the selection is ACTIVE", () => { + expect( + checkPublicServiceWorkers([...app, "service-worker.js"], active), + ).toMatchObject({ expected: true, observed: true, passed: true }); + }); + + it("accepts a worker-free dist when no worker is selected", () => { + expect(checkPublicServiceWorkers(app, null)).toMatchObject({ + expected: false, + passed: true, + }); + }); + + it.each([ + ["no selection", [...app, "service-worker.js"], null], + ["a non-ACTIVE selection", [...app, "service-worker.js"], removal], + ["ACTIVE without the worker", app, active], + ["ACTIVE with an extra worker", [...app, "service-worker.js", "assets/sw.js"], active], + ["ACTIVE with the worker off the root", [...app, "assets/service-worker.js"], active], + ] as const)("rejects %s", (_, files, selection) => { + expect(checkPublicServiceWorkers(files, selection)).toMatchObject({ + passed: false, + reason: expect.stringContaining("dist worker scripts"), + }); + }); +}); describe("live hosting evidence target", () => { it("accepts a canonical production HTTPS root", () => {