Skip to content

Dedupe calls to route.lazy functions #13260

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
Mar 20, 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/green-windows-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Dedupe calls to `route.lazy` functions
60 changes: 37 additions & 23 deletions packages/react-router/__tests__/router/data-strategy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type {
DataStrategyMatch,
DataStrategyResult,
} from "../../lib/router/utils";
import { createDeferred, setup } from "./utils/data-router-setup";
import {
createDeferred,
createLazyStub,
setup,
} from "./utils/data-router-setup";
import { createFormData, tick } from "./utils/utils";

describe("router dataStrategy", () => {
Expand Down Expand Up @@ -95,6 +99,10 @@ describe("router dataStrategy", () => {
keyedResults(matches, results)
)
);
let { lazyStub: lazyJsonStub, lazyDeferred: lazyJsonDeferred } =
createLazyStub();
let { lazyStub: lazyTextStub, lazyDeferred: lazyTextDeferred } =
createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -103,24 +111,24 @@ describe("router dataStrategy", () => {
{
id: "json",
path: "/test",
lazy: true,
lazy: lazyJsonStub,
children: [
{
id: "text",
index: true,
lazy: true,
lazy: lazyTextStub,
},
],
},
],
dataStrategy,
});

let A = await t.navigate("/test");
await A.lazy.json.resolve({
await t.navigate("/test");
await lazyJsonDeferred.resolve({
loader: () => ({ message: "hello json" }),
});
await A.lazy.text.resolve({
await lazyTextDeferred.resolve({
loader: () => "hello text",
});
expect(t.router.state.loaderData).toEqual({
Expand Down Expand Up @@ -211,6 +219,7 @@ describe("router dataStrategy", () => {
});

it("should allow custom implementations to override default behavior with lazy", async () => {
let { lazyStub, lazyDeferred } = createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -219,7 +228,7 @@ describe("router dataStrategy", () => {
{
id: "test",
path: "/test",
lazy: true,
lazy: lazyStub,
},
],
async dataStrategy({ matches }) {
Expand All @@ -234,8 +243,8 @@ describe("router dataStrategy", () => {
},
});

let A = await t.navigate("/test");
await A.lazy.test.resolve({ loader: () => "TEST" });
await t.navigate("/test");
await lazyDeferred.resolve({ loader: () => "TEST" });

expect(t.router.state.loaderData).toMatchObject({
test: 'Route ID "test" returned "TEST"',
Expand Down Expand Up @@ -365,6 +374,7 @@ describe("router dataStrategy", () => {
});

it("does not require resolve to be called if a match is not being loaded", async () => {
let { lazyStub, lazyDeferred } = createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -379,7 +389,7 @@ describe("router dataStrategy", () => {
{
id: "child",
path: "child",
lazy: true,
lazy: lazyStub,
},
],
},
Expand Down Expand Up @@ -415,7 +425,7 @@ describe("router dataStrategy", () => {
});

let B = await t.navigate("/parent/child");
await B.lazy.child.resolve({ loader: () => "CHILD" });
await lazyDeferred.resolve({ loader: () => "CHILD" });

// no-op
await B.loaders.parent.resolve("XXX");
Expand All @@ -441,6 +451,7 @@ describe("router dataStrategy", () => {
keyedResults(matches, results)
);
});
let { lazyStub, lazyDeferred } = createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -457,7 +468,7 @@ describe("router dataStrategy", () => {
{
id: "child",
path: "child",
lazy: true,
lazy: lazyStub,
},
],
},
Expand Down Expand Up @@ -500,7 +511,7 @@ describe("router dataStrategy", () => {
parent: "PARENT",
});

let C = await t.navigate("/parent/child");
await t.navigate("/parent/child");
expect(dataStrategy.mock.calls[2][0].matches).toEqual([
expect.objectContaining({
shouldLoad: false,
Expand All @@ -515,7 +526,7 @@ describe("router dataStrategy", () => {
route: expect.objectContaining({ id: "child" }),
}),
]);
await C.lazy.child.resolve({
await lazyDeferred.resolve({
action: () => "CHILD ACTION",
loader: () => "CHILD",
shouldRevalidate: () => false,
Expand Down Expand Up @@ -621,6 +632,7 @@ describe("router dataStrategy", () => {
keyedResults(matches, results)
)
);
let { lazyStub, lazyDeferred } = createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -629,17 +641,17 @@ describe("router dataStrategy", () => {
{
id: "json",
path: "/test",
lazy: true,
lazy: lazyStub,
},
],
dataStrategy,
});

let A = await t.navigate("/test", {
await t.navigate("/test", {
formMethod: "post",
formData: createFormData({}),
});
await A.lazy.json.resolve({
await lazyDeferred.resolve({
action: () => ({ message: "hello json" }),
});
expect(t.router.state.actionData).toEqual({
Expand Down Expand Up @@ -709,6 +721,7 @@ describe("router dataStrategy", () => {
keyedResults(matches, results)
)
);
let { lazyStub, lazyDeferred } = createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -717,15 +730,15 @@ describe("router dataStrategy", () => {
{
id: "json",
path: "/test",
lazy: true,
lazy: lazyStub,
},
],
dataStrategy,
});

let key = "key";
let A = await t.fetch("/test", key);
await A.lazy.json.resolve({
await t.fetch("/test", key);
await lazyDeferred.resolve({
loader: () => ({ message: "hello json" }),
});
expect(t.fetchers[key].data.message).toBe("hello json");
Expand Down Expand Up @@ -795,6 +808,7 @@ describe("router dataStrategy", () => {
keyedResults(matches, results)
)
);
let { lazyStub, lazyDeferred } = createLazyStub();
let t = setup({
routes: [
{
Expand All @@ -803,18 +817,18 @@ describe("router dataStrategy", () => {
{
id: "json",
path: "/test",
lazy: true,
lazy: lazyStub,
},
],
dataStrategy,
});

let key = "key";
let A = await t.fetch("/test", key, {
await t.fetch("/test", key, {
formMethod: "post",
formData: createFormData({}),
});
await A.lazy.json.resolve({
await lazyDeferred.resolve({
action: () => ({ message: "hello json" }),
});

Expand Down
Loading