Skip to content
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

Avoid unnecessary console error on fetcher abort due to back-to-back revalidation calls #12050

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/itchy-shoes-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Avoid uneccesary console error on fetcher abort due to back-to-back revalidation calls
24 changes: 10 additions & 14 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1979,9 +1979,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 @@ -2022,6 +2020,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 @@ -2049,7 +2048,6 @@ export function createRouter(init: RouterInit): Router {
let { loaderData, errors } = processLoaderData(
state,
matches,
matchesToLoad,
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 was unused

loaderResults,
pendingActionResult,
revalidatingFetchers,
Expand Down Expand Up @@ -2139,7 +2137,8 @@ export function createRouter(init: RouterInit): Router {
);
}

if (fetchControllers.has(key)) abortFetcher(key);
abortFetcher(key);

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

let routesToUse = inFlightDataRoutes || dataRoutes;
Expand Down Expand Up @@ -2412,9 +2411,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 @@ -2480,7 +2477,6 @@ export function createRouter(init: RouterInit): Router {
let { loaderData, errors } = processLoaderData(
state,
matches,
matchesToLoad,
loaderResults,
undefined,
revalidatingFetchers,
Expand Down Expand Up @@ -2934,8 +2930,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 @@ -3018,9 +3014,10 @@ export function createRouter(init: RouterInit): Router {

function abortFetcher(key: string) {
let controller = fetchControllers.get(key);
invariant(controller, `Expected fetch controller: ${key}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All but 2 uses of abortFetcher were already defensive against this invariant and the 2 that weren't were the bug we're fixing, so now that they all want to be defensive we can just flatten the defensive check into this method.

controller.abort();
fetchControllers.delete(key);
if (controller) {
controller.abort();
fetchControllers.delete(key);
}
}

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