Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/short-pugs-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"react-router": patch
"@remix-run/router": patch
---

Optimize route matching by skipping redundant `matchRoutes` calls when possible
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@
"none": "58.1 kB"
},
"packages/react-router/dist/react-router.production.min.js": {
"none": "16.5 kB"
"none": "16.52 kB"
},
"packages/react-router/dist/umd/react-router.production.min.js": {
"none": "19.0 kB"
"none": "19.1 kB"
},
"packages/react-router-dom/dist/react-router-dom.production.min.js": {
"none": "17.5 kB"
Expand Down
8 changes: 7 additions & 1 deletion packages/react-router/lib/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from "react";
import type {
AgnosticRouteMatch,
Blocker,
BlockerFunction,
Location,
Expand Down Expand Up @@ -445,7 +446,12 @@ export function useRoutesImpl(
remainingPathname = "/" + segments.slice(parentSegments.length).join("/");
}

let matches = matchRoutes(routes, { pathname: remainingPathname });
let matches =
dataRouterState &&
dataRouterState.matches &&
dataRouterState.matches.length > 0
? (dataRouterState.matches as AgnosticRouteMatch<string, RouteObject>[])
: matchRoutes(routes, { pathname: remainingPathname });

if (__DEV__) {
warning(
Expand Down
11 changes: 10 additions & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ export function createRouter(init: RouterInit): Router {
let initialScrollRestored = init.hydrationData != null;

let initialMatches = matchRoutes(dataRoutes, init.history.location, basename);
let initialMatchesIsFOW = false;
let initialErrors: RouteData | null = null;

if (initialMatches == null && !patchRoutesOnNavigationImpl) {
Expand Down Expand Up @@ -874,6 +875,7 @@ export function createRouter(init: RouterInit): Router {
init.history.location.pathname
);
if (fogOfWar.active && fogOfWar.matches) {
initialMatchesIsFOW = true;
initialMatches = fogOfWar.matches;
}
}
Expand Down Expand Up @@ -1522,7 +1524,14 @@ export function createRouter(init: RouterInit): Router {

let routesToUse = inFlightDataRoutes || dataRoutes;
let loadingNavigation = opts && opts.overrideNavigation;
let matches = matchRoutes(routesToUse, location, basename);
let matches =
opts?.initialHydration &&
state.matches &&
state.matches.length > 0 &&
!initialMatchesIsFOW
? // `matchRoutes()` has already been called if we're in here via `router.initialize()`
state.matches
: matchRoutes(routesToUse, location, basename);
let flushSync = (opts && opts.flushSync) === true;

let fogOfWar = checkFogOfWar(matches, routesToUse, location.pathname);
Expand Down
Loading