-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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 calling shouldRevalidate on interrupted initial load fetchers #10623
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@remix-run/router": patch | ||
--- | ||
|
||
Avoid calling `shouldRevalidate` for fetchers that have not yet completed a data load |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1481,6 +1481,7 @@ export function createRouter(init: RouterInit): Router { | |
cancelledDeferredRoutes, | ||
cancelledFetcherLoads, | ||
fetchLoadMatches, | ||
fetchRedirectIds, | ||
routesToUse, | ||
basename, | ||
pendingActionData, | ||
|
@@ -1539,6 +1540,9 @@ export function createRouter(init: RouterInit): Router { | |
|
||
pendingNavigationLoadId = ++incrementingLoadId; | ||
revalidatingFetchers.forEach((rf) => { | ||
if (fetchControllers.has(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 | ||
|
@@ -1806,6 +1810,7 @@ export function createRouter(init: RouterInit): Router { | |
cancelledDeferredRoutes, | ||
cancelledFetcherLoads, | ||
fetchLoadMatches, | ||
fetchRedirectIds, | ||
routesToUse, | ||
basename, | ||
{ [match.route.id]: actionResult.data }, | ||
|
@@ -1825,6 +1830,9 @@ export function createRouter(init: RouterInit): Router { | |
existingFetcher ? existingFetcher.data : undefined | ||
); | ||
state.fetchers.set(staleKey, revalidatingFetcher); | ||
if (fetchControllers.has(staleKey)) { | ||
abortFetcher(staleKey); | ||
} | ||
if (rf.controller) { | ||
fetchControllers.set(staleKey, rf.controller); | ||
} | ||
|
@@ -3276,6 +3284,7 @@ function getMatchesToLoad( | |
cancelledDeferredRoutes: string[], | ||
cancelledFetcherLoads: string[], | ||
fetchLoadMatches: Map<string, FetchLoadMatch>, | ||
fetchRedirectIds: Set<string>, | ||
routesToUse: AgnosticDataRouteObject[], | ||
basename: string | undefined, | ||
pendingActionData?: RouteData, | ||
|
@@ -3361,34 +3370,38 @@ function getMatchesToLoad( | |
return; | ||
} | ||
|
||
// Revalidating fetchers are decoupled from the route matches since they | ||
// load from a static href. They only set `defaultShouldRevalidate` on | ||
// explicit revalidation due to submission, useRevalidator, or X-Remix-Revalidate | ||
// | ||
// They automatically revalidate without even calling shouldRevalidate if: | ||
// - They were cancelled | ||
// - They're in the middle of their first load and therefore this is still | ||
// an initial load and not a revalidation | ||
// | ||
// If neither of those is true, then they _always_ check shouldRevalidate | ||
let fetcher = state.fetchers.get(key); | ||
let isPerformingInitialLoad = | ||
fetcher && | ||
fetcher.state !== "idle" && | ||
fetcher.data === undefined && | ||
// If a fetcher.load redirected then it'll be "loading" without any data | ||
// so ensure we're not processing the redirect from this fetcher | ||
!fetchRedirectIds.has(key); | ||
Comment on lines
+3384
to
+3390
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the net new condition, otherwise this code just combines the |
||
let fetcherMatch = getTargetMatch(fetcherMatches, f.path); | ||
|
||
if (cancelledFetcherLoads.includes(key)) { | ||
revalidatingFetchers.push({ | ||
key, | ||
routeId: f.routeId, | ||
path: f.path, | ||
matches: fetcherMatches, | ||
match: fetcherMatch, | ||
controller: new AbortController(), | ||
let shouldRevalidate = | ||
cancelledFetcherLoads.includes(key) || | ||
isPerformingInitialLoad || | ||
shouldRevalidateLoader(fetcherMatch, { | ||
currentUrl, | ||
currentParams: state.matches[state.matches.length - 1].params, | ||
nextUrl, | ||
nextParams: matches[matches.length - 1].params, | ||
...submission, | ||
actionResult, | ||
defaultShouldRevalidate: isRevalidationRequired, | ||
}); | ||
return; | ||
} | ||
|
||
// Revalidating fetchers are decoupled from the route matches since they | ||
// hit a static href, so they _always_ check shouldRevalidate and the | ||
// default is strictly if a revalidation is explicitly required (action | ||
// submissions, useRevalidator, X-Remix-Revalidate). | ||
let shouldRevalidate = shouldRevalidateLoader(fetcherMatch, { | ||
currentUrl, | ||
currentParams: state.matches[state.matches.length - 1].params, | ||
nextUrl, | ||
nextParams: matches[matches.length - 1].params, | ||
...submission, | ||
actionResult, | ||
// Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate | ||
defaultShouldRevalidate: isRevalidationRequired, | ||
}); | ||
if (shouldRevalidate) { | ||
revalidatingFetchers.push({ | ||
key, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we're about to reload/revalidate a
fetcher.load
call, ensure we abort any in-progress loads before we overwrite them with a new controller