Skip to content
Source
Sign inSign up
TanStack/router

refactor: don't reparse upon navigation #6398

Mergedschiller-manuel(GitHub profile, opens in a new tab)merged 9 commits intomain fromrefs/pull/6398/headmerged by schiller-manuel(GitHub profile, opens in a new tab)

Summary by CodeRabbit

  • New Features

    • Navigation operations are now asynchronous and return a result indicating success or blocked.
    • Navigation state now includes cached route-match snapshots to speed back/forward navigation.
  • Bug Fixes

    • Prevents unnecessary route reloads for internal navigations via a new skip-load guard.
  • Tests

    • Test suites updated to await asynchronous navigation and DOM updates.
✏️ Tip: You can customize this high-level summary in your review settings.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core

    3. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: history

    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      📝 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

      Cohort / File(s)Summary
      Async History API
      packages/history/src/index.ts
      Introduced NavigationResult type; push/replace now return Promise<NavigationResult>; tryNavigation/notify propagate navigateOpts and return navigation result
      Subscriber / NavigateOptions
      packages/history/src/index.ts
      Made SubscriberArgs public and added skipTransitionerLoad?: boolean to NavigateOptions; notify forwards navigateOpts to subscribers
      Parsed History State
      packages/history/src/index.ts
      Added __TSR_matches, __TSR_resetScroll, and __TSR_sessionId to history state payloads
      Match Snapshot Types
      packages/router-core/src/location.ts, packages/router-core/src/index.ts
      Added ValidatedSearchEntry and MatchSnapshot types; ParsedLocation._matchSnapshot? property; export surface adjusted to include new types
      Router Core Snapshot Logic
      packages/router-core/src/router.ts
      Added snapshot-driven fast-path matching, buildMatchSnapshot/buildMatchSnapshotFromRoutes, MatchRoutesOpts.snapshot, sessionId handling, and propagation of snapshots into history state; load signature gained _skipUpdateLatestLocation
      Transitioner Load Guard
      packages/react-router/src/Transitioner.tsx
      Router.history subscriber now inspects navigateOpts?.skipTransitionerLoad to conditionally skip router.load()
      Tests — Awaiting Async Navigation
      packages/react-router/tests/*, packages/solid-router/tests/*, packages/vue-router/tests/*, packages/router-core/tests/load.test.ts
      Tests updated to await navigation completion (use await act(), waitFor, microtask awaits); some expected update counts/timings adjusted
      Generated Route Types Adjustments
      e2e/.../src/routeTree.gen.ts (multiple)
      Tweaked generated fullPath typings for some layout/redirect routes (e.g., '' → '/', '/redirect' → '/redirect/')

      Sequence Diagram(s)

      sequenceDiagram
          participant Client as Client Code
          participant History as RouterHistory
          participant Router as Router Core
          participant Matcher as Route Matcher
      
          Client->>History: push(path, state, navigateOpts)
          activate History
          History->>History: tryNavigation() -> Promise<NavigationResult>
          alt navigateOpts.skipTransitionerLoad true
              History->>Router: commit location without triggering Transitioner load
          else
              History->>Router: build/commit location and trigger load()
          end
          activate Router
          alt snapshot available and valid
              Router->>Matcher: reuse MatchSnapshot (fast-path)
          else
              Router->>Matcher: perform full matchRoutes()
          end
          Matcher-->>Router: matchResult or reused snapshot
          Router-->>History: commit location (with __TSR_matches/__TSR_sessionId)
          deactivate Router
          alt Navigation blocked by blocker
              History-->>Client: Promise resolves { type: "BLOCKED" }
          else
              History-->>Client: Promise resolves { type: "SUCCESS" }
          end
          deactivate History
      sequenceDiagram
          participant Browser as Browser (popstate)
          participant History as RouterHistory
          participant Snapshot as MatchSnapshot Cache
          participant Matcher as Route Matcher
      
          Browser->>History: popstate (back/forward)
          activate History
          alt __TSR_matches present and session valid
              History->>Snapshot: read __TSR_matches / _matchSnapshot
              Snapshot-->>History: snapshot data
              History->>Matcher: reuse snapshot (skip full matching)
          else
              History->>Matcher: perform full matchRoutes()
          end
          Matcher-->>History: matchResult
          History-->>Browser: navigation complete
          deactivate History

      Estimated code review effort

      🎯 4 (Complex) | ⏱️ ~45 minutes

      Suggested reviewers

      • nlynzaad
      • SeanCassiere

      "🐰 I nibble code and stash a map, Snapshots tucked in history's lap, Async hops make navigation bright, I skip a load and bound outright, Breadcrumbs saved for back/forward flight! 🥕"

      🚥 Pre-merge checks | ✅ 2 | ❌ 1
      ❌ Failed checks (1 warning)
      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 50.00% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (2 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe title 'refactor: don't reparse upon navigation' directly relates to the PR's core objective of implementing a snapshot-based fast-path routing mechanism to avoid reparsing routes during navigation, as evidenced by the addition of MatchSnapshot types, buildMatchSnapshot functions, and snapshot-driven caching throughout the router-core and history packages.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing touches
      • 📝 Generate docstrings

      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    5. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 4b0a7fee7faa77e822481f3ba6dc237940ccdf38

      CommandStatusDurationResult
      nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded10m 11sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 44sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-01-19 01:13:56 UTC

    6. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/arktype-adapter

      npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6398

      @tanstack/eslint-plugin-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6398

      @tanstack/history

      npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6398

      @tanstack/nitro-v2-vite-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6398

      @tanstack/react-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6398

      @tanstack/react-router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6398

      @tanstack/react-router-ssr-query

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6398

      @tanstack/react-start

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6398

      @tanstack/react-start-client

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6398

      @tanstack/react-start-server

      npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6398

      @tanstack/router-cli

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6398

      @tanstack/router-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6398

      @tanstack/router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6398

      @tanstack/router-devtools-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6398

      @tanstack/router-generator

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6398

      @tanstack/router-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6398

      @tanstack/router-ssr-query-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6398

      @tanstack/router-utils

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6398

      @tanstack/router-vite-plugin

      npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6398

      @tanstack/solid-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6398

      @tanstack/solid-router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6398

      @tanstack/solid-router-ssr-query

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6398

      @tanstack/solid-start

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6398

      @tanstack/solid-start-client

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6398

      @tanstack/solid-start-server

      npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6398

      @tanstack/start-client-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6398

      @tanstack/start-fn-stubs

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6398

      @tanstack/start-plugin-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6398

      @tanstack/start-server-core

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6398

      @tanstack/start-static-server-functions

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6398

      @tanstack/start-storage-context

      npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6398

      @tanstack/valibot-adapter

      npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6398

      @tanstack/virtual-file-routes

      npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6398

      @tanstack/vue-router

      npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6398

      @tanstack/vue-router-devtools

      npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6398

      @tanstack/vue-router-ssr-query

      npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6398

      @tanstack/vue-start

      npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6398

      @tanstack/vue-start-client

      npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6398

      @tanstack/vue-start-server

      npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6398

      @tanstack/zod-adapter

      npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6398

      commit: 4b0a7fe

    7. Actionable comments posted: 2

      🤖 Fix all issues with AI agents
      In `@packages/router-core/src/router.ts`:
      - Around line 1295-1313: The snapshot fast-path sets parsedParams = undefined
      which causes a TypeError later when code checks "key in parsedParams" if
      skipRouteOnParseError is enabled; change the snapshot branch to initialize
      parsedParams to an empty object (e.g., parsedParams = {}) or otherwise ensure
      parsedParams is always an object when snapshotValid is true so subsequent checks
      in the router (referenced by parsedParams, skipRouteOnParseError, snapshotValid,
      matchedRoutes, routeParams) won't throw.
      - Around line 2933-2974: buildMatchSnapshot fails to include a configured custom
      notFoundRoute in the returned snapshot, so when notFoundRoute is provided you
      should set snapshot.globalNotFoundRouteId to that route's id; update
      buildMatchSnapshot to check if notFoundRoute is truthy and assign
      snapshot.globalNotFoundRouteId = notFoundRoute.id (before the fallback logic
      that scans matchedRoutes or assigns rootRouteId) so snapshots include the custom
      404 route for later matching.
      🧹 Nitpick comments (1)
      packages/history/src/index.ts (1)

      227-256: Consider returning NavigationResult from go, back, and forward for API consistency.

      These methods call tryNavigation but discard its result, while push and replace now return Promise<NavigationResult>. This creates an asymmetry in the API. For browser history, this may be intentional since navigation completes via popstate events, but callers cannot programmatically detect if these navigations were blocked.

      @@ -1269,2 +1295,19 @@
      1269- let { matchedRoutes } = matchedRoutesResult
      1270- let isGlobalNotFound = false
      1295+ // Fast-path: use snapshot hint if valid (all route IDs must exist)
      1296+ const snapshot = opts?.snapshot
      1297+ 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 | undefined
      1305+ let parsedParams: Record<string, unknown> | undefined
      1306+
      1307+ if (snapshotValid) {
      1308+ // Rebuild matched routes from snapshot
      1309+ matchedRoutes = snapshot.routeIds.map((id) => this.routesById[id]!)
      1310+ routeParams = { ...snapshot.params }
      1311+ globalNotFoundRouteId = snapshot.globalNotFoundRouteId
      1312+ parsedParams = undefined // Not available from snapshot, will re-parse if needed
      1313+ } else {

      ⚠️ Potential issue | 🟠 Major

      Guard against undefined parsedParams in snapshot fast-path.

      When skipRouteOnParseError is enabled, later logic uses key in parsedParams. With the snapshot path, parsedParams is set to undefined, which can throw a TypeError. Use an empty object (or a guard) to avoid crashes.

      🐛 Proposed fix
      -      parsedParams = undefined // Not available from snapshot, will re-parse if needed
      +      // Not available from snapshot; keep empty to avoid undefined access later
      +      parsedParams = {}
      📝 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.

      Suggested change
      1295- // Fast-path: use snapshot hint if valid (all route IDs must exist)
      1296- const snapshot = opts?.snapshot
      1297- 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 | undefined
      1305- let parsedParams: Record<string, unknown> | undefined
      1306-
      1307- if (snapshotValid) {
      1308- // Rebuild matched routes from snapshot
      1309- matchedRoutes = snapshot.routeIds.map((id) => this.routesById[id]!)
      1310- routeParams = { ...snapshot.params }
      1311- globalNotFoundRouteId = snapshot.globalNotFoundRouteId
      1312- parsedParams = undefined // Not available from snapshot, will re-parse if needed
      1313- } else {
      1295+ // Fast-path: use snapshot hint if valid (all route IDs must exist)
      1296+ const snapshot = opts?.snapshot
      1297+ 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 | undefined
      1305+ let parsedParams: Record<string, unknown> | undefined
      1306+
      1307+ if (snapshotValid) {
      1308+ // Rebuild matched routes from snapshot
      1309+ matchedRoutes = snapshot.routeIds.map((id) => this.routesById[id]!)
      1310+ routeParams = { ...snapshot.params }
      1311+ globalNotFoundRouteId = snapshot.globalNotFoundRouteId
      1312+ // Not available from snapshot; keep empty to avoid undefined access later
      1313+ parsedParams = {}
      1314+ } else {
      🤖 Prompt for AI Agents
      In `@packages/router-core/src/router.ts` around lines 1295 - 1313, The snapshot
      fast-path sets parsedParams = undefined which causes a TypeError later when code
      checks "key in parsedParams" if skipRouteOnParseError is enabled; change the
      snapshot branch to initialize parsedParams to an empty object (e.g.,
      parsedParams = {}) or otherwise ensure parsedParams is always an object when
      snapshotValid is true so subsequent checks in the router (referenced by
      parsedParams, skipRouteOnParseError, snapshotValid, matchedRoutes, routeParams)
      won't throw.
    8. @@ -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 } = matchedRoutesResult
      1269- let { matchedRoutes } = matchedRoutesResult
      1270- let isGlobalNotFound = false
      1295+ // Fast-path: use snapshot hint if valid (all route IDs must exist)
      1296+ const snapshot = opts?.snapshot
      1297+ const snapshotValid =
      1298+ snapshot &&
      1299+ snapshot.routeIds.length > 0 &&
      1300+ snapshot.routeIds.every((id) => this.routesById[id])

      Why is this check necessary? (mainly snapshot.routeIds.every((id) => this.routesById[id]))

      Can we not trust what is passed here?

      theoretically if you reload you could get a new bundle served. this check tries to detect if the route tree changed. however it's not sufficient probably as the routes might still exist but would match otherwise.

      this checks if all snapshot ids exist in the routes, is this sufficient in the case where there are new routes that hasn't been loaded in the snapshot yet? specifically, is this.routeIds.every((id) => snapshot.routesById[id]) not necessary?

    9. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router

    10. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-devtools-core

    11. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: vue-router

    12. @@ -1783,3 +1877,5 @@
      17831877 // If a rewrite function is provided, use it to rewrite the URL
      17841878 const rewrittenUrl = executeRewriteOutput(this.rewrite, url)
      17851879
      1880+ // Use encoded URL path for href (consistent with parseLocation)
      1881+ const encodedHref = url.href.replace(url.origin, '')

      can probably just build this using url.pathname + url.search + url.hash here

    13. nlynzaad(GitHub profile, opens in a new tab) approved these changes

    14. Actionable comments posted: 0

      Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (1)
      packages/router-core/src/router.ts (1)

      1858-1891: href/url invariant breaks after output rewrite. href is computed from the pre‑rewrite URL while url is stored as the rewritten (public) URL. This violates the ParsedLocation contract (href should be derived from url) and can desync pathname/href from url when rewrites change origin or path.

      🐛 Proposed fix
             return {
               publicHref:
                 rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
               href: encodedHref,
      -        url: rewrittenUrl,
      +        url,
               pathname: nextPathname,
      🧹 Nitpick comments (2)
      packages/history/src/index.ts (2)

      34-43: Consider aligning go/back/forward with async results (or document the asymmetry). With push/replace now returning Promise<NavigationResult>, consumers may expect similar affordances for other navigation methods. If you intend to keep them sync, a brief doc note would prevent confusion.


      148-155: Expose navigateOpts on RouterHistory.notify for custom histories. Since notify now forwards navigateOpts to subscribers, consider widening the public RouterHistory.notify signature to (action, navigateOpts?) so custom history implementations can propagate options without casting.

    15. I think this exact PR broke my apps navigation. Before @tanstack/react-router@1.153.2 it worked and from this version it does not.

      Details:

      First when navigating to the article page I do it like that: image

      Then on my article page I have back button which uses back param as an url. Back param usually look like this: /stories?page=1&query=&limit=20 image

      When I click on it, router shows 404 page, when I reload page then it shows correct page without any URL changes.

      Should it be done differently from now on or something is off?

    16. my route params is always undefined after this PR changes

      import { createRootRouteWithContext, createRoute } from '@tanstack/react-router';
      
      const rootRoute = createRootRouteWithContext()();
      
      const layoutRoute = createRoute({
          getParentRoute: () => rootRoute,
          id: 'layout',
          component: AppPageLayout,
      });
      
      const lobbyRoute = createRoute({
          getParentRoute: () => layoutRoute,
          path: '/',
      }).lazy(() => import('./lobby/LobbyPage').then((m) => m.Route));
      
      const itemsIndexRoute = createRoute({
          getParentRoute: () => layoutRoute,
          path: 'items',
      }).lazy(() => import('./item/ItemsPage').then((m) => m.Route));
      
      const itemRoute = createRoute({
          getParentRoute: () => rootRoute,
          path: 'items/view/$itemId',
          loader: (props) => {
              // props.params.itemId is undefined after @tanstack/react-router@1.153.2
          },
      }).lazy(() => import('./item/ItemPage').then((m) => m.Route));
      
      export const routeTree = rootRoute.addChildren([
        itemRoute,
        layoutRoute.addChildren([itemsIndexRoute]),
        lobbyRoute
      ]);
    17. Seeing a similar issue, our app with lots of search param usage now causes a "not found" every time search params change. Once you reload the page, it works.

    18. arekrgw(GitHub profile, opens in a new tab)commented

      Seeing a similar issue, our app with lots of search param usage now causes a "not found" every time search params change. Once you reload the page, it works.

      I've pulled newest changes today and it works now for me

      Ignore me, it does not work, went back to stable version

    19. iffa(GitHub profile, opens in a new tab)commented

      Seeing a similar issue, our app with lots of search param usage now causes a "not found" every time search params change. Once you reload the page, it works.

      I've pulled newest changes today and it works now for me

      Ignore me, it does not work, went back to stable version

      Yep, version 1.154.13 still has the same issue, 1.153.1 is the latest one that works.

    20. we reverted this and will revisit this perf optimization at a later point

Sign in to join this conversation.