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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:38:34 +09:00
co-authored by Claude Opus 5.5
parent 2ec59ad9ee
commit e215c849fb
4 changed files with 99 additions and 2 deletions
+11
View File
@@ -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;
+3 -1
View File
@@ -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 });
}
+66
View File
@@ -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);
});
+19 -1
View File
@@ -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,