Skip to content

Commit c3d0c1b

Browse files
committed
Convert static handler to use getDataStrategyMatch
1 parent 1332dc7 commit c3d0c1b

File tree

1 file changed

+24
-63
lines changed

1 file changed

+24
-63
lines changed

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

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4114,33 +4114,23 @@ export function createStaticHandler(
41144114
: undefined;
41154115

41164116
if (maxIdx != null && index > maxIdx) {
4117-
return {
4118-
...match,
4119-
shouldLoad: false,
4120-
shouldCallHandler: () => false,
4117+
return getDataStrategyMatch(
4118+
request,
4119+
match,
41214120
_lazyPromise,
4122-
resolve: () => Promise.resolve({ type: "data", result: undefined }),
4123-
};
4121+
requestContext,
4122+
false
4123+
);
41244124
}
41254125

4126-
let shouldLoad =
4127-
(match.route.loader || match.route.lazy) != null &&
4128-
(!filterMatchesToLoad || filterMatchesToLoad(match));
4129-
4130-
return {
4131-
...match,
4132-
shouldLoad,
4133-
shouldCallHandler: () => shouldLoad,
4126+
return getDataStrategyMatch(
4127+
request,
4128+
match,
41344129
_lazyPromise,
4135-
resolve: (handlerOverride) =>
4136-
callLoaderOrAction(
4137-
request,
4138-
match,
4139-
_lazyPromise,
4140-
handlerOverride,
4141-
requestContext
4142-
),
4143-
};
4130+
requestContext,
4131+
(match.route.loader || match.route.lazy) != null &&
4132+
(!filterMatchesToLoad || filterMatchesToLoad(match))
4133+
);
41444134
});
41454135
}
41464136

@@ -5239,7 +5229,7 @@ function getDataStrategyMatch(
52395229
match: DataRouteMatch,
52405230
_lazyPromise: Promise<void> | undefined,
52415231
scopedContext: unknown,
5242-
shouldLoad = false,
5232+
shouldLoad: boolean,
52435233
_shouldCallHandler?: DataStrategyMatch["shouldCallHandler"]
52445234
): DataStrategyMatch {
52455235
let isUsingNewApi = false;
@@ -5280,9 +5270,7 @@ function getTargetedDataStrategyMatches(
52805270
targetMatch: AgnosticDataRouteMatch,
52815271
scopedContext: unknown,
52825272
mapRouteProperties: MapRoutePropertiesFunction,
5283-
manifest: RouteManifest,
5284-
shouldLoad = true,
5285-
shouldCallHandler = (_: boolean | undefined) => shouldLoad
5273+
manifest: RouteManifest
52865274
): DataStrategyMatch[] {
52875275
return matches.map((match, index) => {
52885276
// Kick off route.lazy loads
@@ -5291,51 +5279,24 @@ function getTargetedDataStrategyMatches(
52915279
: undefined;
52925280

52935281
if (match.route.id !== targetMatch.route.id) {
5282+
// We don't use getDataStrategyMatch here because these are for actions/fetchers
5283+
// where we should _never_ call the handler for any matches other than the target
52945284
return {
52955285
...match,
52965286
shouldLoad: false,
52975287
shouldCallHandler: () => false,
5298-
resolve: () => Promise.resolve({ type: "data", result: undefined }),
52995288
_lazyPromise,
5289+
resolve: () => Promise.resolve({ type: "data", result: undefined }),
53005290
};
53015291
}
53025292

5303-
let isUsingNewApi = false;
5304-
return {
5305-
...match,
5306-
shouldLoad,
5307-
shouldCallHandler: (defaultShouldRevalidate) => {
5308-
isUsingNewApi = true;
5309-
return shouldCallHandler(defaultShouldRevalidate);
5310-
},
5293+
return getDataStrategyMatch(
5294+
request,
5295+
match,
53115296
_lazyPromise,
5312-
resolve: (handlerOverride) => {
5313-
// If the route has `shouldLoad=true`, call the handler
5314-
// Otherwise, call only if:
5315-
// - They're using the new `shouldCallHandler` API in which case resolve
5316-
// changes behavior slightly and is a direct 1-1 to call the handler
5317-
// - They passed a `handlerOverride` for a GET request on a route with a
5318-
// `loader` (or unresolved `lazy`), in which case they're taking control
5319-
// over handler execution. This mimics logic we had in the original
5320-
// implementation and is kept the same to avoid a breaking change
5321-
if (
5322-
shouldLoad ||
5323-
isUsingNewApi ||
5324-
(handlerOverride &&
5325-
request.method === "GET" &&
5326-
(match.route.lazy || match.route.loader))
5327-
) {
5328-
return callLoaderOrAction(
5329-
request,
5330-
match,
5331-
_lazyPromise,
5332-
handlerOverride,
5333-
scopedContext
5334-
);
5335-
}
5336-
return Promise.resolve({ type: ResultType.data, result: undefined });
5337-
},
5338-
};
5297+
scopedContext,
5298+
true
5299+
);
53395300
});
53405301
}
53415302

0 commit comments

Comments
 (0)