diff --git a/scripts/build-frontend.ts b/scripts/build-frontend.ts index a110ffa0..d77ba74b 100644 --- a/scripts/build-frontend.ts +++ b/scripts/build-frontend.ts @@ -32,7 +32,9 @@ import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-serv * without it every build — including a production one — ships the local * runtime document. The Service Worker asset scan excludes `config.json` * (`scripts/generate-service-worker-assets.ts`), so the order against step 5 - * is not what keeps the profile out of the worker cache (13-15). + * is not what keeps the profile out of the worker cache (13-15). Step 6 reads + * the release manifest URL from the `dist/config.json` step 4 wrote, and fails + * if its `APP_ENV` is not `APP_PROFILE` (06-10). */ const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker; diff --git a/scripts/lib/service-worker-build-input.ts b/scripts/lib/service-worker-build-input.ts index 2315f317..65db5023 100644 --- a/scripts/lib/service-worker-build-input.ts +++ b/scripts/lib/service-worker-build-input.ts @@ -29,7 +29,10 @@ export function resolveServiceWorkerBuildInput(input: Readonly<{ selection: InstalledServiceWorkerSelection | null; assets: unknown; contractSet: unknown; + /** `dist/config.json`, materialized from the build's profile (06-10). */ runtimeConfig: unknown; + /** The build's `APP_PROFILE`; the runtime document must be for it. */ + appProfile: string; buildId: string; releaseId: string; }>): ServiceWorkerBuildInput { @@ -70,7 +73,14 @@ export function resolveServiceWorkerBuildInput(input: Readonly<{ throw new TypeError("Generated contract set digest is invalid."); } const runtimeConfig = record(input.runtimeConfig); - const releaseManifestUrl = runtimeConfig?.RELEASE_MANIFEST_URL; + // 06-10 / F-8. `public/config.json` is the local profile; a worker built from + // it for another profile would carry that profile's URLs. + if (runtimeConfig?.APP_ENV !== input.appProfile) { + throw new TypeError( + `Service Worker runtime config APP_ENV ${String(runtimeConfig?.APP_ENV)} does not match APP_PROFILE ${input.appProfile}.`, + ); + } + const releaseManifestUrl = runtimeConfig.RELEASE_MANIFEST_URL; if ( typeof releaseManifestUrl !== "string" || releaseManifestUrl.length === 0 || diff --git a/tests/unit/service-worker-build-input.test.ts b/tests/unit/service-worker-build-input.test.ts index b716aa53..1169ce42 100644 --- a/tests/unit/service-worker-build-input.test.ts +++ b/tests/unit/service-worker-build-input.test.ts @@ -28,6 +28,12 @@ const ASSET_ROWS: readonly StaticAssetRow[] = Object.freeze([ }), ]); +/** The shape `scripts/generate-runtime-config.ts` writes to `dist/config.json`. */ +const LOCAL_RUNTIME_CONFIG = Object.freeze({ + APP_ENV: "local", + RELEASE_MANIFEST_URL: "/release-manifest.json", +}); + describe("service worker build input", () => { const selection = { mode: "ACTIVE" as const, @@ -48,7 +54,8 @@ describe("service worker build input", () => { selection: null, assets, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }), @@ -58,7 +65,8 @@ describe("service worker build input", () => { selection, assets: null, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }), @@ -71,7 +79,8 @@ describe("service worker build input", () => { selection, assets: { ...assets, buildId: "old-build" }, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }), @@ -84,7 +93,8 @@ describe("service worker build input", () => { selection: { ...selection, handlers: ["WEB_PUSH"] }, assets, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }), @@ -97,7 +107,8 @@ describe("service worker build input", () => { selection, assets, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }), @@ -113,7 +124,8 @@ describe("service worker build input", () => { const base = { selection, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }; @@ -182,13 +194,35 @@ describe("service worker build input", () => { } }); + // 06-10 / F-8. The worker build read `public/config.json`, the local + // profile, for every build. It now reads the profile document the build + // materialized, and a document for another profile fails the build. + it("rejects a runtime document for a profile other than the build's", () => { + const build = (appProfile: string) => + resolveServiceWorkerBuildInput({ + selection, + assets, + contractSet: { setDigest: digest("b") }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile, + buildId: "build-1", + releaseId: "release-1", + }); + + expect(() => build("production")).toThrow( + /APP_ENV local does not match APP_PROFILE production/u, + ); + expect(build("local").releaseManifestUrl).toBe("/release-manifest.json"); + }); + it("accepts generator-shaped output unchanged", () => { expect(() => resolveServiceWorkerBuildInput({ selection, assets, contractSet: { setDigest: digest("b") }, - runtimeConfig: { RELEASE_MANIFEST_URL: "/release-manifest.json" }, + runtimeConfig: LOCAL_RUNTIME_CONFIG, + appProfile: "local", buildId: "build-1", releaseId: "release-1", }), diff --git a/vite.service-worker.config.ts b/vite.service-worker.config.ts index 28eeb96e..9ecce64b 100644 --- a/vite.service-worker.config.ts +++ b/vite.service-worker.config.ts @@ -48,8 +48,10 @@ const contractSet = await readGenerated<{ setDigest?: string }>( "./.generated/frontend-runtime/contract-set.ts", "BUILD_CONTRACT_SET", ); +// 06-10 / F-8. The profile document `scripts/generate-runtime-config.ts` wrote +// in step 4, not `public/config.json`, which is always the local profile. const runtimeConfig = JSON.parse( - await readFile("public/config.json", "utf8"), + await readFile("dist/config.json", "utf8"), ) as Record; const buildId = process.env.VITE_BUILD_ID ?? "local-build"; @@ -59,6 +61,7 @@ const buildInput = resolveServiceWorkerBuildInput({ assets, contractSet, runtimeConfig, + appProfile: process.env.APP_PROFILE ?? "local", buildId, releaseId, });