Skip to content

Commit ee7d150

Browse files
authored
Lift aborted fetcher short circuit check (#14114)
1 parent 8628a77 commit ee7d150

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

.changeset/famous-laws-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Prevent _"Did not find corresponding fetcher result"_ console error when navigating during a `fetcher.submit` revalidation

packages/react-router/__tests__/router/fetchers-test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,71 @@ describe("fetchers", () => {
19231923
expect(t.router.getFetcher(keyB)?.state).toBe("idle");
19241924
});
19251925
});
1926+
1927+
it("properly ignores aborted action revalidation fetchers when a navigation triggers revalidations", async () => {
1928+
let keyA = "a";
1929+
let keyB = "b";
1930+
let t = initializeTest();
1931+
1932+
// Complete a fetch load
1933+
let A = await t.fetch("/foo", keyA, "root");
1934+
await A.loaders.foo.resolve("FOO");
1935+
expect(t.fetchers[keyA]).toMatchObject({
1936+
state: "idle",
1937+
data: "FOO",
1938+
});
1939+
expect(t.router.state.fetchers.get(keyA)).toBe(undefined);
1940+
1941+
// Submit to trigger fetch revalidation
1942+
let B = await t.fetch("/bar", keyB, "root", {
1943+
formMethod: "post",
1944+
formData: createFormData({}),
1945+
});
1946+
t.shimHelper(B.loaders, "fetch", "loader", "foo");
1947+
await B.actions.bar.resolve("BAR");
1948+
expect(t.fetchers[keyB]).toMatchObject({
1949+
state: "loading",
1950+
data: "BAR",
1951+
});
1952+
expect(t.fetchers[keyA]).toMatchObject({
1953+
state: "loading",
1954+
data: "FOO",
1955+
});
1956+
1957+
// Interrupt revalidation with GEt navigation
1958+
// TODO: This shouldn't actually abort the revalidation but it does currently
1959+
// which then causes the invalid invariant error. This test is to ensure
1960+
// the invariant doesn't throw, but we'll fix the unnecessary revalidation
1961+
// in https://github.com/remix-run/react-router/issues/14115
1962+
let C = await t.navigate("/baz", undefined, ["foo"]);
1963+
expect(B.loaders.foo.signal.aborted).toBe(true);
1964+
expect(t.fetchers[keyA]).toMatchObject({
1965+
state: "loading",
1966+
data: "FOO",
1967+
});
1968+
1969+
// Complete the aborted fetcher revalidation calls
1970+
await B.loaders.root.resolve("NOPE");
1971+
await B.loaders.index.resolve("NOPE");
1972+
await B.loaders.foo.resolve("NOPE");
1973+
1974+
// Complete the navigation
1975+
await C.loaders.root.resolve("ROOT*");
1976+
await C.loaders.baz.resolve("BAZ");
1977+
await C.loaders.foo.resolve("FOO*");
1978+
expect(t.router.state).toMatchObject({
1979+
navigation: IDLE_NAVIGATION,
1980+
location: { pathname: "/baz" },
1981+
loaderData: {
1982+
root: "ROOT*",
1983+
baz: "BAZ",
1984+
},
1985+
});
1986+
expect(t.fetchers[keyA]).toMatchObject({
1987+
state: "idle",
1988+
data: "FOO*",
1989+
});
1990+
});
19261991
});
19271992

19281993
describe("fetcher revalidation", () => {

packages/react-router/lib/router/router.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6244,14 +6244,16 @@ function processLoaderData(
62446244
.filter((f) => !f.matches || f.matches.some((m) => m.shouldLoad))
62456245
.forEach((rf) => {
62466246
let { key, match, controller } = rf;
6247+
if (controller && controller.signal.aborted) {
6248+
// Nothing to do for aborted fetchers
6249+
return;
6250+
}
6251+
62476252
let result = fetcherResults[key];
62486253
invariant(result, "Did not find corresponding fetcher result");
62496254

62506255
// Process fetcher non-redirect errors
6251-
if (controller && controller.signal.aborted) {
6252-
// Nothing to do for aborted fetchers
6253-
return;
6254-
} else if (isErrorResult(result)) {
6256+
if (isErrorResult(result)) {
62556257
let boundaryMatch = findNearestBoundary(state.matches, match?.route.id);
62566258
if (!(errors && errors[boundaryMatch.route.id])) {
62576259
errors = {

0 commit comments

Comments
 (0)