diff --git a/packages/plugin-vite/src/plugins/server_entry.ts b/packages/plugin-vite/src/plugins/server_entry.ts index 1943968a..f45017ab 100644 --- a/packages/plugin-vite/src/plugins/server_entry.ts +++ b/packages/plugin-vite/src/plugins/server_entry.ts @@ -16,9 +16,19 @@ export function serverEntryPlugin( let serverOutDir = ""; let clientOutDir = ""; let root = ""; + let basePath = ""; let isDev = false; + const getAssetPath = (id: string): string => { + if (basePath === "/") { + return `/${id}`; + } + // Ensure basePath ends with / and construct the path manually to avoid platform-specific path issues + const normalizedBase = basePath.endsWith("/") ? basePath : basePath + "/"; + return normalizedBase + id; + }; + return { name: "fresh:server_entry", applyToEnvironment(env) { @@ -29,6 +39,10 @@ export function serverEntryPlugin( }, configResolved(config) { root = config.root; + basePath = config.base || "/"; + if (basePath !== "/" && !basePath.endsWith("/")) { + basePath += "/"; + } serverEntry = pathWithRoot(options.serverEntry, config.root); serverOutDir = pathWithRoot( config.environments.ssr.build.outDir, @@ -98,7 +112,7 @@ if (import.meta.hot) import.meta.hot.accept();`; staticFiles.push({ filePath: path.join(serverOutDir, id), hash: null, - pathname: `/${id}`, + pathname: getAssetPath(id), }); } } @@ -110,7 +124,7 @@ if (import.meta.hot) import.meta.hot.accept();`; staticFiles.push({ filePath: path.join(serverOutDir, id), hash: null, - pathname: `/${id}`, + pathname: getAssetPath(id), }); } } diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index 301cf1bc..c9f8fbda 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -447,3 +447,20 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "vite build - base path asset handling", + fn: async () => { + await using res = await buildVite(DEMO_DIR, { base: "/my-app/" }); + + // Read the generated server.js to check asset paths + const serverJs = await Deno.readTextFile( + path.join(res.tmp, "_fresh", "server.js"), + ); + + // Asset paths should include the base path /my-app/ + expect(serverJs).toContain('"/my-app/assets/'); + }, + sanitizeOps: false, + sanitizeResources: false, +}); diff --git a/packages/plugin-vite/tests/test_utils.ts b/packages/plugin-vite/tests/test_utils.ts index 461d3883..912367dc 100644 --- a/packages/plugin-vite/tests/test_utils.ts +++ b/packages/plugin-vite/tests/test_utils.ts @@ -131,7 +131,10 @@ export async function withDevServer( await launchDevServer(tmp.dir, fn, env); } -export async function buildVite(fixtureDir: string) { +export async function buildVite( + fixtureDir: string, + options?: { base?: string }, +) { const tmp = await withTmpDir({ dir: path.join(import.meta.dirname!, ".."), prefix: "tmp_vite_", @@ -140,6 +143,7 @@ export async function buildVite(fixtureDir: string) { const builder = await createBuilder({ logLevel: "error", root: fixtureDir, + base: options?.base, build: { emptyOutDir: true, },