diff --git a/scripts/generate-service-worker-assets.ts b/scripts/generate-service-worker-assets.ts index ee1a1991..5bccadec 100644 --- a/scripts/generate-service-worker-assets.ts +++ b/scripts/generate-service-worker-assets.ts @@ -9,6 +9,7 @@ import { import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises"; import path from "node:path"; +import { getBuildConfig } from "../src/contracts/env.ts"; import { HASHED_STATIC_ASSET_NAME, SERVICE_WORKER_BOUNDS, @@ -41,10 +42,15 @@ const EXCLUDED_FILES: ReadonlySet = new Set([ /** Vite emits content-hashed names; only those may be treated as immutable. */ const HASHED_NAME = HASHED_STATIC_ASSET_NAME; +/** + * `basePath` is the router base path (`VITE_ROUTER_BASE_PATH`): dist, and so + * every asset, is served under it (0929/06-02). + */ export async function collectStaticAssets( distDirectory: string, buildId: string, releaseId: string, + basePath = "/", ): Promise { const files = await walk(distDirectory, distDirectory); const assets: StaticAssetManifestV1["assets"][number][] = []; @@ -63,7 +69,7 @@ export async function collectStaticAssets( // SW-02. The URL is checked against the same predicate the runtime decoder // applies. Emitting a path the decoder will refuse turned a correct build // into a runtime contract failure discovered only at install time. - const url = `/${relative.split(path.sep).join("/")}`; + const url = `${basePath}${relative.split(path.sep).join("/")}`; if (!isCanonicalStaticAssetUrl(url)) { throw new Error( `Static asset path is not canonical for the service worker manifest: ${relative}`, @@ -129,7 +135,12 @@ async function main(): Promise { const distDirectory = process.argv[2] ?? "dist"; const buildId = process.env.VITE_BUILD_ID ?? "local-build"; const releaseId = process.env.RELEASE_ID ?? "local-release"; - const manifest = await collectStaticAssets(distDirectory, buildId, releaseId); + const manifest = await collectStaticAssets( + distDirectory, + buildId, + releaseId, + getBuildConfig(process.env).routerBasePath, + ); const source = [ "// Generated by scripts/generate-service-worker-assets.ts. Do not edit.", "", diff --git a/tests/unit/service-worker-build-output.test.ts b/tests/unit/service-worker-build-output.test.ts index b82a33fe..ae24bfa0 100644 --- a/tests/unit/service-worker-build-output.test.ts +++ b/tests/unit/service-worker-build-output.test.ts @@ -1,7 +1,7 @@ import { access, readFile } from "node:fs/promises"; import path from "node:path"; -import { afterAll, describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts"; import { @@ -34,25 +34,28 @@ const BUILD_ID = "sw-profile-active"; let builds: FixtureBuilds<"active"> | undefined; +beforeAll(async () => { + builds = await prepareFixtureBuilds("sw-profile", [ + { + name: "active", + env: PROFILE, + edit: (root) => + editOnce( + path.join(root, "src/features/installed-runtime-capabilities.ts"), + "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", + ACTIVE_SELECTION, + ), + }, + ]); +}, 180_000); + afterAll(async () => { await removeFixtureBuilds(builds); }); -describe("Service Worker build output (0929/06-01)", () => { +describe("Service Worker build output (0929/06-01, 0929/06-02)", () => { it("keeps the profile runtime config step 4 wrote", async () => { - builds = await prepareFixtureBuilds("sw-profile", [ - { - name: "active", - env: PROFILE, - edit: (root) => - editOnce( - path.join(root, "src/features/installed-runtime-capabilities.ts"), - "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", - ACTIVE_SELECTION, - ), - }, - ]); - const dist = builds.dist.active; + const dist = builds!.dist.active; // The worker build really ran; otherwise nothing below tests step 6. await access(path.join(dist, "service-worker.js")); @@ -67,5 +70,28 @@ describe("Service Worker build output (0929/06-01)", () => { RELEASE_ID: BUILD_ID, }), ); - }, 180_000); + }); + + // 0929/06-02. Under /app/ the worker must precache /app/assets/... and know + // /app/config.json; root URLs 404 at install and the worker never installs. + it("gives the worker URLs under the router base path", async () => { + const generated = (await import( + path.join( + builds!.root, + ".generated/frontend-runtime/service-worker-assets.ts", + ) + )) as { SERVICE_WORKER_ASSETS: { assets: readonly { url: string }[] } }; + const urls = generated.SERVICE_WORKER_ASSETS.assets.map((asset) => asset.url); + expect(urls.length).toBeGreaterThan(0); + expect(urls.filter((url) => !url.startsWith("/app/assets/"))).toEqual([]); + + const worker = await readFile( + path.join(builds!.dist.active, "service-worker.js"), + "utf8", + ); + // Property names survive minification; the quote style does not. + expect( + /runtimeConfigUrl:\s*["'`]([^"'`]*)["'`]/u.exec(worker)?.[1], + ).toBe("/app/config.json"); + }); }); diff --git a/tests/unit/service-worker-web-push-remediation.test.ts b/tests/unit/service-worker-web-push-remediation.test.ts index 9a163265..dfb9efc9 100644 --- a/tests/unit/service-worker-web-push-remediation.test.ts +++ b/tests/unit/service-worker-web-push-remediation.test.ts @@ -125,6 +125,31 @@ describe("SW-02 the generator and the decoder share one path grammar", () => { } }); + // 0929/06-02. dist is served under the router base path; a root URL made + // the worker's install request /assets/... and fail under /app/. + it("puts every asset URL under the router base path", async () => { + const { collectStaticAssets } = await import( + "../../scripts/generate-service-worker-assets.ts" + ); + const root = await distWith({ + "assets/index-abcdefgh.js": "entry", + "assets/nested/chunk-abcdefgz.css": "style", + }); + + const manifest = await collectStaticAssets( + root, + "build-1", + "release-1", + "/app/", + ); + + expect(manifest.assets.map((asset) => asset.url)).toEqual([ + "/app/assets/index-abcdefgh.js", + "/app/assets/nested/chunk-abcdefgz.css", + ]); + expect(decodeStaticAssetManifest(manifest)).toMatchObject({ ok: true }); + }); + it.each([ { label: "an at sign", name: "bad@name-abcdefgh.js" }, { label: "a space", name: "bad name-abcdefgh.js" }, diff --git a/vite.service-worker.config.ts b/vite.service-worker.config.ts index 2477b752..aee34b9e 100644 --- a/vite.service-worker.config.ts +++ b/vite.service-worker.config.ts @@ -1,6 +1,7 @@ import { readFile } from "node:fs/promises"; import { defineConfig } from "vite"; +import { getBuildConfig } from "./src/contracts/env.ts"; import { SERVICE_WORKER_CACHE_SCHEMA_VERSION, SERVICE_WORKER_PROTOCOL_VERSION, @@ -95,8 +96,9 @@ export default defineConfig({ }), __CA_SERVICE_WORKER_ASSETS__: JSON.stringify(buildInput.assets), __CA_SERVICE_WORKER_HANDLERS__: JSON.stringify(buildInput.handlers), + // 0929/06-02. The page's own default, which follows the router base path. __CA_RUNTIME_CONFIG_URL__: JSON.stringify( - process.env.VITE_RUNTIME_CONFIG_URL ?? "/config.json", + getBuildConfig(process.env).runtimeConfigUrl, ), __CA_RELEASE_MANIFEST_URL__: JSON.stringify( buildInput.releaseManifestUrl,