Skip to content

Commit

Permalink
Limit matchRoutes optimization to client side routers (#12882)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Jan 28, 2025
1 parent c02d029 commit a039471
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-masks-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

REMOVE: limit matchRoutes optimization to client side
6 changes: 3 additions & 3 deletions packages/react-router/__tests__/dom/ssr/components-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ describe("<NavLink />", () => {

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

invariant(
!(staticHandlerContext instanceof Response),
Expand Down
7 changes: 6 additions & 1 deletion packages/react-router/lib/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export function useRoutesImpl(
`useRoutes() may be used only in the context of a <Router> component.`
);

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

// Use data router matches when available to avoid another match routes call.
// Skip this during SSR because the matches coming in from StaticHandlerContext
// might be UI agnostic and we want the matches from the createStaticRouter's
// routes
let matches =
!isStatic &&
dataRouterState &&
dataRouterState.matches &&
dataRouterState.matches.length > 0
Expand Down

0 comments on commit a039471

Please sign in to comment.