Skip to content

Commit 5112743

Browse files
authored
POC optimization for matchRoutes (#12169)
1 parent 3fd082a commit 5112743

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

.changeset/short-pugs-double.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"react-router": patch
3+
"@remix-run/router": patch
4+
---
5+
6+
Optimize route matching by skipping redundant `matchRoutes` calls when possible

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@
108108
"none": "58.1 kB"
109109
},
110110
"packages/react-router/dist/react-router.production.min.js": {
111-
"none": "16.5 kB"
111+
"none": "16.52 kB"
112112
},
113113
"packages/react-router/dist/umd/react-router.production.min.js": {
114-
"none": "19.0 kB"
114+
"none": "19.1 kB"
115115
},
116116
"packages/react-router-dom/dist/react-router-dom.production.min.js": {
117117
"none": "17.5 kB"

packages/react-router/lib/hooks.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from "react";
22
import type {
3+
AgnosticRouteMatch,
34
Blocker,
45
BlockerFunction,
56
Location,
@@ -445,7 +446,12 @@ export function useRoutesImpl(
445446
remainingPathname = "/" + segments.slice(parentSegments.length).join("/");
446447
}
447448

448-
let matches = matchRoutes(routes, { pathname: remainingPathname });
449+
let matches =
450+
dataRouterState &&
451+
dataRouterState.matches &&
452+
dataRouterState.matches.length > 0
453+
? (dataRouterState.matches as AgnosticRouteMatch<string, RouteObject>[])
454+
: matchRoutes(routes, { pathname: remainingPathname });
449455

450456
if (__DEV__) {
451457
warning(

packages/router/router.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ export function createRouter(init: RouterInit): Router {
829829
let initialScrollRestored = init.hydrationData != null;
830830

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

834835
if (initialMatches == null && !patchRoutesOnNavigationImpl) {
@@ -874,6 +875,7 @@ export function createRouter(init: RouterInit): Router {
874875
init.history.location.pathname
875876
);
876877
if (fogOfWar.active && fogOfWar.matches) {
878+
initialMatchesIsFOW = true;
877879
initialMatches = fogOfWar.matches;
878880
}
879881
}
@@ -1522,7 +1524,14 @@ export function createRouter(init: RouterInit): Router {
15221524

15231525
let routesToUse = inFlightDataRoutes || dataRoutes;
15241526
let loadingNavigation = opts && opts.overrideNavigation;
1525-
let matches = matchRoutes(routesToUse, location, basename);
1527+
let matches =
1528+
opts?.initialHydration &&
1529+
state.matches &&
1530+
state.matches.length > 0 &&
1531+
!initialMatchesIsFOW
1532+
? // `matchRoutes()` has already been called if we're in here via `router.initialize()`
1533+
state.matches
1534+
: matchRoutes(routesToUse, location, basename);
15261535
let flushSync = (opts && opts.flushSync) === true;
15271536

15281537
let fogOfWar = checkFogOfWar(matches, routesToUse, location.pathname);

0 commit comments

Comments
 (0)