refactor: don't reparse upon navigation #6398
Mergedschiller-manuel(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/6398/headmerged by schiller-manuel(GitHub profile, opens in a new tab) github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: history
- @@ -1269,2 +1295,19 @@1269- let { matchedRoutes } = matchedRoutesResult1270- let isGlobalNotFound = false1295+ // Fast-path: use snapshot hint if valid (all route IDs must exist)1296+ const snapshot = opts?.snapshot1297+ const snapshotValid =1298+ snapshot &&1299+ snapshot.routeIds.length > 0 &&1300+ snapshot.routeIds.every((id) => this.routesById[id])1301+1302+ let matchedRoutes: ReadonlyArray<AnyRoute>1303+ let routeParams: Record<string, string>1304+ let globalNotFoundRouteId: string | undefined1305+ let parsedParams: Record<string, unknown> | undefined1306+1307+ if (snapshotValid) {1308+ // Rebuild matched routes from snapshot1309+ matchedRoutes = snapshot.routeIds.map((id) => this.routesById[id]!)1310+ routeParams = { ...snapshot.params }1311+ globalNotFoundRouteId = snapshot.globalNotFoundRouteId1312+ parsedParams = undefined // Not available from snapshot, will re-parse if needed1313+ } else {packages/router-core/src/router.ts lines 2938 to 2983Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2786,0 +2954,21 @@2954+ : trimPathRight(pathname)2955+2956+ if (isGlobalNotFound) {2957+ if (!notFoundRoute) {2958+ if (notFoundMode !== 'root') {2959+ for (let i = matchResult.matchedRoutes.length - 1; i >= 0; i--) {2960+ const route = matchResult.matchedRoutes[i]!2961+ if (route.children) {2962+ snapshot.globalNotFoundRouteId = route.id2963+ break2964+ }2965+ }2966+ }2967+ if (!snapshot.globalNotFoundRouteId) {2968+ snapshot.globalNotFoundRouteId = rootRouteId2969+ }2970+ }2971+ }2972+2973+ return snapshot2974+}
- @@ -1264,7 +1292,9 @@12641292 next: ParsedLocation,12651293 opts?: MatchRoutesOpts,12661294 ): Array<AnyRouteMatch> {1267- const matchedRoutesResult = this.getMatchedRoutes(next.pathname)1268- const { foundRoute, routeParams, parsedParams } = matchedRoutesResult1269- let { matchedRoutes } = matchedRoutesResult1270- let isGlobalNotFound = false1295+ // Fast-path: use snapshot hint if valid (all route IDs must exist)1296+ const snapshot = opts?.snapshot1297+ const snapshotValid =1298+ snapshot &&1299+ snapshot.routeIds.length > 0 &&1300+ snapshot.routeIds.every((id) => this.routesById[id])
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-devtools-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: vue-router
- @@ -1783,3 +1877,5 @@17831877 // If a rewrite function is provided, use it to rewrite the URL17841878 const rewrittenUrl = executeRewriteOutput(this.rewrite, url)178518791880+ // Use encoded URL path for href (consistent with parseLocation)1881+ const encodedHref = url.href.replace(url.origin, '')
nlynzaad(GitHub profile, opens in a new tab) approved these changes
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
Adds async navigation results and navigate options propagation, introduces match-snapshot caching and session markers for fast-path back/forward navigations, updates Transitioner to optionally skip loads, and updates tests to await asynchronous navigation timing.
Changes#
packages/history/src/index.tsNavigationResulttype;push/replacenow returnPromise<NavigationResult>;tryNavigation/notify propagatenavigateOptsand return navigation resultpackages/history/src/index.tsSubscriberArgspublic and addedskipTransitionerLoad?: booleantoNavigateOptions;notifyforwardsnavigateOptsto subscriberspackages/history/src/index.ts__TSR_matches,__TSR_resetScroll, and__TSR_sessionIdto history state payloadspackages/router-core/src/location.ts,packages/router-core/src/index.tsValidatedSearchEntryandMatchSnapshottypes;ParsedLocation._matchSnapshot?property; export surface adjusted to include new typespackages/router-core/src/router.tsbuildMatchSnapshot/buildMatchSnapshotFromRoutes,MatchRoutesOpts.snapshot,sessionIdhandling, and propagation of snapshots into history state; load signature gained_skipUpdateLatestLocationpackages/react-router/src/Transitioner.tsxnavigateOpts?.skipTransitionerLoadto conditionally skiprouter.load()packages/react-router/tests/*,packages/solid-router/tests/*,packages/vue-router/tests/*,packages/router-core/tests/load.test.tsawait act(),waitFor, microtask awaits); some expected update counts/timings adjustede2e/.../src/routeTree.gen.ts(multiple)fullPathtypings for some layout/redirect routes (e.g.,''→'/','/redirect'→'/redirect/')Sequence Diagram(s)#
Estimated code review effort#
🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs#
Suggested reviewers#
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
Comment `@coderabbitai help` to get the list of available commands and usage tips.