Skip to content

Commit a9384a2

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

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

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

Lines changed: 25 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
@@ -2793,6 +2797,7 @@ export function createRouter(init: RouterInit): Router {
27932797
fetcherKey,
27942798
manifest,
27952799
mapRouteProperties,
2800+
future,
27962801
scopedContext
27972802
);
27982803
} catch (e) {
@@ -3372,7 +3377,8 @@ export function createRouter(init: RouterInit): Router {
33723377
export interface CreateStaticHandlerOptions {
33733378
basename?: string;
33743379
mapRouteProperties?: MapRoutePropertiesFunction;
3375-
future?: {};
3380+
// Pick<> so we only include flags that apply to the static handler
3381+
future?: Pick<FutureConfig, "unstable_middleware">;
33763382
}
33773383

33783384
export function createStaticHandler(
@@ -3388,6 +3394,10 @@ export function createStaticHandler(
33883394
let basename = (opts ? opts.basename : null) || "/";
33893395
let mapRouteProperties =
33903396
opts?.mapRouteProperties || defaultMapRouteProperties;
3397+
let future: FutureConfig = {
3398+
unstable_middleware: false,
3399+
...opts?.future,
3400+
};
33913401

33923402
let dataRoutes = convertRoutesToDataRoutes(
33933403
routes,
@@ -3482,6 +3492,7 @@ export function createStaticHandler(
34823492
}
34833493

34843494
if (
3495+
future.unstable_middleware &&
34853496
respond &&
34863497
matches.some(
34873498
(m) => m.route.unstable_middleware || m.route.unstable_lazyMiddleware
@@ -3681,6 +3692,7 @@ export function createStaticHandler(
36813692
}
36823693

36833694
if (
3695+
future.unstable_middleware &&
36843696
respond &&
36853697
matches.some(
36863698
(m) => m.route.unstable_middleware || m.route.unstable_lazyMiddleware
@@ -4159,6 +4171,7 @@ export function createStaticHandler(
41594171
null,
41604172
manifest,
41614173
mapRouteProperties,
4174+
future,
41624175
requestContext
41634176
);
41644177

@@ -5175,20 +5188,23 @@ async function callDataStrategyImpl(
51755188
fetcherKey: string | null,
51765189
manifest: RouteManifest,
51775190
mapRouteProperties: MapRoutePropertiesFunction,
5191+
future: FutureConfig,
51785192
scopedContext: unknown
51795193
): 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);
5194+
// Ensure all lazy async functions are kicked off in parallel before we await
5195+
// them where needed below
51835196
let loadRouteDefinitionsPromises = matches.map((m) =>
51845197
m.route.lazy
51855198
? loadLazyRouteModule(m.route, mapRouteProperties, manifest)
51865199
: undefined
51875200
);
51885201

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

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

0 commit comments

Comments
 (0)