Skip to content

Commit 75d96f5

Browse files
committed
Cleanups
1 parent 64e3eba commit 75d96f5

File tree

2 files changed

+126
-164
lines changed

2 files changed

+126
-164
lines changed

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

Lines changed: 118 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,28 +1958,27 @@ export function createRouter(init: RouterInit): Router {
19581958
}
19591959

19601960
let routesToUse = inFlightDataRoutes || dataRoutes;
1961-
let { navigationMatches: dsMatches, revalidatingFetchers } =
1962-
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-
);
1961+
let { dsMatches, revalidatingFetchers } = getMatchesToLoad(
1962+
request,
1963+
scopedContext,
1964+
mapRouteProperties,
1965+
manifest,
1966+
init.history,
1967+
state,
1968+
matches,
1969+
activeSubmission,
1970+
location,
1971+
initialHydration ? [] : hydrationRouteProperties,
1972+
initialHydration === true,
1973+
isRevalidationRequired,
1974+
cancelledFetcherLoads,
1975+
fetchersQueuedForDeletion,
1976+
fetchLoadMatches,
1977+
fetchRedirectIds,
1978+
routesToUse,
1979+
basename,
1980+
pendingActionResult
1981+
);
19831982

19841983
pendingNavigationLoadId = ++incrementingLoadId;
19851984

@@ -2405,28 +2404,27 @@ export function createRouter(init: RouterInit): Router {
24052404
let loadFetcher = getLoadingFetcher(submission, actionResult.data);
24062405
state.fetchers.set(key, loadFetcher);
24072406

2408-
let { navigationMatches: dsMatches, revalidatingFetchers } =
2409-
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-
);
2407+
let { dsMatches, revalidatingFetchers } = getMatchesToLoad(
2408+
revalidationRequest,
2409+
scopedContext,
2410+
mapRouteProperties,
2411+
manifest,
2412+
init.history,
2413+
state,
2414+
matches,
2415+
submission,
2416+
nextLocation,
2417+
hydrationRouteProperties,
2418+
false,
2419+
isRevalidationRequired,
2420+
cancelledFetcherLoads,
2421+
fetchersQueuedForDeletion,
2422+
fetchLoadMatches,
2423+
fetchRedirectIds,
2424+
routesToUse,
2425+
basename,
2426+
[match.route.id, actionResult]
2427+
);
24302428

24312429
// Put all revalidating fetchers into the loading state, except for the
24322430
// current fetcher which we want to keep in it's current loading state which
@@ -4568,7 +4566,7 @@ function getMatchesToLoad(
45684566
basename: string | undefined,
45694567
pendingActionResult?: PendingActionResult
45704568
): {
4571-
navigationMatches: DataStrategyMatch[];
4569+
dsMatches: DataStrategyMatch[];
45724570
revalidatingFetchers: RevalidatingFetcher[];
45734571
} {
45744572
let actionResult = pendingActionResult
@@ -4604,22 +4602,17 @@ function getMatchesToLoad(
46044602
: undefined;
46054603
let shouldSkipRevalidation = actionStatus && actionStatus >= 400;
46064604

4607-
let baseShouldRevalidateArgs: Omit<
4608-
ShouldRevalidateFunctionArgs,
4609-
"defaultShouldRevalidate"
4610-
> | null = initialHydration
4611-
? null
4612-
: {
4613-
currentUrl,
4614-
currentParams: state.matches[0]?.params || {},
4615-
nextUrl,
4616-
nextParams: matches[0].params,
4617-
...submission,
4618-
actionResult,
4619-
actionStatus,
4620-
};
4605+
let baseShouldRevalidateArgs = {
4606+
currentUrl,
4607+
currentParams: state.matches[0]?.params || {},
4608+
nextUrl,
4609+
nextParams: matches[0].params,
4610+
...submission,
4611+
actionResult,
4612+
actionStatus,
4613+
};
46214614

4622-
let navigationMatches: DataStrategyMatch[] = matches.map((match, index) => {
4615+
let dsMatches: DataStrategyMatch[] = matches.map((match, index) => {
46234616
let { route } = match;
46244617

46254618
// For these cases we don't let the user have control via shouldRevalidate
@@ -4672,28 +4665,11 @@ function getMatchesToLoad(
46724665
// Search params affect all loaders
46734666
currentUrl.search !== nextUrl.search ||
46744667
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 = {
4668+
let shouldRevalidateArgs = {
46814669
...baseShouldRevalidateArgs,
46824670
defaultShouldRevalidate,
46834671
};
46844672
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-
46974673
return getDataStrategyMatch(
46984674
mapRouteProperties,
46994675
manifest,
@@ -4702,8 +4678,7 @@ function getMatchesToLoad(
47024678
lazyRoutePropertiesToSkip,
47034679
scopedContext,
47044680
shouldLoad,
4705-
shouldRevalidateArgs,
4706-
shouldCallHandler
4681+
shouldRevalidateArgs
47074682
);
47084683
});
47094684

@@ -4760,26 +4735,20 @@ function getMatchesToLoad(
47604735
fetchController.signal
47614736
);
47624737

4738+
let fetcherDsMatches: DataStrategyMatch[] | null = null;
4739+
47634740
if (cancelledFetcherLoads.has(key)) {
47644741
// Always mark for revalidation if the fetcher was cancelled
47654742
cancelledFetcherLoads.delete(key);
4766-
revalidatingFetchers.push({
4767-
key,
4768-
routeId: f.routeId,
4769-
path: f.path,
4770-
matches: getTargetedDataStrategyMatches(
4771-
mapRouteProperties,
4772-
manifest,
4773-
fetchRequest,
4774-
fetcherMatches,
4775-
fetcherMatch,
4776-
lazyRoutePropertiesToSkip,
4777-
scopedContext
4778-
),
4779-
match: fetcherMatch,
4780-
request: fetchRequest,
4781-
controller: fetchController,
4782-
});
4743+
fetcherDsMatches = getTargetedDataStrategyMatches(
4744+
mapRouteProperties,
4745+
manifest,
4746+
fetchRequest,
4747+
fetcherMatches,
4748+
fetcherMatch,
4749+
lazyRoutePropertiesToSkip,
4750+
scopedContext
4751+
);
47834752
} else if (
47844753
fetcher &&
47854754
fetcher.state !== "idle" &&
@@ -4789,68 +4758,53 @@ function getMatchesToLoad(
47894758
// If the fetcher hasn't ever completed loading yet, then this isn't a
47904759
// revalidation, it would just be a brand new load if an explicit
47914760
// revalidation is required
4792-
revalidatingFetchers.push({
4793-
key,
4794-
routeId: f.routeId,
4795-
path: f.path,
4796-
matches: getTargetedDataStrategyMatches(
4797-
mapRouteProperties,
4798-
manifest,
4799-
fetchRequest,
4800-
fetcherMatches,
4801-
fetcherMatch,
4802-
lazyRoutePropertiesToSkip,
4803-
scopedContext
4804-
),
4805-
match: fetcherMatch,
4806-
request: fetchRequest,
4807-
controller: fetchController,
4808-
});
4761+
fetcherDsMatches = getTargetedDataStrategyMatches(
4762+
mapRouteProperties,
4763+
manifest,
4764+
fetchRequest,
4765+
fetcherMatches,
4766+
fetcherMatch,
4767+
lazyRoutePropertiesToSkip,
4768+
scopedContext
4769+
);
48094770
}
48104771
} else {
48114772
// Otherwise fall back on any user-defined shouldRevalidate, defaulting
48124773
// to explicit revalidations only
4813-
let defaultShouldRevalidate = shouldSkipRevalidation
4814-
? false
4815-
: isRevalidationRequired;
4816-
// Shouldn't run during initialHydration so this should always be defined
4817-
invariant(
4818-
baseShouldRevalidateArgs,
4819-
"Expected shouldRevalidateArgs to be defined for fetcher"
4820-
);
48214774
let shouldRevalidateArgs: ShouldRevalidateFunctionArgs = {
48224775
...baseShouldRevalidateArgs,
4823-
defaultShouldRevalidate,
4776+
defaultShouldRevalidate: shouldSkipRevalidation
4777+
? false
4778+
: isRevalidationRequired,
48244779
};
4825-
let shouldLoad = shouldRevalidateLoader(
4826-
fetcherMatch,
4827-
shouldRevalidateArgs
4828-
);
4829-
4830-
if (shouldLoad) {
4831-
revalidatingFetchers.push({
4832-
key,
4833-
routeId: f.routeId,
4834-
path: f.path,
4835-
matches: getTargetedDataStrategyMatches(
4836-
mapRouteProperties,
4837-
manifest,
4838-
fetchRequest,
4839-
fetcherMatches,
4840-
fetcherMatch,
4841-
lazyRoutePropertiesToSkip,
4842-
scopedContext,
4843-
shouldRevalidateArgs
4844-
),
4845-
match: fetcherMatch,
4846-
request: fetchRequest,
4847-
controller: fetchController,
4848-
});
4780+
if (shouldRevalidateLoader(fetcherMatch, shouldRevalidateArgs)) {
4781+
fetcherDsMatches = getTargetedDataStrategyMatches(
4782+
mapRouteProperties,
4783+
manifest,
4784+
fetchRequest,
4785+
fetcherMatches,
4786+
fetcherMatch,
4787+
lazyRoutePropertiesToSkip,
4788+
scopedContext,
4789+
shouldRevalidateArgs
4790+
);
48494791
}
48504792
}
4793+
4794+
if (fetcherDsMatches) {
4795+
revalidatingFetchers.push({
4796+
key,
4797+
routeId: f.routeId,
4798+
path: f.path,
4799+
matches: fetcherDsMatches,
4800+
match: fetcherMatch,
4801+
request: fetchRequest,
4802+
controller: fetchController,
4803+
});
4804+
}
48514805
});
48524806

4853-
return { navigationMatches, revalidatingFetchers };
4807+
return { dsMatches, revalidatingFetchers };
48544808
}
48554809

48564810
function shouldLoadRouteOnHydration(
@@ -5502,9 +5456,7 @@ function getDataStrategyMatch(
55025456
lazyRoutePropertiesToSkip: string[],
55035457
scopedContext: unknown,
55045458
shouldLoad: boolean,
5505-
unstable_shouldRevalidateArgs: DataStrategyMatch["unstable_shouldRevalidateArgs"] = null,
5506-
unstable_shouldCallHandler: DataStrategyMatch["unstable_shouldCallHandler"] = () =>
5507-
shouldLoad
5459+
unstable_shouldRevalidateArgs: DataStrategyMatch["unstable_shouldRevalidateArgs"] = null
55085460
): DataStrategyMatch {
55095461
// The hope here is to avoid a breaking change to the resolve behavior.
55105462
// Opt-ing into the `unstable_shouldCallHandler` API changes some nuanced behavior
@@ -5521,13 +5473,23 @@ function getDataStrategyMatch(
55215473

55225474
return {
55235475
...match,
5476+
_lazyPromises,
55245477
shouldLoad,
55255478
unstable_shouldRevalidateArgs,
55265479
unstable_shouldCallHandler(defaultShouldRevalidate) {
55275480
isUsingNewApi = true;
5528-
return unstable_shouldCallHandler(defaultShouldRevalidate);
5481+
if (!unstable_shouldRevalidateArgs) {
5482+
return shouldLoad;
5483+
}
5484+
5485+
if (typeof defaultShouldRevalidate === "boolean") {
5486+
return shouldRevalidateLoader(match, {
5487+
...unstable_shouldRevalidateArgs,
5488+
defaultShouldRevalidate,
5489+
});
5490+
}
5491+
return shouldRevalidateLoader(match, unstable_shouldRevalidateArgs);
55295492
},
5530-
_lazyPromises,
55315493
resolve(handlerOverride) {
55325494
if (
55335495
isUsingNewApi ||
@@ -5558,7 +5520,7 @@ function getTargetedDataStrategyMatches(
55585520
targetMatch: AgnosticDataRouteMatch,
55595521
lazyRoutePropertiesToSkip: string[],
55605522
scopedContext: unknown,
5561-
unstable_shouldRevalidateArgs: DataStrategyMatch["unstable_shouldRevalidateArgs"] = null
5523+
shouldRevalidateArgs: DataStrategyMatch["unstable_shouldRevalidateArgs"] = null
55625524
): DataStrategyMatch[] {
55635525
return matches.map((match) => {
55645526
if (match.route.id !== targetMatch.route.id) {
@@ -5567,7 +5529,7 @@ function getTargetedDataStrategyMatches(
55675529
return {
55685530
...match,
55695531
shouldLoad: false,
5570-
unstable_shouldRevalidateArgs,
5532+
unstable_shouldRevalidateArgs: shouldRevalidateArgs,
55715533
unstable_shouldCallHandler: () => false,
55725534
_lazyPromises: getDataStrategyMatchLazyPromises(
55735535
mapRouteProperties,
@@ -5588,7 +5550,7 @@ function getTargetedDataStrategyMatches(
55885550
lazyRoutePropertiesToSkip,
55895551
scopedContext,
55905552
true,
5591-
unstable_shouldRevalidateArgs
5553+
shouldRevalidateArgs
55925554
);
55935555
});
55945556
}

0 commit comments

Comments
 (0)