From e215c849fbb02730f8d1f83dcf9dd16b549fedc6 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:38:34 +0900 Subject: [PATCH] fix(build): serve config and manifest URLs under the router base path (13-07) A /app/ build still fetched /config.json and /release-manifest.json and failed at boot. The config URL default and a root-relative profile manifest URL now follow VITE_ROUTER_BASE_PATH, and an explicit config URL outside the base fails the build. Co-Authored-By: Claude Opus 5.5 --- scripts/generate-runtime-config.ts | 11 +++++ src/contracts/env.ts | 4 +- tests/unit/sub-path-deployment.test.ts | 66 ++++++++++++++++++++++++++ vite.config.ts | 20 +++++++- 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 tests/unit/sub-path-deployment.test.ts diff --git a/scripts/generate-runtime-config.ts b/scripts/generate-runtime-config.ts index 9ca4ed2b..5e7e8b84 100644 --- a/scripts/generate-runtime-config.ts +++ b/scripts/generate-runtime-config.ts @@ -53,6 +53,17 @@ export async function generateRuntimeConfig( const supplied = environment[variable]; if (supplied !== undefined && supplied !== "") draft[field] = supplied; } + // 13-07. dist, and the release manifest in it, is served under the router + // base path; a root-relative profile URL is rebased onto it. + const basePath = environment["VITE_ROUTER_BASE_PATH"] || "/"; + const manifestUrl = draft["RELEASE_MANIFEST_URL"]; + if ( + typeof manifestUrl === "string" && + manifestUrl.startsWith("/") && + !manifestUrl.startsWith(basePath) + ) { + draft["RELEASE_MANIFEST_URL"] = `${basePath}${manifestUrl.slice(1)}`; + } const buildId = environment["VITE_BUILD_ID"] ?? "local-build"; const releaseId = environment["RELEASE_ID"] ?? "local-release"; draft["BUILD_ID"] = buildId; diff --git a/src/contracts/env.ts b/src/contracts/env.ts index 3b756399..b9905a3b 100644 --- a/src/contracts/env.ts +++ b/src/contracts/env.ts @@ -136,7 +136,9 @@ export function getBuildConfig( const buildId = environment.VITE_BUILD_ID || "local-build"; const commitSha = environment.VITE_COMMIT_SHA || "local"; const routerBasePath = environment.VITE_ROUTER_BASE_PATH || "/"; - const runtimeConfigUrl = environment.VITE_RUNTIME_CONFIG_URL || "/config.json"; + // 13-07. dist is served under the base path, so the default follows it. + const runtimeConfigUrl = + environment.VITE_RUNTIME_CONFIG_URL || `${routerBasePath}config.json`; return Object.freeze({ buildId, commitSha, routerBasePath, runtimeConfigUrl }); } diff --git a/tests/unit/sub-path-deployment.test.ts b/tests/unit/sub-path-deployment.test.ts new file mode 100644 index 00000000..1d10e0cd --- /dev/null +++ b/tests/unit/sub-path-deployment.test.ts @@ -0,0 +1,66 @@ +import { spawnSync } from "node:child_process"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { afterAll, describe, expect, it } from "vitest"; + +import { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts"; +import { getBuildConfig } from "../../src/contracts/env.ts"; + +/** + * 13-07. A build served from a sub-path must fetch its runtime config and + * release manifest from that sub-path; dist is served under the base, so a + * root URL is a 404 at boot. + */ +const roots: string[] = []; + +afterAll(async () => { + await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true }))); +}); + +describe("sub-path deployment URLs (13-07)", () => { + it("derives the runtime config URL from the base path", () => { + expect(getBuildConfig({ VITE_ROUTER_BASE_PATH: "/app/" }).runtimeConfigUrl).toBe( + "/app/config.json", + ); + expect(getBuildConfig({}).runtimeConfigUrl).toBe("/config.json"); + expect( + getBuildConfig({ + VITE_ROUTER_BASE_PATH: "/app/", + VITE_RUNTIME_CONFIG_URL: "/app/runtime.json", + }).runtimeConfigUrl, + ).toBe("/app/runtime.json"); + }); + + it("rebases the profile's root release manifest URL onto the base path", async () => { + const rebased = await generateRuntimeConfig("local", { VITE_ROUTER_BASE_PATH: "/app/" }); + expect(rebased["RELEASE_MANIFEST_URL"]).toBe("/app/release-manifest.json"); + const root = await generateRuntimeConfig("local", {}); + expect(root["RELEASE_MANIFEST_URL"]).toBe("/release-manifest.json"); + }); + + it("fails the build when an explicit runtime config URL leaves the base path", async () => { + const outDir = await mkdtemp(path.join(tmpdir(), "sub-path-build-")); + roots.push(outDir); + const environment = Object.fromEntries( + Object.entries(process.env).filter(([name]) => !name.startsWith("VITE_")), + ); + const run = spawnSync( + path.resolve("node_modules/.bin/vite"), + ["build", "--outDir", outDir, "--emptyOutDir", "--logLevel", "error"], + { + encoding: "utf8", + env: { + ...environment, + VITE_ROUTER_BASE_PATH: "/app/", + VITE_RUNTIME_CONFIG_URL: "/config.json", + }, + }, + ); + expect(run.status).not.toBe(0); + expect(run.stderr + run.stdout).toMatch( + /VITE_RUNTIME_CONFIG_URL \/config\.json is outside VITE_ROUTER_BASE_PATH \/app\//u, + ); + }, 120_000); +}); diff --git a/vite.config.ts b/vite.config.ts index ffd866ea..adee7e0e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -43,10 +43,28 @@ function routerBasePath(environment: NodeJS.ProcessEnv): string { return declared; } +/** + * 13-07. The runtime document ships inside dist, which is served under the + * base path. An explicit root-relative URL outside it would 404 at boot. + */ +function assertRuntimeConfigUnderBase( + environment: NodeJS.ProcessEnv, + base: string, +): void { + const declared = environment["VITE_RUNTIME_CONFIG_URL"]; + if (declared?.startsWith("/") && !declared.startsWith(base)) { + throw new Error( + `VITE_RUNTIME_CONFIG_URL ${declared} is outside VITE_ROUTER_BASE_PATH ${base}`, + ); + } +} + export default defineConfig(({ mode }) => { assertDeclaredBuildVariables(mode); + const base = routerBasePath(process.env); + assertRuntimeConfigUnderBase(process.env, base); return { - base: routerBasePath(process.env), + base, plugins: [react(), tailwindcss(), viteModuleInventoryPlugin()], build: { manifest: true,