Skip to content

Commit a039471

Browse files
authored
Limit matchRoutes optimization to client side routers (#12882)
1 parent c02d029 commit a039471

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

.changeset/loud-masks-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
REMOVE: limit matchRoutes optimization to client side

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ describe("<NavLink />", () => {
150150

151151
describe("<ServerRouter>", () => {
152152
it("handles empty default export objects from the compiler", async () => {
153-
let staticHandlerContext = await createStaticHandler([
154-
{ id: "root", path: "/", children: [{ id: "empty", index: true }] },
155-
]).query(new Request("http://localhost/"));
153+
let staticHandlerContext = await createStaticHandler([{ path: "/" }]).query(
154+
new Request("http://localhost/")
155+
);
156156

157157
invariant(
158158
!(staticHandlerContext instanceof Response),

packages/react-router/lib/hooks.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ export function useRoutesImpl(
448448
`useRoutes() may be used only in the context of a <Router> component.`
449449
);
450450

451-
let { navigator } = React.useContext(NavigationContext);
451+
let { navigator, static: isStatic } = React.useContext(NavigationContext);
452452
let { matches: parentMatches } = React.useContext(RouteContext);
453453
let routeMatch = parentMatches[parentMatches.length - 1];
454454
let parentParams = routeMatch ? routeMatch.params : {};
@@ -535,7 +535,12 @@ export function useRoutesImpl(
535535
remainingPathname = "/" + segments.slice(parentSegments.length).join("/");
536536
}
537537

538+
// Use data router matches when available to avoid another match routes call.
539+
// Skip this during SSR because the matches coming in from StaticHandlerContext
540+
// might be UI agnostic and we want the matches from the createStaticRouter's
541+
// routes
538542
let matches =
543+
!isStatic &&
539544
dataRouterState &&
540545
dataRouterState.matches &&
541546
dataRouterState.matches.length > 0

0 commit comments

Comments
 (0)