fix(service-worker): build the worker from the profile config (06-10)

The worker build read public/config.json, the local profile, even in a
production build (F-8). It now reads dist/config.json and fails when that
document's APP_ENV is not the build's APP_PROFILE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
donghyeon-ka
2026-09-27 12:57:41 +09:00
co-authored by Claude Opus 5.5
parent dd7a44677b
commit cefb0b2975
4 changed files with 59 additions and 10 deletions
+3 -1
View File
@@ -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;
+11 -1
View File
@@ -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 ||
+41 -7
View File
@@ -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",
}),
+4 -1
View File
@@ -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<string, string>;
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,
});