Skip to content

Commit b608685

Browse files
committed
Move lazy loading into getDataStrategyMatch
1 parent c3d0c1b commit b608685

File tree

1 file changed

+64
-52
lines changed

1 file changed

+64
-52
lines changed

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

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,12 +1785,12 @@ export function createRouter(init: RouterInit): Router {
17851785
};
17861786
} else {
17871787
let dsMatches = getTargetedDataStrategyMatches(
1788+
mapRouteProperties,
1789+
manifest,
17881790
request,
17891791
matches,
17901792
actionMatch,
1791-
scopedContext,
1792-
mapRouteProperties,
1793-
manifest
1793+
scopedContext
17941794
);
17951795
let results = await callDataStrategy(
17961796
request,
@@ -2315,12 +2315,12 @@ export function createRouter(init: RouterInit): Router {
23152315

23162316
let originatingLoadId = incrementingLoadId;
23172317
let fetchMatches = getTargetedDataStrategyMatches(
2318+
mapRouteProperties,
2319+
manifest,
23182320
fetchRequest,
23192321
requestMatches,
23202322
match,
2321-
scopedContext,
2322-
mapRouteProperties,
2323-
manifest
2323+
scopedContext
23242324
);
23252325
let actionResults = await callDataStrategy(
23262326
fetchRequest,
@@ -2606,12 +2606,12 @@ export function createRouter(init: RouterInit): Router {
26062606

26072607
let originatingLoadId = incrementingLoadId;
26082608
let dsMatches = getTargetedDataStrategyMatches(
2609+
mapRouteProperties,
2610+
manifest,
26092611
fetchRequest,
26102612
matches,
26112613
match,
2612-
scopedContext,
2613-
mapRouteProperties,
2614-
manifest
2614+
scopedContext
26152615
);
26162616
let results = await callDataStrategy(
26172617
fetchRequest,
@@ -3899,12 +3899,12 @@ export function createStaticHandler(
38993899
};
39003900
} else {
39013901
let dsMatches = getTargetedDataStrategyMatches(
3902+
mapRouteProperties,
3903+
manifest,
39023904
request,
39033905
matches,
39043906
actionMatch,
3905-
requestContext,
3906-
mapRouteProperties,
3907-
manifest
3907+
requestContext
39083908
);
39093909

39103910
let results = await callDataStrategy(
@@ -4093,12 +4093,12 @@ export function createStaticHandler(
40934093
let dsMatches: DataStrategyMatch[];
40944094
if (routeMatch) {
40954095
dsMatches = getTargetedDataStrategyMatches(
4096+
mapRouteProperties,
4097+
manifest,
40964098
request,
40974099
matches,
40984100
routeMatch,
4099-
requestContext,
4100-
mapRouteProperties,
4101-
manifest
4101+
requestContext
41024102
);
41034103
} else {
41044104
let maxIdx =
@@ -4108,25 +4108,22 @@ export function createStaticHandler(
41084108
: undefined;
41094109

41104110
dsMatches = matches.map((match, index) => {
4111-
// Kick off route.lazy loads
4112-
let _lazyPromise = match.route.lazy
4113-
? loadLazyRouteModule(match.route, mapRouteProperties, manifest)
4114-
: undefined;
4115-
41164111
if (maxIdx != null && index > maxIdx) {
41174112
return getDataStrategyMatch(
4113+
mapRouteProperties,
4114+
manifest,
41184115
request,
41194116
match,
4120-
_lazyPromise,
41214117
requestContext,
41224118
false
41234119
);
41244120
}
41254121

41264122
return getDataStrategyMatch(
4123+
mapRouteProperties,
4124+
manifest,
41274125
request,
41284126
match,
4129-
_lazyPromise,
41304127
requestContext,
41314128
(match.route.loader || match.route.lazy) != null &&
41324129
(!filterMatchesToLoad || filterMatchesToLoad(match))
@@ -4576,48 +4573,52 @@ function getMatchesToLoad(
45764573

45774574
let navigationMatches: DataStrategyMatch[] = matches.map((match, index) => {
45784575
let { route } = match;
4579-
let _lazyPromise: Promise<void> | undefined;
45804576

45814577
if (maxIdx != null && index > maxIdx) {
45824578
// Don't call loaders below the boundary
45834579
return getDataStrategyMatch(
4580+
mapRouteProperties,
4581+
manifest,
45844582
request,
45854583
match,
4586-
undefined,
45874584
scopedContext,
45884585
false
45894586
);
45904587
} else if (route.lazy) {
45914588
// We haven't loaded this route yet so we don't know if it's got a loader!
45924589
return getDataStrategyMatch(
4590+
mapRouteProperties,
4591+
manifest,
45934592
request,
45944593
match,
4595-
loadLazyRouteModule(match.route, mapRouteProperties, manifest),
45964594
scopedContext,
45974595
true
45984596
);
45994597
} else if (route.loader == null) {
46004598
return getDataStrategyMatch(
4599+
mapRouteProperties,
4600+
manifest,
46014601
request,
46024602
match,
4603-
undefined,
46044603
scopedContext,
46054604
false
46064605
);
46074606
} else if (initialHydration) {
46084607
return getDataStrategyMatch(
4608+
mapRouteProperties,
4609+
manifest,
46094610
request,
46104611
match,
4611-
undefined,
46124612
scopedContext,
46134613
shouldLoadRouteOnHydration(route, state.loaderData, state.errors)
46144614
);
46154615
} else if (isNewLoader(state.loaderData, state.matches[index], match)) {
46164616
// Always call the loader on new route instances
46174617
return getDataStrategyMatch(
4618+
mapRouteProperties,
4619+
manifest,
46184620
request,
46194621
match,
4620-
undefined,
46214622
scopedContext,
46224623
true
46234624
);
@@ -4651,9 +4652,10 @@ function getMatchesToLoad(
46514652
});
46524653

46534654
return getDataStrategyMatch(
4655+
mapRouteProperties,
4656+
manifest,
46544657
request,
46554658
match,
4656-
_lazyPromise,
46574659
scopedContext,
46584660
shouldLoad,
46594661
(defaultOverride) =>
@@ -4729,12 +4731,12 @@ function getMatchesToLoad(
47294731
routeId: f.routeId,
47304732
path: f.path,
47314733
matches: getTargetedDataStrategyMatches(
4734+
mapRouteProperties,
4735+
manifest,
47324736
fetchRequest,
47334737
fetcherMatches,
47344738
fetcherMatch,
4735-
scopedContext,
4736-
mapRouteProperties,
4737-
manifest
4739+
scopedContext
47384740
),
47394741
match: fetcherMatch,
47404742
request: fetchRequest,
@@ -4754,12 +4756,12 @@ function getMatchesToLoad(
47544756
routeId: f.routeId,
47554757
path: f.path,
47564758
matches: getTargetedDataStrategyMatches(
4759+
mapRouteProperties,
4760+
manifest,
47574761
fetchRequest,
47584762
fetcherMatches,
47594763
fetcherMatch,
4760-
scopedContext,
4761-
mapRouteProperties,
4762-
manifest
4764+
scopedContext
47634765
),
47644766
match: fetcherMatch,
47654767
request: fetchRequest,
@@ -4796,12 +4798,12 @@ function getMatchesToLoad(
47964798
routeId: f.routeId,
47974799
path: f.path,
47984800
matches: getTargetedDataStrategyMatches(
4801+
mapRouteProperties,
4802+
manifest,
47994803
fetchRequest,
48004804
fetcherMatches,
48014805
fetcherMatch,
4802-
scopedContext,
4803-
mapRouteProperties,
4804-
manifest
4806+
scopedContext
48054807
),
48064808
match: fetcherMatch,
48074809
request: fetchRequest,
@@ -5225,22 +5227,32 @@ async function callRouteMiddleware(
52255227
}
52265228

52275229
function getDataStrategyMatch(
5230+
mapRouteProperties: MapRoutePropertiesFunction,
5231+
manifest: RouteManifest,
52285232
request: Request,
52295233
match: DataRouteMatch,
5230-
_lazyPromise: Promise<void> | undefined,
52315234
scopedContext: unknown,
52325235
shouldLoad: boolean,
5233-
_shouldCallHandler?: DataStrategyMatch["shouldCallHandler"]
5236+
shouldCallHandler: DataStrategyMatch["shouldCallHandler"] = () => shouldLoad
52345237
): DataStrategyMatch {
5238+
// Kick off route.lazy loads
5239+
let _lazyPromise = match.route.lazy
5240+
? loadLazyRouteModule(match.route, mapRouteProperties, manifest)
5241+
: undefined;
5242+
// Prevent unhandled rejection errors - handled inside of `callLoadOrAction`
5243+
_lazyPromise?.catch(() => {});
5244+
5245+
// The hope here is to avoid a breaking change to the resolve behavior.
5246+
// Opt-ing into the `shouldCallHandler` API changes some nuanced behavior
5247+
// around when resolve calls through to the handler
52355248
let isUsingNewApi = false;
5249+
52365250
return {
52375251
...match,
52385252
shouldLoad,
52395253
shouldCallHandler(defaultShouldRevalidate) {
52405254
isUsingNewApi = true;
5241-
return typeof _shouldCallHandler === "function"
5242-
? _shouldCallHandler(defaultShouldRevalidate)
5243-
: shouldLoad;
5255+
return shouldCallHandler(defaultShouldRevalidate);
52445256
},
52455257
_lazyPromise,
52465258
resolve(handlerOverride) {
@@ -5265,20 +5277,19 @@ function getDataStrategyMatch(
52655277
}
52665278

52675279
function getTargetedDataStrategyMatches(
5280+
mapRouteProperties: MapRoutePropertiesFunction,
5281+
manifest: RouteManifest,
52685282
request: Request,
52695283
matches: AgnosticDataRouteMatch[],
52705284
targetMatch: AgnosticDataRouteMatch,
5271-
scopedContext: unknown,
5272-
mapRouteProperties: MapRoutePropertiesFunction,
5273-
manifest: RouteManifest
5285+
scopedContext: unknown
52745286
): DataStrategyMatch[] {
52755287
return matches.map((match, index) => {
5276-
// Kick off route.lazy loads
5277-
let _lazyPromise = match.route.lazy
5278-
? loadLazyRouteModule(match.route, mapRouteProperties, manifest)
5279-
: undefined;
5280-
52815288
if (match.route.id !== targetMatch.route.id) {
5289+
let _lazyPromise = match.route.lazy
5290+
? loadLazyRouteModule(match.route, mapRouteProperties, manifest)
5291+
: undefined;
5292+
52825293
// We don't use getDataStrategyMatch here because these are for actions/fetchers
52835294
// where we should _never_ call the handler for any matches other than the target
52845295
return {
@@ -5291,9 +5302,10 @@ function getTargetedDataStrategyMatches(
52915302
}
52925303

52935304
return getDataStrategyMatch(
5305+
mapRouteProperties,
5306+
manifest,
52945307
request,
52955308
match,
5296-
_lazyPromise,
52975309
scopedContext,
52985310
true
52995311
);

0 commit comments

Comments
 (0)