Skip to content

Commit 2071cc4

Browse files
committed
Add middleware future flag to a few spots in the router for library mode
1 parent c0f766f commit 2071cc4

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -830,13 +830,17 @@ export function createRouter(init: RouterInit): Router {
830830
);
831831
let inFlightDataRoutes: AgnosticDataRouteObject[] | undefined;
832832
let basename = init.basename || "/";
833-
let dataStrategyImpl = init.dataStrategy || defaultDataStrategyWithMiddleware;
834-
835833
// Config driven behavior flags
836834
let future: FutureConfig = {
837835
unstable_middleware: false,
838836
...init.future,
839837
};
838+
let dataStrategyImpl =
839+
init.dataStrategy ||
840+
(future.unstable_middleware
841+
? defaultDataStrategyWithMiddleware
842+
: defaultDataStrategy);
843+
840844
// Cleanup function for history
841845
let unlistenHistory: (() => void) | null = null;
842846
// Externally-provided functions to call on all state changes
@@ -3372,7 +3376,8 @@ export function createRouter(init: RouterInit): Router {
33723376
export interface CreateStaticHandlerOptions {
33733377
basename?: string;
33743378
mapRouteProperties?: MapRoutePropertiesFunction;
3375-
future?: {};
3379+
// Pick<> so we only include flags that apply to the static handler
3380+
future?: Pick<FutureConfig, "unstable_middleware">;
33763381
}
33773382

33783383
export function createStaticHandler(
@@ -3388,6 +3393,10 @@ export function createStaticHandler(
33883393
let basename = (opts ? opts.basename : null) || "/";
33893394
let mapRouteProperties =
33903395
opts?.mapRouteProperties || defaultMapRouteProperties;
3396+
let future: FutureConfig = {
3397+
unstable_middleware: false,
3398+
...opts?.future,
3399+
};
33913400

33923401
let dataRoutes = convertRoutesToDataRoutes(
33933402
routes,
@@ -3482,6 +3491,7 @@ export function createStaticHandler(
34823491
}
34833492

34843493
if (
3494+
future.unstable_middleware &&
34853495
respond &&
34863496
matches.some(
34873497
(m) => m.route.unstable_middleware || m.route.unstable_lazyMiddleware
@@ -3681,6 +3691,7 @@ export function createStaticHandler(
36813691
}
36823692

36833693
if (
3694+
future.unstable_middleware &&
36843695
respond &&
36853696
matches.some(
36863697
(m) => m.route.unstable_middleware || m.route.unstable_lazyMiddleware
@@ -5175,20 +5186,23 @@ async function callDataStrategyImpl(
51755186
fetcherKey: string | null,
51765187
manifest: RouteManifest,
51775188
mapRouteProperties: MapRoutePropertiesFunction,
5189+
future: FutureConfig,
51785190
scopedContext: unknown
51795191
): Promise<Record<string, DataStrategyResult>> {
5180-
// Ensure all lazy/lazyMiddleware async functions are kicked off in parallel
5181-
// before we await them where needed below
5182-
let loadMiddlewarePromise = loadLazyMiddlewareForMatches(matches, manifest);
5192+
// Ensure all lazy async functions are kicked off in parallel before we await
5193+
// them where needed below
51835194
let loadRouteDefinitionsPromises = matches.map((m) =>
51845195
m.route.lazy
51855196
? loadLazyRouteModule(m.route, mapRouteProperties, manifest)
51865197
: undefined
51875198
);
51885199

5189-
// Ensure all middleware is loaded before we start executing routes
5190-
if (loadMiddlewarePromise) {
5191-
await loadMiddlewarePromise;
5200+
// Ensure all middleware is loaded before we start executing dataStrategy
5201+
if (future.unstable_middleware) {
5202+
let loadMiddlewarePromise = loadLazyMiddlewareForMatches(matches, manifest);
5203+
if (loadMiddlewarePromise) {
5204+
await loadMiddlewarePromise;
5205+
}
51925206
}
51935207

51945208
let dsMatches = matches.map((match, i) => {

0 commit comments

Comments
 (0)