Skip to content

Fix single fetch revalidation bug on reused routes #13105

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

Closed
wants to merge 1 commit into from
Closed
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/fair-weeks-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix single fetch bug where no revalidation request would be made when navigating upwards to a reused parent route
86 changes: 86 additions & 0 deletions packages/react-router/__tests__/router/data-strategy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,92 @@ describe("router dataStrategy", () => {
child: "CHILD",
});
});

it("does not short circuit when there are no matchesToLoad", async () => {
let dataStrategy = mockDataStrategy(async ({ matches }) => {
let results = await Promise.all(
matches.map((m) => m.resolve((handler) => handler()))
);
// Don't use keyedResults since it checks for shouldLoad and this test
// is always loading
return results.reduce(
(acc, r, i) => Object.assign(acc, { [matches[i].route.id]: r }),
{}
);
});
let t = setup({
routes: [
{
path: "/",
},
{
id: "parent",
path: "/parent",
loader: true,
children: [
{
id: "child",
path: "child",
loader: true,
},
],
},
],
dataStrategy,
});

let A = await t.navigate("/parent");
await A.loaders.parent.resolve("PARENT1");
expect(A.loaders.parent.stub).toHaveBeenCalled();
expect(t.router.state.loaderData).toEqual({
parent: "PARENT1",
});
expect(dataStrategy.mock.calls[0][0].matches).toEqual([
expect.objectContaining({
route: expect.objectContaining({
id: "parent",
}),
}),
]);

let B = await t.navigate("/parent/child");
await B.loaders.parent.resolve("PARENT2");
await B.loaders.child.resolve("CHILD");
expect(B.loaders.parent.stub).toHaveBeenCalled();
expect(B.loaders.child.stub).toHaveBeenCalled();
expect(t.router.state.loaderData).toEqual({
parent: "PARENT2",
child: "CHILD",
});
expect(dataStrategy.mock.calls[1][0].matches).toEqual([
expect.objectContaining({
route: expect.objectContaining({
id: "parent",
}),
}),
expect.objectContaining({
route: expect.objectContaining({
id: "child",
}),
}),
]);

let C = await t.navigate("/parent");
await C.loaders.parent.resolve("PARENT3");
expect(C.loaders.parent.stub).toHaveBeenCalled();
expect(t.router.state.loaderData).toEqual({
parent: "PARENT3",
});
expect(dataStrategy.mock.calls[2][0].matches).toEqual([
expect.objectContaining({
route: expect.objectContaining({
id: "parent",
}),
}),
]);

expect(dataStrategy).toHaveBeenCalledTimes(3);
});
});

describe("actions", () => {
Expand Down
11 changes: 8 additions & 3 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1927,8 +1927,13 @@ export function createRouter(init: RouterInit): Router {

pendingNavigationLoadId = ++incrementingLoadId;

// Short circuit if we have no loaders to run
if (matchesToLoad.length === 0 && revalidatingFetchers.length === 0) {
// Short circuit if we have no loaders to run, unless there's a custom dataStrategy
// since they may have different revalidation rules (i.e., single fetch)
if (
!init.dataStrategy &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes issues with our "don't revalidate on action 4xx/5xx responses" logic since both scenarios result in a dataStrategy with all matches having shouldLoad: false. We may need to expose some more info to dataStrategy (or potentially just let dataStrategy handle all of the getMatchesToLoad logic)

matchesToLoad.length === 0 &&
revalidatingFetchers.length === 0
) {
let updatedFetchers = markFetchRedirectsDone();
completeNavigation(
location,
Expand Down Expand Up @@ -3783,7 +3788,7 @@ export function createStaticHandler(
);

// Short circuit if we have no loaders to run (query())
if (matchesToLoad.length === 0) {
if (!dataStrategy && matchesToLoad.length === 0) {
return {
matches,
// Add a null for all matched routes for proper revalidation on the client
Expand Down
Loading