Skip to content

Stop inserting null placeholders for non-executed loaders in staticHandler.query #13223

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 4 commits into from
Apr 9, 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
9 changes: 9 additions & 0 deletions .changeset/smart-ads-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"react-router": patch
---

Do not automatically add `null` to `staticHandler.query()` `context.loaderData` if routes do not have loaders

- This was a Remix v2 implementation detail inadvertently left in for React Router v7
- Now that we allow returning `undefined` from loaders, our prior check of `loaderData[routeId] !== undefined` was no longer sufficient and was changed to a `routeId in loaderData` check - these `null` values can cause issues for this new check
- ⚠️ This could be a "breaking bug fix" for you if you are doing manual SSR with `createStaticHandler()`/`<StaticRouterProvider>`, and using `context.loaderData` to control `<RouterProvider>` hydration behavior on the client
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,7 @@ describe("A <StaticRouterProvider>", () => {

let expectedJsonString = JSON.stringify(
JSON.stringify({
loaderData: {
0: null,
"0-0": null,
},
loaderData: {},
actionData: null,
errors: null,
})
Expand Down
20 changes: 5 additions & 15 deletions packages/react-router/__tests__/router/lazy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2930,9 +2930,7 @@ describe("lazily loaded route modules", () => {
!(context instanceof Response),
"Expected a StaticContext instance"
);
expect(context.loaderData).toEqual({
root: null,
});
expect(context.loaderData).toEqual({});
expect(context.errors).toEqual({
lazy: new Error("LAZY LOADER ERROR"),
});
Expand Down Expand Up @@ -2969,9 +2967,7 @@ describe("lazily loaded route modules", () => {
!(context instanceof Response),
"Expected a StaticContext instance"
);
expect(context.loaderData).toEqual({
root: null,
});
expect(context.loaderData).toEqual({});
expect(context.errors).toEqual({
lazy: new Error("LAZY LOADER ERROR"),
});
Expand Down Expand Up @@ -3006,9 +3002,7 @@ describe("lazily loaded route modules", () => {
!(context instanceof Response),
"Expected a StaticContext instance"
);
expect(context.loaderData).toEqual({
root: null,
});
expect(context.loaderData).toEqual({});
expect(context.errors).toEqual({
root: new Error("LAZY LOADER ERROR"),
});
Expand Down Expand Up @@ -3045,9 +3039,7 @@ describe("lazily loaded route modules", () => {
!(context instanceof Response),
"Expected a StaticContext instance"
);
expect(context.loaderData).toEqual({
root: null,
});
expect(context.loaderData).toEqual({});
expect(context.errors).toEqual({
root: new Error("LAZY LOADER ERROR"),
});
Expand Down Expand Up @@ -3084,9 +3076,7 @@ describe("lazily loaded route modules", () => {
!(context instanceof Response),
"Expected a StaticContext instance"
);
expect(context.loaderData).toEqual({
root: null,
});
expect(context.loaderData).toEqual({});
expect(context.errors).toEqual({
root: new Error("LAZY LOADER ERROR"),
});
Expand Down
9 changes: 2 additions & 7 deletions packages/react-router/__tests__/router/ssr-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("ssr", () => {
});
});

it("should fill in null loaderData values for routes without loaders", async () => {
it("should not fill in null loaderData values for routes without loaders", async () => {
let { query } = createStaticHandler([
{
id: "root",
Expand Down Expand Up @@ -215,10 +215,7 @@ describe("ssr", () => {
let context = await query(createRequest("/none"));
expect(context).toMatchObject({
actionData: null,
loaderData: {
root: null,
none: null,
},
loaderData: {},
errors: null,
location: { pathname: "/none" },
});
Expand All @@ -228,9 +225,7 @@ describe("ssr", () => {
expect(context).toMatchObject({
actionData: null,
loaderData: {
root: null,
a: "A",
b: null,
},
errors: null,
location: { pathname: "/a/b" },
Expand Down
32 changes: 4 additions & 28 deletions packages/react-router/__tests__/server-runtime/server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,7 @@ describe("shared server runtime", () => {
let context = calls[0][3].staticHandlerContext as StaticHandlerContext;
expect(context.errors).toBeTruthy();
expect(context.errors!.root.status).toBe(400);
expect(context.loaderData).toEqual({
root: null,
"routes/test": null,
});
expect(context.loaderData).toEqual({});
});

test("thrown action responses bubble up for index routes", async () => {
Expand Down Expand Up @@ -1386,10 +1383,7 @@ describe("shared server runtime", () => {
let context = calls[0][3].staticHandlerContext as StaticHandlerContext;
expect(context.errors).toBeTruthy();
expect(context.errors!.root.status).toBe(400);
expect(context.loaderData).toEqual({
root: null,
"routes/_index": null,
});
expect(context.loaderData).toEqual({});
});

test("thrown action responses catch deep", async () => {
Expand Down Expand Up @@ -1435,7 +1429,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/test"].status).toBe(400);
expect(context.loaderData).toEqual({
root: "root",
"routes/test": null,
});
});

Expand Down Expand Up @@ -1482,7 +1475,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/_index"].status).toBe(400);
expect(context.loaderData).toEqual({
root: "root",
"routes/_index": null,
});
});

Expand Down Expand Up @@ -1537,8 +1529,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/__layout"].data).toBe("action");
expect(context.loaderData).toEqual({
root: "root",
"routes/__layout": null,
"routes/__layout/test": null,
});
});

Expand Down Expand Up @@ -1593,8 +1583,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/__layout"].data).toBe("action");
expect(context.loaderData).toEqual({
root: "root",
"routes/__layout": null,
"routes/__layout/index": null,
});
});

Expand Down Expand Up @@ -1728,10 +1716,7 @@ describe("shared server runtime", () => {
expect(context.errors!.root).toBeInstanceOf(Error);
expect(context.errors!.root.message).toBe("Unexpected Server Error");
expect(context.errors!.root.stack).toBeUndefined();
expect(context.loaderData).toEqual({
root: null,
"routes/test": null,
});
expect(context.loaderData).toEqual({});
});

test("action errors bubble up for index routes", async () => {
Expand Down Expand Up @@ -1777,10 +1762,7 @@ describe("shared server runtime", () => {
expect(context.errors!.root).toBeInstanceOf(Error);
expect(context.errors!.root.message).toBe("Unexpected Server Error");
expect(context.errors!.root.stack).toBeUndefined();
expect(context.loaderData).toEqual({
root: null,
"routes/_index": null,
});
expect(context.loaderData).toEqual({});
});

test("action errors catch deep", async () => {
Expand Down Expand Up @@ -1830,7 +1812,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/test"].stack).toBeUndefined();
expect(context.loaderData).toEqual({
root: "root",
"routes/test": null,
});
});

Expand Down Expand Up @@ -1881,7 +1862,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/_index"].stack).toBeUndefined();
expect(context.loaderData).toEqual({
root: "root",
"routes/_index": null,
});
});

Expand Down Expand Up @@ -1940,8 +1920,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/__layout"].stack).toBeUndefined();
expect(context.loaderData).toEqual({
root: "root",
"routes/__layout": null,
"routes/__layout/test": null,
});
});

Expand Down Expand Up @@ -2000,8 +1978,6 @@ describe("shared server runtime", () => {
expect(context.errors!["routes/__layout"].stack).toBeUndefined();
expect(context.loaderData).toEqual({
root: "root",
"routes/__layout": null,
"routes/__layout/index": null,
});
});

Expand Down
16 changes: 1 addition & 15 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4165,11 +4165,7 @@ export function createStaticHandler(
if (!dataStrategy && !dsMatches.some((m) => m.shouldLoad)) {
return {
matches,
// Add a null for all matched routes for proper revalidation on the client
loaderData: matches.reduce(
(acc, m) => Object.assign(acc, { [m.route.id]: null }),
{}
),
loaderData: {},
errors:
pendingActionResult && isErrorResult(pendingActionResult[1])
? {
Expand Down Expand Up @@ -4202,16 +4198,6 @@ export function createStaticHandler(
skipLoaderErrorBubbling
);

// Add a null for any non-loader matches for proper revalidation on the client
let executedLoaders = new Set<string>(
dsMatches.filter((m) => m.shouldLoad).map((match) => match.route.id)
);
matches.forEach((match) => {
if (!executedLoaders.has(match.route.id)) {
handlerContext.loaderData[match.route.id] = null;
}
});

return {
...handlerContext,
matches,
Expand Down