Skip to content

Commit 64e3eba

Browse files
committed
Dry up some data strategy match logic
1 parent b84f4f2 commit 64e3eba

File tree

1 file changed

+104
-122
lines changed

1 file changed

+104
-122
lines changed

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

Lines changed: 104 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,26 +1960,26 @@ export function createRouter(init: RouterInit): Router {
19601960
let routesToUse = inFlightDataRoutes || dataRoutes;
19611961
let { navigationMatches: dsMatches, revalidatingFetchers } =
19621962
getMatchesToLoad(
1963-
request,
1964-
scopedContext,
1965-
mapRouteProperties,
1966-
manifest,
1967-
init.history,
1968-
state,
1969-
matches,
1970-
activeSubmission,
1971-
location,
1972-
initialHydration ? [] : hydrationRouteProperties,
1973-
initialHydration === true,
1974-
isRevalidationRequired,
1975-
cancelledFetcherLoads,
1976-
fetchersQueuedForDeletion,
1977-
fetchLoadMatches,
1978-
fetchRedirectIds,
1979-
routesToUse,
1980-
basename,
1981-
pendingActionResult
1982-
);
1963+
request,
1964+
scopedContext,
1965+
mapRouteProperties,
1966+
manifest,
1967+
init.history,
1968+
state,
1969+
matches,
1970+
activeSubmission,
1971+
location,
1972+
initialHydration ? [] : hydrationRouteProperties,
1973+
initialHydration === true,
1974+
isRevalidationRequired,
1975+
cancelledFetcherLoads,
1976+
fetchersQueuedForDeletion,
1977+
fetchLoadMatches,
1978+
fetchRedirectIds,
1979+
routesToUse,
1980+
basename,
1981+
pendingActionResult
1982+
);
19831983

19841984
pendingNavigationLoadId = ++incrementingLoadId;
19851985

@@ -2407,26 +2407,26 @@ export function createRouter(init: RouterInit): Router {
24072407

24082408
let { navigationMatches: dsMatches, revalidatingFetchers } =
24092409
getMatchesToLoad(
2410-
revalidationRequest,
2411-
scopedContext,
2412-
mapRouteProperties,
2413-
manifest,
2414-
init.history,
2415-
state,
2416-
matches,
2417-
submission,
2418-
nextLocation,
2419-
hydrationRouteProperties,
2420-
false,
2421-
isRevalidationRequired,
2422-
cancelledFetcherLoads,
2423-
fetchersQueuedForDeletion,
2424-
fetchLoadMatches,
2425-
fetchRedirectIds,
2426-
routesToUse,
2427-
basename,
2428-
[match.route.id, actionResult]
2429-
);
2410+
revalidationRequest,
2411+
scopedContext,
2412+
mapRouteProperties,
2413+
manifest,
2414+
init.history,
2415+
state,
2416+
matches,
2417+
submission,
2418+
nextLocation,
2419+
hydrationRouteProperties,
2420+
false,
2421+
isRevalidationRequired,
2422+
cancelledFetcherLoads,
2423+
fetchersQueuedForDeletion,
2424+
fetchLoadMatches,
2425+
fetchRedirectIds,
2426+
routesToUse,
2427+
basename,
2428+
[match.route.id, actionResult]
2429+
);
24302430

24312431
// Put all revalidating fetchers into the loading state, except for the
24322432
// current fetcher which we want to keep in it's current loading state which
@@ -4622,107 +4622,89 @@ function getMatchesToLoad(
46224622
let navigationMatches: DataStrategyMatch[] = matches.map((match, index) => {
46234623
let { route } = match;
46244624

4625+
// For these cases we don't let the user have control via shouldRevalidate
4626+
// and we either force the loader to run or not run
4627+
let forceShouldLoad: boolean | null = null;
4628+
46254629
if (maxIdx != null && index > maxIdx) {
46264630
// Don't call loaders below the boundary
4627-
return getDataStrategyMatch(
4628-
mapRouteProperties,
4629-
manifest,
4630-
request,
4631-
match,
4632-
lazyRoutePropertiesToSkip,
4633-
scopedContext,
4634-
false
4635-
);
4631+
forceShouldLoad = false;
46364632
} else if (route.lazy) {
46374633
// We haven't loaded this route yet so we don't know if it's got a loader!
4638-
return getDataStrategyMatch(
4639-
mapRouteProperties,
4640-
manifest,
4641-
request,
4642-
match,
4643-
lazyRoutePropertiesToSkip,
4644-
scopedContext,
4645-
true
4646-
);
4634+
forceShouldLoad = true;
46474635
} else if (route.loader == null) {
4648-
return getDataStrategyMatch(
4649-
mapRouteProperties,
4650-
manifest,
4651-
request,
4652-
match,
4653-
lazyRoutePropertiesToSkip,
4654-
scopedContext,
4655-
false
4656-
);
4636+
// Nothing to load!
4637+
forceShouldLoad = false;
46574638
} else if (initialHydration) {
4658-
return getDataStrategyMatch(
4659-
mapRouteProperties,
4660-
manifest,
4661-
request,
4662-
match,
4663-
lazyRoutePropertiesToSkip,
4664-
scopedContext,
4665-
shouldLoadRouteOnHydration(route, state.loaderData, state.errors)
4639+
// Only run on hydration if this is a hydrating `clientLoader`
4640+
forceShouldLoad = shouldLoadRouteOnHydration(
4641+
route,
4642+
state.loaderData,
4643+
state.errors
46664644
);
46674645
} else if (isNewLoader(state.loaderData, state.matches[index], match)) {
46684646
// Always call the loader on new route instances
4669-
return getDataStrategyMatch(
4670-
mapRouteProperties,
4671-
manifest,
4672-
request,
4673-
match,
4674-
lazyRoutePropertiesToSkip,
4675-
scopedContext,
4676-
true
4677-
);
4678-
} else {
4679-
// This is the default implementation for when we revalidate. If the route
4680-
// provides it's own implementation, then we give them full control but
4681-
// provide this value so they can leverage it if needed after they check
4682-
// their own specific use cases
4683-
let defaultShouldRevalidate = shouldSkipRevalidation
4684-
? false
4685-
: // Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate
4686-
isRevalidationRequired ||
4687-
currentUrl.pathname + currentUrl.search ===
4688-
nextUrl.pathname + nextUrl.search ||
4689-
// Search params affect all loaders
4690-
currentUrl.search !== nextUrl.search ||
4691-
isNewRouteInstance(state.matches[index], match);
4692-
// Already checked `initialHydration` above so this should always be defined
4693-
invariant(
4694-
baseShouldRevalidateArgs,
4695-
"Expected shouldRevalidateArgs to be defined for route match"
4696-
);
4697-
let shouldRevalidateArgs: ShouldRevalidateFunctionArgs = {
4698-
...baseShouldRevalidateArgs,
4699-
defaultShouldRevalidate,
4700-
};
4701-
let shouldLoad = shouldRevalidateLoader(match, shouldRevalidateArgs);
4702-
let shouldCallHandler: DataStrategyMatch["unstable_shouldCallHandler"] = (
4703-
defaultOverride
4704-
) =>
4705-
shouldRevalidateLoader(match, {
4706-
// We're not in initialHydration here so this will be defined
4707-
...shouldRevalidateArgs!,
4708-
defaultShouldRevalidate:
4709-
typeof defaultOverride === "boolean"
4710-
? defaultOverride
4711-
: defaultShouldRevalidate,
4712-
});
4647+
forceShouldLoad = true;
4648+
}
47134649

4650+
if (forceShouldLoad !== null) {
47144651
return getDataStrategyMatch(
47154652
mapRouteProperties,
47164653
manifest,
47174654
request,
47184655
match,
47194656
lazyRoutePropertiesToSkip,
47204657
scopedContext,
4721-
shouldLoad,
4722-
shouldRevalidateArgs,
4723-
shouldCallHandler
4658+
forceShouldLoad
47244659
);
47254660
}
4661+
4662+
// This is the default implementation for when we revalidate. If the route
4663+
// provides it's own implementation, then we give them full control but
4664+
// provide this value so they can leverage it if needed after they check
4665+
// their own specific use cases
4666+
let defaultShouldRevalidate = shouldSkipRevalidation
4667+
? false
4668+
: // Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate
4669+
isRevalidationRequired ||
4670+
currentUrl.pathname + currentUrl.search ===
4671+
nextUrl.pathname + nextUrl.search ||
4672+
// Search params affect all loaders
4673+
currentUrl.search !== nextUrl.search ||
4674+
isNewRouteInstance(state.matches[index], match);
4675+
// Already checked `initialHydration` above so this should always be defined
4676+
invariant(
4677+
baseShouldRevalidateArgs,
4678+
"Expected shouldRevalidateArgs to be defined for route match"
4679+
);
4680+
let shouldRevalidateArgs: ShouldRevalidateFunctionArgs = {
4681+
...baseShouldRevalidateArgs,
4682+
defaultShouldRevalidate,
4683+
};
4684+
let shouldLoad = shouldRevalidateLoader(match, shouldRevalidateArgs);
4685+
let shouldCallHandler: DataStrategyMatch["unstable_shouldCallHandler"] = (
4686+
defaultOverride
4687+
) =>
4688+
shouldRevalidateLoader(match, {
4689+
// We're not in initialHydration here so this will be defined
4690+
...shouldRevalidateArgs!,
4691+
defaultShouldRevalidate:
4692+
typeof defaultOverride === "boolean"
4693+
? defaultOverride
4694+
: defaultShouldRevalidate,
4695+
});
4696+
4697+
return getDataStrategyMatch(
4698+
mapRouteProperties,
4699+
manifest,
4700+
request,
4701+
match,
4702+
lazyRoutePropertiesToSkip,
4703+
scopedContext,
4704+
shouldLoad,
4705+
shouldRevalidateArgs,
4706+
shouldCallHandler
4707+
);
47264708
});
47274709

47284710
// Pick fetcher.loads that need to be revalidated

0 commit comments

Comments
 (0)