Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-prerender-basename-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Fix prerender output paths to not include basename
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
- kno-raziel
- knownasilya
- koojaa
- korkt-kim
- KostiantynPopovych
- KubasuIvanSakwa
- KutnerUri
Expand Down
42 changes: 42 additions & 0 deletions integration/vite-prerender-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3008,5 +3008,47 @@ test.describe("Prerendering", () => {
);
expect(requests).toEqual([]);
});

test("Prerenders files in correct path with basename", async () => {
fixture = await createFixture({
prerender: true,
files: {
"react-router.config.ts": reactRouterConfig({
prerender: ["/", "/about"],
basename: "/base",
}),
"vite.config.ts": files["vite.config.ts"],
"app/root.tsx": js`
import { Outlet, Scripts } from "react-router";
export function Layout({ children }) {
return (
<html><body>{children}<Scripts /></body></html>
);
}
export default function Root() {
return <Outlet />;
}
`,
"app/routes/_index.tsx": js`
export function loader() { return "INDEX"; }
export default function Index() { return <h1>Index</h1>; }
`,
"app/routes/about.tsx": js`
export function loader() { return "ABOUT"; }
export default function About() { return <h1>About</h1>; }
`,
},
});

let clientDir = path.join(fixture.projectDir, "build", "client");
// basename should NOT create a subfolder - it's the deployment path, not build output path
expect(listAllFiles(clientDir).sort()).toEqual([
"_root.data",
"about.data",
"about/index.html",
"favicon.ico",
"index.html",
]);
});
});
});
9 changes: 6 additions & 3 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2934,7 +2934,7 @@ async function prerenderData(
}

// Write out the .data file
let outfile = path.join(clientBuildDirectory, ...normalizedPath.split("/"));
let outfile = path.join(clientBuildDirectory, ...dataRequestPath.split("/"));
await mkdir(path.dirname(outfile), { recursive: true });
await writeFile(outfile, data);
viteConfig.logger.info(
Expand Down Expand Up @@ -2996,7 +2996,7 @@ async function prerenderRoute(
// Write out the HTML file
let outfile = path.join(
clientBuildDirectory,
...normalizedPath.split("/"),
...prerenderPath.split("/"),
"index.html",
);
await mkdir(path.dirname(outfile), { recursive: true });
Expand Down Expand Up @@ -3032,7 +3032,10 @@ async function prerenderResourceRoute(
}

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