Skip to content
Source
Sign inSign up
TanStack/router

fix: Commit context together with status to avoid race condition #6407

Mergedschiller-manuel(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/6407/headmerged by schiller-manuel(GitHub profile, opens in a new tab)

fixes #6082

Summary by CodeRabbit

  • Performance Improvements
    • Optimized route navigation to reduce store update frequency across React Router, Solid Router, and Vue Router implementations, resulting in fewer state updates during navigation transitions.
    • Improved context data handling to be applied more efficiently during route matching and loader execution.
✏️ 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: solid-router

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

    5. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      📝 Walkthrough

      Walkthrough

      This PR refactors where route match context is built and attached during navigation. Instead of using deferred commitContext calls, context is now constructed directly within state updates—during loader execution and redirect/not-found handling—resulting in fewer store updates across the routing system.

      Changes

      Cohort / File(s)Summary
      Core context attachment refactoring
      packages/router-core/src/load-matches.ts
      Context is now built inline at state update points (success, error, redirect/notFound) via buildMatchContext(inner, index) rather than through separate commitContext calls. Removes deferred context commits from loadRouteMatch finalization blocks.
      Test expectation updates - React Router
      packages/react-router/tests/store-updates-during-navigation.test.tsx
      Adjusted expected store update counts downward across multiple navigation tests to reflect consolidated context building (e.g., 11→10, 5→4, 10→9, 8→6, 19→16).
      Test expectation updates - Solid Router
      packages/solid-router/tests/store-updates-during-navigation.test.tsx
      Reduced expected update counts in navigation tests (e.g., 11→10, 7→6, 10→8, 19→16, 9→7, 5→3) to align with consolidated context handling.
      Test expectation updates - Vue Router
      packages/vue-router/tests/store-updates-during-navigation.test.tsx
      Adjusted expected update counts downward across tests (33→27, 13→10, 31→25, 47→38, 24→18, 22→16, 18→12, 6→3) to reflect the reduced number of store updates from consolidated context building.

      Estimated code review effort

      🎯 3 (Moderate) | ⏱️ ~20 minutes

      • #6053: Modifies how route context is committed/attached in load-matches.ts, moving context building to earlier stages in the flow.
      • #6070: Directly centralizes route match context building using buildMatchContext during loader/beforeLoad flows in load-matches.ts.
      • #6082: Addresses a bug where getRouteApi().useRouteContext() returns undefined for beforeLoad context values—this PR likely fixes that by ensuring context is built earlier and more consistently.

      Suggested labels

      package: router-core, package: react-router, package: solid-router, package: vue-router

      Suggested reviewers

      • nlynzaad

      Poem

      🐰 Context now blooms where updates flow, No more deferred commits steal the show, Consolidated paths, a cleaner way— Fewer ripples in the store each day! ✨

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe title clearly describes the main fix: committing context together with status to avoid a race condition, which directly addresses the issue of undefined context values.
      Linked Issues check✅ PassedThe PR addresses issue #6082 by fixing how context is committed with status updates in load-matches.ts, which resolves the race condition causing useRouteContext() to return undefined.
      Out of Scope Changes check✅ PassedTest file updates in React Router, Solid Router, and Vue Router reflecting lower update counts are consistent with the core fix reducing unnecessary context commits.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      ✏️ 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.
    6. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 85c6cfa2cdcc917b9eb7fc812b32c77d0fa478e1

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

      ☁️ Nx Cloud last updated this comment at 2026-01-17 23:57:47 UTC

    7. More templates

      @tanstack/arktype-adapter

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

      @tanstack/eslint-plugin-router

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

      @tanstack/history

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

      @tanstack/nitro-v2-vite-plugin

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

      @tanstack/react-router

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

      @tanstack/react-router-devtools

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

      @tanstack/react-router-ssr-query

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

      @tanstack/react-start

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

      @tanstack/react-start-client

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

      @tanstack/react-start-server

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

      @tanstack/router-cli

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

      @tanstack/router-core

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

      @tanstack/router-devtools

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

      @tanstack/router-devtools-core

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

      @tanstack/router-generator

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

      @tanstack/router-plugin

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

      @tanstack/router-ssr-query-core

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

      @tanstack/router-utils

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

      @tanstack/router-vite-plugin

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

      @tanstack/solid-router

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

      @tanstack/solid-router-devtools

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

      @tanstack/solid-router-ssr-query

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

      @tanstack/solid-start

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

      @tanstack/solid-start-client

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

      @tanstack/solid-start-server

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

      @tanstack/start-client-core

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

      @tanstack/start-fn-stubs

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

      @tanstack/start-plugin-core

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

      @tanstack/start-server-core

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

      @tanstack/start-static-server-functions

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

      @tanstack/start-storage-context

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

      @tanstack/valibot-adapter

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

      @tanstack/virtual-file-routes

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

      @tanstack/vue-router

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

      @tanstack/vue-router-devtools

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

      @tanstack/vue-router-ssr-query

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

      @tanstack/vue-start

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

      @tanstack/vue-start-client

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

      @tanstack/vue-start-server

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

      @tanstack/zod-adapter

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

      commit: 85c6cfa

Sign in to join this conversation.