refactor(router-core): cache matched route branch alongside the rest of the route match #5882
main fromrefs/pull/5882/head·merged by Sheraff(GitHub profile, opens in a new tab) Summary by CodeRabbit#
- Refactor
- Route matching now includes route ancestry (branch) with each match, simplifying how matched routes are built.
- Simplified traversal: matched routes are derived directly from branch data rather than walking parent links.
- Immutable handling: matched route lists are treated as readonly and extended immutably (no in-place mutation).
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
View your CI Pipeline Execution ↗ for commit 803023269c14a2453b46bfe0971445d505143419
Command Status Duration Result nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded 7m 46s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 1m 23s View ↗
☁️ Nx Cloud last updated this comment at
2025-11-16 17:26:34UTCMore templates
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-authenticated-routes-firebase
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-devtools-panel
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-basic-non-nested-devtools
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-i18n-paraglide
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-start-example-basic-cloudflare
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-static
- tanstack-start-bun-hosting
- tanstack-start-example-clerk-basic
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-i18n-paraglide
- tanstack-start-example-large
- tanstack-start-example-material-ui
- tanstack-start-streaming-data-from-server-functions
- tanstack-start-example-supabase-basic
- tanstack-start-tailwind-v4
- tanstack-start-example-trellaux
- tanstack-start-example-workos
- tanstack-router-react-example-view-transitions
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
- tanstack-router-solid-example-authenticated-routes
- tanstack-router-solid-example-authenticated-routes-firebase
- tanstack-router-solid-example-basic
- tanstack-router-solid-example-basic-default-search-params
- tanstack-router-solid-example-basic-devtools-panel
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-non-nested-devtools
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-basic-ssr-file-based
- tanstack-router-solid-example-basic-ssr-streaming-file-based
- tanstack-router-solid-example-basic-virtual-file-based
- tanstack-router-solid-example-basic-virtual-inside-file-based
- tanstack-router-solid-example-deferred-data
- tanstack-router-solid-i18n-paraglide
- tanstack-router-solid-example-kitchen-sink
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-kitchen-sink-solid-query
- tanstack-router-solid-example-kitchen-sink-solid-query-file-based
- tanstack-router-solid-example-large-file-based
- tanstack-router-solid-example-location-masking
- tanstack-router-solid-example-navigation-blocking
- tanstack-router-solid-example-quickstart
- tanstack-router-solid-example-quickstart-esbuild-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-router-solid-example-quickstart-rspack-file-based
- tanstack-router-solid-example-quickstart-webpack-file-based
- router-solid-mono-simple
- router-solid-mono-simple-lazy
- router-solid-monorepo-solid-query
- tanstack-router-solid-example-scroll-restoration
- tanstack-solid-router-search-validator-adapters
- tanstack-solid-start-example-basic
- tanstack-solid-start-example-basic-auth
- tanstack-solid-start-example-basic-cloudflare
- tanstack-solid-start-example-basic-netlify
- tanstack-solid-start-example-basic-nitro
- tanstack-start-example-basic-solid-query
- tanstack-solid-start-example-basic-static
- tanstack-solid-start-bun-hosting
- tanstack-solid-start-example-convex-better-auth
- tanstack-solid-start-example-counter
- tanstack-solid-start-i18n-paraglide
- tanstack-solid-start-example-large
- tanstack-solid-start-streaming-data-from-server-functions
- tanstack-solid-start-example-supabase-basic
- tanstack-solid-start-tailwind-v4
- tanstack-router-solid-example-view-transitions
- tanstack-router-solid-example-with-framer-motion
- tanstack-router-solid-example-with-trpc
@tanstack/arktype-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5882@tanstack/directive-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5882@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5882@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5882@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5882@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5882@tanstack/react-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5882@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5882@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5882@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5882@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5882@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5882@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5882@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5882@tanstack/router-devtools-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5882@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5882@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5882@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5882@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5882@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5882@tanstack/server-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5882@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5882@tanstack/solid-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5882@tanstack/solid-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@5882@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5882@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5882@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5882@tanstack/start-client-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5882@tanstack/start-plugin-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5882@tanstack/start-server-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5882@tanstack/start-static-server-functions
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5882@tanstack/start-storage-context
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5882@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5882@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5882@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5882commit:
8030232Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 40b7576ff89d549af1218a1925c7cfaf9e85d724 and da014c1e001cc25545c6fd45d6d7245893882054.
📒 Files selected for processing (2)
packages/router-core/src/new-process-route-tree.ts(5 hunks)packages/router-core/src/router.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2025-10-08T08:11:47.088Z
Learnt from: nlynzaad Repo: TanStack/router PR: 5402 File: packages/router-generator/tests/generator/no-formatted-route-tree/routeTree.nonnested.snapshot.ts:19-21 Timestamp: 2025-10-08T08:11:47.088Z Learning: Test snapshot files in the router-generator tests directory (e.g., files matching the pattern `packages/router-generator/tests/generator/**/routeTree*.snapshot.ts` or `routeTree*.snapshot.js`) should not be modified or have issues flagged, as they are fixtures used to verify the generator's output and are intentionally preserved as-is.Applied to files:
packages/router-core/src/router.tspackages/router-core/src/new-process-route-tree.ts
📚 Learning: 2025-10-01T18:30:26.591Z
Learnt from: schiller-manuel Repo: TanStack/router PR: 5330 File: packages/router-core/src/router.ts:2231-2245 Timestamp: 2025-10-01T18:30:26.591Z Learning: In `packages/router-core/src/router.ts`, the `resolveRedirect` method intentionally strips the router's origin from redirect URLs when they match (e.g., `https://foo.com/bar` → `/bar` for same-origin redirects) while preserving the full URL for cross-origin redirects. This logic should not be removed or simplified to use `location.publicHref` directly.Applied to files:
packages/router-core/src/router.ts
📚 Learning: 2025-10-01T18:31:35.420Z
Learnt from: schiller-manuel Repo: TanStack/router PR: 5330 File: e2e/react-start/custom-basepath/src/routeTree.gen.ts:58-61 Timestamp: 2025-10-01T18:31:35.420Z Learning: Do not review files named `routeTree.gen.ts` in TanStack Router repositories, as these are autogenerated files that should not be manually modified.Applied to files:
packages/router-core/src/router.tspackages/router-core/src/new-process-route-tree.ts
🧬 Code graph analysis (2)
packages/router-core/src/router.ts (1)
packages/router-core/src/root.ts (1)
rootRouteId(2-2)
packages/router-core/src/new-process-route-tree.ts (3)
packages/router-core/src/lru-cache.ts (2)
LRUCache(1-5)createLRUCache(7-74)
packages/router-core/src/Matches.ts (1)
RouteMatch(118-175)
packages/router-core/src/route.ts (2)
RouteLike(1968-1979)path(1551-1553)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
@@ -2642,12 +2642,4 @@26422642 Object.assign(routeParams, match.params) // Copy params, because they're cached26432643 }264426442645- let routeCursor: TRouteLike = foundRoute || routesById[rootRouteId]!2646-2647- const matchedRoutes: Array<TRouteLike> = [routeCursor]2648-2649- while (routeCursor.parentRoute) {2650- routeCursor = routeCursor.parentRoute as TRouteLike2651- matchedRoutes.push(routeCursor)2652- }2653- matchedRoutes.reverse()2645+ const matchedRoutes = match?.branch || [routesById[rootRouteId]!]⚠️ Potential issue | 🔴 Critical
Do not mutate cached branch arrays.
match.branchis stored in the shared LRU cache. When we later push a not-found route intomatchedRoutes, we end up mutating the cached array, so every future lookup for the same path returns a branch polluted with that extra entry. Clone the branch before use so downstream logic can safely mutate it.- const matchedRoutes = match?.branch || [routesById[rootRouteId]!] + const matchedRoutes = match ? [...match.branch] : [routesById[rootRouteId]!]📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const matchedRoutes = match ? [...match.branch] : [routesById[rootRouteId]!]🤖 Prompt for AI Agents
In packages/router-core/src/router.ts around line 2645, the assignment uses match.branch directly which is a cached array in the shared LRU and later gets mutated; clone the branch before use so mutations don't affect the cache — e.g. if match?.branch exists assign a shallow copy (e.g. [...match.branch] or Array.from(match.branch]) to matchedRoutes, otherwise fall back to [routesById[rootRouteId]!]; ensure downstream code can safely push/remove items without altering the cached branch.✅ Addressed in commit 8030232
Sheraff(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Walkthrough#
A new internal
RouteMatch<T>type (including abrancharray) replaces the prior implicit match result; the match cache andfindRouteMatchnow useRouteMatch<T> | null.buildRouteBranchcreates the ancestry chain. Router logic now reads matched routes frommatch.branchinstead of manually traversing parents.Changes#
packages/router-core/src/new-process-route-tree.tsRouteMatch<T>(route, params, branch); updatedmatchCacheto `LRUCache<string, RouteMatchpackages/router-core/src/router.tsmatch?.branch(or root fallback) to produce aReadonlyArray<AnyRoute>; updated call sites to use immutable spreads when appendingnotFoundRoute.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
RouteMatchpropagates (new generic constraints).buildRouteBranchcorrectness and ordering.ReadonlyArrayand immutable updates preserve previous behavior.Possibly related PRs#
Suggested reviewers#
Poem#
Pre-merge checks and finishing touches#
✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between da014c1e001cc25545c6fd45d6d7245893882054 and 803023269c14a2453b46bfe0971445d505143419.
📒 Files selected for processing (2)
packages/router-core/src/new-process-route-tree.ts(5 hunks)packages/router-core/src/router.ts(4 hunks)🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2025-10-08T08:11:47.088Z
Applied to files:
packages/router-core/src/router.ts📚 Learning: 2025-10-01T18:31:35.420Z
Applied to files:
packages/router-core/src/router.ts📚 Learning: 2025-10-01T18:30:26.591Z
Applied to files:
packages/router-core/src/router.ts📚 Learning: 2025-09-28T21:41:45.233Z
Applied to files:
packages/router-core/src/router.ts🧬 Code graph analysis (1)
packages/router-core/src/router.ts (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
🔇 Additional comments (2)
Comment `@coderabbitai help` to get the list of available commands and usage tips.