Skip to content

Commit e458b55

Browse files
committed
fix: prerender output paths should not include basename
1 parent 3126264 commit e458b55

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Fix prerender output paths to not include basename

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
- kno-raziel
231231
- knownasilya
232232
- koojaa
233+
- korkt.kim
233234
- KostiantynPopovych
234235
- KubasuIvanSakwa
235236
- KutnerUri

integration/vite-prerender-test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,5 +3008,47 @@ test.describe("Prerendering", () => {
30083008
);
30093009
expect(requests).toEqual([]);
30103010
});
3011+
3012+
test("Prerenders files in correct path with basename", async () => {
3013+
fixture = await createFixture({
3014+
prerender: true,
3015+
files: {
3016+
"react-router.config.ts": reactRouterConfig({
3017+
prerender: ["/", "/about"],
3018+
basename: "/base",
3019+
}),
3020+
"vite.config.ts": files["vite.config.ts"],
3021+
"app/root.tsx": js`
3022+
import { Outlet, Scripts } from "react-router";
3023+
export function Layout({ children }) {
3024+
return (
3025+
<html><body>{children}<Scripts /></body></html>
3026+
);
3027+
}
3028+
export default function Root() {
3029+
return <Outlet />;
3030+
}
3031+
`,
3032+
"app/routes/_index.tsx": js`
3033+
export function loader() { return "INDEX"; }
3034+
export default function Index() { return <h1>Index</h1>; }
3035+
`,
3036+
"app/routes/about.tsx": js`
3037+
export function loader() { return "ABOUT"; }
3038+
export default function About() { return <h1>About</h1>; }
3039+
`,
3040+
},
3041+
});
3042+
3043+
let clientDir = path.join(fixture.projectDir, "build", "client");
3044+
// basename should NOT create a subfolder - it's the deployment path, not build output path
3045+
expect(listAllFiles(clientDir).sort()).toEqual([
3046+
"_root.data",
3047+
"about.data",
3048+
"about/index.html",
3049+
"favicon.ico",
3050+
"index.html",
3051+
]);
3052+
});
30113053
});
30123054
});

packages/react-router-dev/vite/plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,7 +2934,7 @@ async function prerenderData(
29342934
}
29352935

29362936
// Write out the .data file
2937-
let outfile = path.join(clientBuildDirectory, ...normalizedPath.split("/"));
2937+
let outfile = path.join(clientBuildDirectory, ...dataRequestPath.split("/"));
29382938
await mkdir(path.dirname(outfile), { recursive: true });
29392939
await writeFile(outfile, data);
29402940
viteConfig.logger.info(
@@ -2996,7 +2996,7 @@ async function prerenderRoute(
29962996
// Write out the HTML file
29972997
let outfile = path.join(
29982998
clientBuildDirectory,
2999-
...normalizedPath.split("/"),
2999+
...prerenderPath.split("/"),
30003000
"index.html",
30013001
);
30023002
await mkdir(path.dirname(outfile), { recursive: true });
@@ -3032,7 +3032,8 @@ async function prerenderResourceRoute(
30323032
}
30333033

30343034
// Write out the resource route file
3035-
let outfile = path.join(clientBuildDirectory, ...normalizedPath.split("/"));
3035+
let outfilePath = `${prerenderPath}/`.replace(/\/\/+/g, "/").replace(/\/$/g, "");
3036+
let outfile = path.join(clientBuildDirectory, ...outfilePath.split("/"));
30363037
await mkdir(path.dirname(outfile), { recursive: true });
30373038
await writeFile(outfile, content);
30383039
viteConfig.logger.info(

0 commit comments

Comments
 (0)