Skip to content

Limit matchRoutes optimization to client side routers #12882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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