Skip to content
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/curvy-queens-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix hydration error if a server `loader` returned `undefined`
18 changes: 18 additions & 0 deletions integration/single-fetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,24 @@ test.describe("single-fetch", () => {
expect(await app.getHtml("#date")).toContain(ISO_DATE);
});

test("allows SSR loaders to return undefined", async ({ page }) => {
let fixture = await createFixture({
files: {
...files,
"app/routes/_index.tsx": js`
export function loader() {}
export default function Index() {
return <h1>Index</h1>
}
`,
},
});
let appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/", true);
expect(await app.getHtml("h1")).toContain("Index");
});

test("loads proper data on client side navigation", async ({ page }) => {
let fixture = await createFixture({
files,
Expand Down
29 changes: 29 additions & 0 deletions packages/react-router/__tests__/router/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,35 @@ describe("a router", () => {
router.dispose();
});

it("allows routes to be initialized with undefined loaderData", async () => {
let t = setup({
routes: [
{
id: "root",
path: "/",
loader: true,
},
],
hydrationData: {
loaderData: {
root: undefined,
},
},
});

expect(t.router.state).toMatchObject({
historyAction: "POP",
location: {
pathname: "/",
},
initialized: true,
navigation: IDLE_NAVIGATION,
loaderData: {
root: undefined,
},
});
});

it("handles interruptions of initial data load", async () => {
let parentDfd = createDeferred();
let parentSpy = jest.fn(() => parentDfd.promise);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4822,7 +4822,7 @@ function shouldLoadRouteOnHydration(
return false;
}

let hasData = loaderData != null && loaderData[route.id] !== undefined;
let hasData = loaderData != null && route.id in loaderData;
let hasError = errors != null && errors[route.id] !== undefined;

// Don't run if we error'd during SSR
Expand Down