Skip to content

Commit

Permalink
Avoid unnecessary console error on fetcher abort due to back-to-back …
Browse files Browse the repository at this point in the history
…revalidation calls (#12050)
  • Loading branch information
brophdawg11 committed Oct 1, 2024
1 parent 12c37f0 commit d5aaee4
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1968,9 +1968,7 @@ export function createRouter(init: RouterInit): Router {
}

revalidatingFetchers.forEach((rf) => {
if (fetchControllers.has(rf.key)) {
abortFetcher(rf.key);
}
abortFetcher(rf.key);
if (rf.controller) {
// Fetchers use an independent AbortController so that aborting a fetcher
// (via deleteFetcher) does not abort the triggering navigation that
Expand Down Expand Up @@ -2011,6 +2009,7 @@ export function createRouter(init: RouterInit): Router {
abortPendingFetchRevalidations
);
}

revalidatingFetchers.forEach((rf) => fetchControllers.delete(rf.key));

// If any loaders returned a redirect Response, start a new REPLACE navigation
Expand Down Expand Up @@ -2038,7 +2037,6 @@ export function createRouter(init: RouterInit): Router {
let { loaderData, errors } = processLoaderData(
state,
matches,
matchesToLoad,
loaderResults,
pendingActionResult,
revalidatingFetchers,
Expand Down Expand Up @@ -2107,7 +2105,8 @@ export function createRouter(init: RouterInit): Router {
href: string | null,
opts?: RouterFetchOptions
) {
if (fetchControllers.has(key)) abortFetcher(key);
abortFetcher(key);

let flushSync = (opts && opts.flushSync) === true;

let routesToUse = inFlightDataRoutes || dataRoutes;
Expand Down Expand Up @@ -2370,9 +2369,7 @@ export function createRouter(init: RouterInit): Router {
existingFetcher ? existingFetcher.data : undefined
);
state.fetchers.set(staleKey, revalidatingFetcher);
if (fetchControllers.has(staleKey)) {
abortFetcher(staleKey);
}
abortFetcher(staleKey);
if (rf.controller) {
fetchControllers.set(staleKey, rf.controller);
}
Expand Down Expand Up @@ -2438,7 +2435,6 @@ export function createRouter(init: RouterInit): Router {
let { loaderData, errors } = processLoaderData(
state,
matches,
matchesToLoad,
loaderResults,
undefined,
revalidatingFetchers,
Expand Down Expand Up @@ -2863,8 +2859,8 @@ export function createRouter(init: RouterInit): Router {
fetchLoadMatches.forEach((_, key) => {
if (fetchControllers.has(key)) {
cancelledFetcherLoads.add(key);
abortFetcher(key);
}
abortFetcher(key);
});
}

Expand Down Expand Up @@ -2941,9 +2937,10 @@ export function createRouter(init: RouterInit): Router {

function abortFetcher(key: string) {
let controller = fetchControllers.get(key);
invariant(controller, `Expected fetch controller: ${key}`);
controller.abort();
fetchControllers.delete(key);
if (controller) {
controller.abort();
fetchControllers.delete(key);
}
}

function markFetchersDone(keys: string[]) {
Expand Down Expand Up @@ -5154,7 +5151,6 @@ function processRouteLoaderData(
function processLoaderData(
state: RouterState,
matches: AgnosticDataRouteMatch[],
matchesToLoad: AgnosticDataRouteMatch[],
results: Record<string, DataResult>,
pendingActionResult: PendingActionResult | undefined,
revalidatingFetchers: RevalidatingFetcher[],
Expand Down

0 comments on commit d5aaee4

Please sign in to comment.