Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle prerendering paths missing a leading slash #12684

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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/lazy-elephants-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Fix mismatch in prerendering html/data files when path is missing a leading slash
59 changes: 56 additions & 3 deletions integration/vite-prerender-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,59 @@ test.describe("Prerendering", () => {
});
});

test("Adds leading slashes if omitted in config", async () => {
fixture = await createFixture({
prerender: true,
files: {
...files,
"react-router.config.ts": js`
export default {
async prerender() {
await new Promise(r => setTimeout(r, 1));
return ['/', 'about'];
},
}
`,
"vite.config.ts": js`
import { defineConfig } from "vite";
import { reactRouter } from "@react-router/dev/vite";
export default defineConfig({
build: { manifest: true },
plugins: [
reactRouter()
],
});
`,
},
});
appFixture = await createAppFixture(fixture);

let clientDir = path.join(fixture.projectDir, "build", "client");
expect(listAllFiles(clientDir).sort()).toEqual([
"__manifest",
"_root.data",
"about.data",
"about/index.html",
"favicon.ico",
"index.html",
]);

let res = await fixture.requestDocument("/");
let html = await res.text();
expect(html).toMatch("<title>Index Title: Index Loader Data</title>");
expect(html).toMatch("<h1>Root</h1>");
expect(html).toMatch('<h2 data-route="true">Index</h2>');
expect(html).toMatch('<p data-loader-data="true">Index Loader Data</p>');

res = await fixture.requestDocument("/about");
html = await res.text();
expect(html).toMatch("<title>About Title: About Loader Data</title>");
expect(html).toMatch("<h1>Root</h1>");
expect(html).toMatch('<h2 data-route="true">About</h2>');
expect(html).toMatch('<p data-loader-data="true">About Loader Data</p>');
});

test("Hydrates into a navigable app", async ({ page }) => {
fixture = await createFixture({
prerender: true,
Expand Down Expand Up @@ -588,7 +641,7 @@ test.describe("Prerendering", () => {
"vite.config.ts": js`
import { defineConfig } from "vite";
import { reactRouter } from "@react-router/dev/vite";
export default defineConfig({
build: { manifest: true },
plugins: [reactRouter()],
Expand All @@ -602,7 +655,7 @@ test.describe("Prerendering", () => {
data: "한글 데이터 - UTF-8 문자",
};
}
export default function Comp() {
let data = useLoaderData();
return (
Expand All @@ -622,7 +675,7 @@ test.describe("Prerendering", () => {
data: "非プリレンダリングデータ - UTF-8文字",
};
}
export default function Comp() {
let data = useLoaderData();
return (
Expand Down
3 changes: 2 additions & 1 deletion packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,8 @@ async function handlePrerender(
"X-React-Router-Prerender": "yes",
};
for (let path of routesToPrerender) {
let matches = matchRoutes(routes, path);
// Ensure we have a leading slash for matching
let matches = matchRoutes(routes, `/${path}/`.replace(/^\/\/+/, "/"));
let hasLoaders = matches?.some((m) => m.route.loader);
let data: string | undefined;
if (hasLoaders) {
Expand Down
Loading