Skip to content

Commit b1cae72

Browse files
committed
Move RSC matcher creation out of createStaticRouter
1 parent e5cde38 commit b1cae72

4 files changed

Lines changed: 55 additions & 55 deletions

File tree

packages/react-router/__tests__/dom/ssr/components-test.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,21 @@ describe("<NavLink />", () => {
163163

164164
describe("<ServerRouter>", () => {
165165
it("handles empty default export objects from the compiler", async () => {
166-
let staticHandlerContext = await createStaticHandler([{ path: "/" }]).query(
167-
new Request("http://localhost/"),
168-
);
166+
let staticHandlerContext = await createStaticHandler([
167+
{
168+
id: "root",
169+
path: "/",
170+
children: [{ id: "empty", index: true }],
171+
},
172+
]).query(new Request("http://localhost/"));
169173

170174
invariant(
171175
!(staticHandlerContext instanceof Response),
172176
"Expected a context",
173177
);
174178

175179
let context = mockEntryContext({
180+
staticHandlerContext,
176181
manifest: {
177182
routes: {
178183
root: {
@@ -528,16 +533,17 @@ describe("<Links />", () => {
528533

529534
describe("<Scripts />", () => {
530535
it("propagates nonce to modulepreload links", async () => {
531-
let staticHandlerContext = await createStaticHandler([{ path: "/" }]).query(
532-
new Request("http://localhost/"),
533-
);
536+
let staticHandlerContext = await createStaticHandler([
537+
{ id: "root", path: "/" },
538+
]).query(new Request("http://localhost/"));
534539

535540
invariant(
536541
!(staticHandlerContext instanceof Response),
537542
"Expected a context",
538543
);
539544

540545
let context = mockEntryContext({
546+
staticHandlerContext,
541547
manifest: {
542548
routes: {
543549
root: {
@@ -604,16 +610,17 @@ describe("<Scripts />", () => {
604610
});
605611

606612
it("propagates the ServerRouter nonce to default HydrateFallback scripts when a route has a clientLoader without a HydrateFallback", async () => {
607-
let staticHandlerContext = await createStaticHandler([{ path: "/" }]).query(
608-
new Request("http://localhost/"),
609-
);
613+
let staticHandlerContext = await createStaticHandler([
614+
{ id: "root", path: "/" },
615+
]).query(new Request("http://localhost/"));
610616

611617
invariant(
612618
!(staticHandlerContext instanceof Response),
613619
"Expected a context",
614620
);
615621

616622
let context = mockEntryContext({
623+
staticHandlerContext,
617624
manifest: {
618625
routes: {
619626
root: {

packages/react-router/lib/dom/server.tsx

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import type {
1414
RevalidationState,
1515
StaticHandlerContext,
1616
} from "../router/router";
17-
import {
18-
createDataRouteMatcher,
19-
IDLE_BLOCKER,
20-
IDLE_FETCHER,
21-
IDLE_NAVIGATION,
22-
} from "../router/router";
17+
import { IDLE_BLOCKER, IDLE_FETCHER, IDLE_NAVIGATION } from "../router/router";
2318
import type {
2419
DataRouteObject,
2520
RouteBranch,
@@ -395,7 +390,6 @@ export function createStaticRouter(
395390
...opts?.future,
396391
};
397392
let matchRoutes = context._match;
398-
let dataRouteMatcher: ReturnType<typeof createDataRouteMatcher> | undefined;
399393

400394
// Because our context matches may be from a set of routes passed to
401395
// createStaticHandler(), we update them here with our newly created/enhanced
@@ -447,18 +441,7 @@ export function createStaticRouter(
447441
return undefined;
448442
},
449443
match(locationArg) {
450-
let routeMatches;
451-
if (matchRoutes) {
452-
routeMatches = matchRoutes(locationArg);
453-
} else {
454-
// Contexts not created by createStaticHandler() need their own matcher.
455-
if (!dataRouteMatcher) {
456-
dataRouteMatcher = createDataRouteMatcher(context.basename || "/");
457-
dataRouteMatcher.update(dataRoutes);
458-
}
459-
routeMatches = dataRouteMatcher.match(locationArg);
460-
}
461-
return routeMatches && routeMatches.map(mapRouteMatch);
444+
return matchRoutes(locationArg)?.map(mapRouteMatch) ?? null;
462445
},
463446
initialize() {
464447
throw msg("initialize");

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ export interface StaticHandlerContext {
480480
actionHeaders: Record<string, Headers>;
481481
_deepestRenderedBoundaryId?: string | null;
482482
/** @private */
483-
_match?: StaticHandler["match"];
483+
_match: StaticHandler["match"];
484484
}
485485

486486
/**
@@ -4578,7 +4578,9 @@ export function createStaticHandler(
45784578
routeMatch: DataRouteMatch | null,
45794579
filterMatchesToLoad: ((m: DataRouteMatch) => boolean) | null,
45804580
skipRevalidation: boolean,
4581-
): Promise<Omit<StaticHandlerContext, "location" | "basename"> | Response> {
4581+
): Promise<
4582+
Omit<StaticHandlerContext, "location" | "basename" | "_match"> | Response
4583+
> {
45824584
invariant(
45834585
request.signal,
45844586
"query()/queryRoute() requests must contain an AbortController signal",
@@ -4648,7 +4650,9 @@ export function createStaticHandler(
46484650
isRouteRequest: boolean,
46494651
filterMatchesToLoad: ((m: DataRouteMatch) => boolean) | null,
46504652
skipRevalidation: boolean,
4651-
): Promise<Omit<StaticHandlerContext, "location" | "basename"> | Response> {
4653+
): Promise<
4654+
Omit<StaticHandlerContext, "location" | "basename" | "_match"> | Response
4655+
> {
46524656
let result: DataResult;
46534657

46544658
if (!actionMatch.route.action && !actionMatch.route.lazy) {
@@ -4844,7 +4848,7 @@ export function createStaticHandler(
48444848
): Promise<
48454849
| Omit<
48464850
StaticHandlerContext,
4847-
"location" | "basename" | "actionData" | "actionHeaders"
4851+
"location" | "basename" | "actionData" | "actionHeaders" | "_match"
48484852
>
48494853
| Response
48504854
> {

packages/react-router/lib/rsc/server.ssr.tsx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { shouldHydrateRouteLoader } from "../dom/ssr/routes";
1010
import type { RSCPayload } from "./server.rsc";
1111
import { createRSCRouteModules } from "./route-modules";
1212
import { isRouteErrorResponse, type DataRouteObject } from "../router/utils";
13-
import { hasInvalidProtocol } from "../router/router";
13+
import {
14+
createDataRouteMatcher,
15+
hasInvalidProtocol,
16+
type StaticHandlerContext,
17+
} from "../router/router";
1418
import {
1519
decodeRedirectErrorDigest,
1620
decodeRouteErrorResponseDigest,
@@ -560,7 +564,28 @@ export function RSCStaticRouter({ getPayload, nonce }: RSCStaticRouterProps) {
560564
}
561565
}
562566

563-
const context = {
567+
const routes = payload.matches.reduceRight((previous, match) => {
568+
const route: DataRouteObject = {
569+
id: match.id,
570+
action: match.hasAction || !!match.clientAction,
571+
element: match.element,
572+
errorElement: match.errorElement,
573+
handle: match.handle,
574+
hydrateFallbackElement: match.hydrateFallbackElement,
575+
index: match.index,
576+
loader: match.hasLoader || !!match.clientLoader,
577+
path: match.path,
578+
shouldRevalidate: match.shouldRevalidate,
579+
};
580+
if (previous.length > 0) {
581+
route.children = previous;
582+
}
583+
return [route];
584+
}, [] as DataRouteObject[]);
585+
const dataRouteMatcher = createDataRouteMatcher(payload.basename || "/");
586+
dataRouteMatcher.update(routes);
587+
588+
const context: StaticHandlerContext = {
564589
get _deepestRenderedBoundaryId() {
565590
return decoded._deepestRenderedBoundaryId ?? null;
566591
},
@@ -575,6 +600,7 @@ export function RSCStaticRouter({ getPayload, nonce }: RSCStaticRouterProps) {
575600
loaderHeaders: {},
576601
location: payload.location,
577602
statusCode: 200,
603+
_match: (locationArg) => dataRouteMatcher.match(locationArg),
578604
matches: payload.matches.map((match) => ({
579605
params: match.params,
580606
pathname: match.pathname,
@@ -591,27 +617,7 @@ export function RSCStaticRouter({ getPayload, nonce }: RSCStaticRouterProps) {
591617
})),
592618
};
593619

594-
const router = createStaticRouter(
595-
payload.matches.reduceRight((previous, match) => {
596-
const route: DataRouteObject = {
597-
id: match.id,
598-
action: match.hasAction || !!match.clientAction,
599-
element: match.element,
600-
errorElement: match.errorElement,
601-
handle: match.handle,
602-
hydrateFallbackElement: match.hydrateFallbackElement,
603-
index: match.index,
604-
loader: match.hasLoader || !!match.clientLoader,
605-
path: match.path,
606-
shouldRevalidate: match.shouldRevalidate,
607-
};
608-
if (previous.length > 0) {
609-
route.children = previous;
610-
}
611-
return [route];
612-
}, [] as DataRouteObject[]),
613-
context,
614-
);
620+
const router = createStaticRouter(routes, context);
615621

616622
const frameworkContext: FrameworkContextObject = {
617623
future: {},

0 commit comments

Comments
 (0)