Skip to content

Fix root loader data on initial load redirects in SPA mode #13222

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

Merged
merged 1 commit into from
Mar 17, 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/four-ligers-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix root loader data on initial load redirects in SPA mode
73 changes: 73 additions & 0 deletions integration/vite-spa-mode-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,79 @@ test.describe("SPA Mode", () => {
expect(await page.locator("h1").textContent()).toBe("Parent: 1");
expect(await page.locator("h2").textContent()).toBe("Child");
});

test("does not hydrate root loaderData if there's no root loader", async ({
page,
}) => {
fixture = await createFixture({
spaMode: true,
files: {
"react-router.config.ts": reactRouterConfig({
ssr: false,
splitRouteModules,
}),
"app/root.tsx": js`
import {
Meta,
Links,
Outlet,
Routes,
Route,
Scripts,
ScrollRestoration,
} from "react-router";

export function Layout({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}

let count = 0;
export function clientLoader() {
return ++count;
}

export default function Root({ loaderData }) {
return (
<>
<h1>{loaderData}</h1>
<Outlet />
</>
);
}
`,
"app/routes/_index.tsx": js`
import { redirect } from 'react-router';
export const clientLoader = () => redirect('/target');
export default function() { return null; }
`,
"app/routes/target.tsx": js`
import { useRouteLoaderData } from 'react-router';
export default function Comp() {
return <h2>{useRouteLoaderData('root')}</h2>;
}
`,
},
});
appFixture = await createAppFixture(fixture);

let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await page.waitForSelector("h2");
expect(await page.locator("h1").textContent()).toBe("2");
expect(await page.locator("h2").textContent()).toBe("2");
});
});

test.describe("normal apps", () => {
Expand Down
15 changes: 12 additions & 3 deletions packages/react-router/lib/dom-export/hydrated-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,19 @@ function createHydratedRouter({

let hydrationData: HydrationState | undefined = undefined;
let loaderData = ssrInfo.context.state.loaderData;
// In SPA mode we only hydrate build-time root loader data
if (ssrInfo.context.isSpaMode) {
// In SPA mode we hydrate in any build-time loader data which should be
// limited to the root route
hydrationData = { loaderData };
if (
ssrInfo.manifest.routes.root?.hasLoader &&
loaderData &&
"root" in loaderData
) {
hydrationData = {
loaderData: {
root: loaderData.root,
},
};
}
} else {
// Create a shallow clone of `loaderData` we can mutate for partial hydration.
// When a route exports a `clientLoader` and a `HydrateFallback`, the SSR will
Expand Down
Loading