Skip to content

fix(router-core): keep useLocation on the rendered route's location - #8038

Open
ulrichstark wants to merge 1 commit into
TanStack:mainfrom
ulrichstark:fix-issue-8037
Open

fix(router-core): keep useLocation on the rendered route's location#8038
ulrichstark wants to merge 1 commit into
TanStack:mainfrom
ulrichstark:fix-issue-8037

Conversation

@ulrichstark

@ulrichstark ulrichstark commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

This change is AI generated.

useLocation read router.stores.location, which is written when a navigation starts rather than when its matches are published, so a component on the route being left re-rendered once with the destination's pathname before unmounting.

Add stores.presentedLocation, published atomically with the match lane inside setMatches, and read it from useLocation in react, solid, and vue. router.state.location is unchanged and still reports the requested location while it loads.

Closes #8037

Summary by CodeRabbit

  • Bug Fixes

    • Corrected useLocation behavior across React, Solid, and Vue routers so it reflects the location associated with currently rendered routes.
    • Prevented components from observing a destination pathname before the corresponding route is presented.
    • Preserved access to the requested location through router.state.location during loading.
  • Documentation

    • Clarified when useLocation updates and how to observe requested locations during navigation.
  • Tests

    • Added regression coverage for stale pathname reporting during navigation.

@nx-cloud

nx-cloud Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 962cf68

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 11m 44s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 58s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-11 09:10:47 UTC

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Presented location synchronization

Layer / File(s) Summary
Presented location store contract
packages/router-core/src/router.ts, packages/router-core/src/stores.ts
Router stores now expose presentedLocation. Match updates receive and publish the location associated with the presented matches.
Publication and rollback location flow
packages/router-core/src/load-client.ts, packages/router-core/src/load-server.ts
Client and server publication, rollback, refresh, restoration, and hydration paths track committed locations with match sets.
Framework hook adoption and validation
packages/react-router/src/useLocation.tsx, packages/solid-router/src/useLocation.tsx, packages/vue-router/src/useLocation.tsx, packages/react-router/tests/*, docs/router/api/router/useLocationHook.md, .changeset/*
Framework hooks read presentedLocation. Documentation, release metadata, and a regression test describe and validate the rendered-route behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • TanStack/router#7948: Modifies overlapping lane-based RouterStores and match-publication internals.
  • TanStack/router#7992: Modifies match presentation and loading flows that track the rendered UI location.

Suggested labels: package: router-core

Suggested reviewers: sheraff

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.65% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes fix issue #8037 by reading the atomically published presented location and adding a regression test for stale destination pathnames.
Out of Scope Changes check ✅ Passed The changes remain within scope by updating router location publication, framework hooks, documentation, changesets, and regression coverage.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: keeping useLocation aligned with the rendered route's location during navigation.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ulrichstark ulrichstark changed the title fix(router): keep useLocation on the rendered route's location fix(router-core): keep useLocation on the rendered route's location Aug 11, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 11, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@8038

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

npm i https://pkg.pr.new/@tanstack/eslint-plugin-start@8038

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@8038

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

npm i https://pkg.pr.new/@tanstack/react-start@8038

@tanstack/react-start-client

npm i https://pkg.pr.new/@tanstack/react-start-client@8038

@tanstack/react-start-rsc

npm i https://pkg.pr.new/@tanstack/react-start-rsc@8038

@tanstack/react-start-server

npm i https://pkg.pr.new/@tanstack/react-start-server@8038

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

npm i https://pkg.pr.new/@tanstack/solid-start@8038

@tanstack/solid-start-client

npm i https://pkg.pr.new/@tanstack/solid-start-client@8038

@tanstack/solid-start-server

npm i https://pkg.pr.new/@tanstack/solid-start-server@8038

@tanstack/start-client-core

npm i https://pkg.pr.new/@tanstack/start-client-core@8038

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/@tanstack/start-fn-stubs@8038

@tanstack/start-plugin-core

npm i https://pkg.pr.new/@tanstack/start-plugin-core@8038

@tanstack/start-server-core

npm i https://pkg.pr.new/@tanstack/start-server-core@8038

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

npm i https://pkg.pr.new/@tanstack/start-storage-context@8038

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@8038

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@8038

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

npm i https://pkg.pr.new/@tanstack/vue-start@8038

@tanstack/vue-start-client

npm i https://pkg.pr.new/@tanstack/vue-start-client@8038

@tanstack/vue-start-server

npm i https://pkg.pr.new/@tanstack/vue-start-server@8038

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@8038

commit: 962cf68

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/router-core/src/router.ts`:
- Around line 1086-1087: Update the _committedLocation property to use
ParsedLocation<FullSearchSchema<TRouteTree>> instead of the default
ParsedLocation type, matching latestLocation so route-specific search keys and
value types are preserved.

In `@packages/solid-router/src/useLocation.tsx`:
- Around line 30-41: Add braces to both unbraced if statements in the
useLocation flow: the opts?.select guard and the prev === undefined check inside
Solid.createMemo. Preserve their existing return behavior and do not change
surrounding selection logic.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f128de46-8c05-4de1-8d4d-310bd07653f8

📥 Commits

Reviewing files that changed from the base of the PR and between 783f883 and 962cf68.

📒 Files selected for processing (10)
  • .changeset/use-location-presented-lane.md
  • docs/router/api/router/useLocationHook.md
  • packages/react-router/src/useLocation.tsx
  • packages/react-router/tests/issue-8037-use-location-stale-pathname.test.tsx
  • packages/router-core/src/load-client.ts
  • packages/router-core/src/load-server.ts
  • packages/router-core/src/router.ts
  • packages/router-core/src/stores.ts
  • packages/solid-router/src/useLocation.tsx
  • packages/vue-router/src/useLocation.tsx

Comment on lines +1086 to +1087
/** The location that produced `_committed`. */
_committedLocation?: ParsedLocation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- target file outline ---'
ast-grep outline packages/router-core/src/router.ts | head -120

printf '%s\n' '--- relevant declarations and usages ---'
rg -n -C 4 'ParsedLocation|FullSearchSchema|_committedLocation|latestLocation' packages/router-core/src/router.ts packages/router-core/src

Repository: TanStack/router

Length of output: 50372


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- location and schema types ---'
cat -n packages/router-core/src/location.ts | sed -n '1,70p'
rg -n -C 3 'export type AnySchema|type AnySchema' packages/router-core/src

printf '%s\n' '--- RouterCore declaration and fields ---'
cat -n packages/router-core/src/router.ts | sed -n '1000,1125p'

printf '%s\n' '--- committed-location signatures and calls ---'
rg -n -C 8 '_committedLocation|function publishMatches|publishMatches\(' packages/router-core/src/load-client.ts packages/router-core/src/router.ts

Repository: TanStack/router

Length of output: 18632


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- load-client type aliases and location-bearing types ---'
rg -n -C 10 'type CoordinatorRouter|interface CoordinatorRouter|type LoadTransaction|type ProjectedLane|type PublicationCheckpoint|previousPresentedLocation|function publishMatches' packages/router-core/src/load-client.ts

printf '%s\n' '--- location-producing declarations passed to publication ---'
sed -n '145,245p' packages/router-core/src/load-client.ts
sed -n '1565,1630p' packages/router-core/src/load-client.ts
sed -n '2620,2660p' packages/router-core/src/load-client.ts

printf '%s\n' '--- compiler availability ---'
if command -v tsc >/dev/null 2>&1; then
  tsc --version
else
  printf '%s\n' 'tsc unavailable'
fi

Repository: TanStack/router

Length of output: 12429


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- AnyRouter and route-info aliases ---'
rg -n -C 8 'export type AnyRouter|type AnyRouter|export type FullSearchSchema|type FullSearchSchema|type ParseRoute|PartialMergeAll' packages/router-core/src/router.ts packages/router-core/src/routeInfo.ts packages/router-core/src

printf '%s\n' '--- strict assignability probe ---'
cat > /tmp/router-location-type-probe.ts <<'TS'
type AnySchema = {}
interface ParsedLocation<TSearchObj extends AnySchema = {}> {
  search: TSearchObj
  maskedLocation?: ParsedLocation<TSearchObj>
}

type RequiredSearch = { required: string }
declare const bare: ParsedLocation
declare const typed: ParsedLocation<RequiredSearch>

const typedFromBare: ParsedLocation<RequiredSearch> = bare
const bareFromTyped: ParsedLocation = typed

interface Router<TRouteTree> {
  committed?: ParsedLocation<TRouteTree>
}
declare const router: Router<RequiredSearch>
router.committed = bare
TS

tsc --strict --noEmit --skipLibCheck /tmp/router-location-type-probe.ts 2>&1 || true

Repository: TanStack/router

Length of output: 17099


🏁 Script executed:

#!/bin/bash
set -eu

cat > /tmp/router-location-type-probe.ts <<'TS'
type AnySchema = {}

interface ParsedLocation<TSearchObj extends AnySchema = {}> {
  search: TSearchObj
  maskedLocation?: ParsedLocation<TSearchObj>
}

type RequiredSearch = { required: string }
type OptionalSearch = { optional?: string }

declare const bare: ParsedLocation
declare const requiredTyped: ParsedLocation<RequiredSearch>
declare const optionalTyped: ParsedLocation<OptionalSearch>

const requiredFromBare: ParsedLocation<RequiredSearch> = bare
const optionalFromBare: ParsedLocation<OptionalSearch> = bare
const bareFromRequired: ParsedLocation = requiredTyped
const bareFromOptional: ParsedLocation = optionalTyped

interface Router<TRouteTree> {
  committed?: ParsedLocation<TRouteTree>
}
declare const requiredRouter: Router<RequiredSearch>
declare const optionalRouter: Router<OptionalSearch>
requiredRouter.committed = bare
optionalRouter.committed = bare
TS

tsc --ignoreConfig --strict --noEmit --skipLibCheck /tmp/router-location-type-probe.ts 2>&1 || true

Repository: TanStack/router

Length of output: 751


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- search-schema merge helper ---'
sed -n '95,132p' packages/router-core/src/utils.ts

printf '%s\n' '--- optional route-schema probe ---'
cat > /tmp/router-location-optional-schema-probe.ts <<'TS'
type AnySchema = {}
interface ParsedLocation<TSearchObj extends AnySchema = {}> {
  search: TSearchObj
}

type FullSearchSchema = { page?: number; filter?: string }
declare const bare: ParsedLocation
declare const typed: ParsedLocation<FullSearchSchema>

const page: number | undefined = typed.search.page
const filter: string | undefined = typed.search.filter
const bareFromTyped: ParsedLocation = typed

// These must fail: the bare default loses the route-specific keys.
const missingPage: number | undefined = bare.search.page
const missingFilter: string | undefined = bare.search.filter
TS

tsc --ignoreConfig --strict --noEmit --skipLibCheck /tmp/router-location-optional-schema-probe.ts 2>&1 || true

Repository: TanStack/router

Length of output: 1346


Preserve the route search schema in _committedLocation.

ParsedLocation defaults its search type to {}, which removes route-specific search keys and value types. Use ParsedLocation<FullSearchSchema<TRouteTree>>, consistent with latestLocation.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/router-core/src/router.ts` around lines 1086 - 1087, Update the
_committedLocation property to use ParsedLocation<FullSearchSchema<TRouteTree>>
instead of the default ParsedLocation type, matching latestLocation so
route-specific search keys and value types are preserved.

Source: Coding guidelines

Comment on lines 30 to 41
if (!opts?.select) {
return (() => router.stores.location.get()) as Accessor<
return (() => router.stores.presentedLocation.get()) as Accessor<
UseLocationResult<TRouter, TSelected>
>
}

const select = opts.select

return Solid.createMemo((prev: TSelected | undefined) => {
const res = select(router.stores.location.get())
const res = select(router.stores.presentedLocation.get())
if (prev === undefined) return res
return replaceEqualDeep(prev, res)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add braces to both if statements.

Line 30 and line 40 use unbraced control bodies. Add braces while changing this function.

Proposed fix
-  if (!opts?.select) {
+  if (!opts?.select) {
     return (() => router.stores.presentedLocation.get()) as Accessor<
       UseLocationResult<TRouter, TSelected>
     >
   }
@@
-    if (prev === undefined) return res
+    if (prev === undefined) {
+      return res
+    }

As per coding guidelines, **/*.{ts,tsx} requires braces for every control statement. Based on learnings, changed Solid adapter code must fix directly related unbraced branches.

📝 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
if (!opts?.select) {
return (() => router.stores.location.get()) as Accessor<
return (() => router.stores.presentedLocation.get()) as Accessor<
UseLocationResult<TRouter, TSelected>
>
}
const select = opts.select
return Solid.createMemo((prev: TSelected | undefined) => {
const res = select(router.stores.location.get())
const res = select(router.stores.presentedLocation.get())
if (prev === undefined) return res
return replaceEqualDeep(prev, res)
if (!opts?.select) {
return (() => router.stores.presentedLocation.get()) as Accessor<
UseLocationResult<TRouter, TSelected>
>
}
const select = opts.select
return Solid.createMemo((prev: TSelected | undefined) => {
const res = select(router.stores.presentedLocation.get())
if (prev === undefined) {
return res
}
return replaceEqualDeep(prev, res)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/solid-router/src/useLocation.tsx` around lines 30 - 41, Add braces
to both unbraced if statements in the useLocation flow: the opts?.select guard
and the prev === undefined check inside Solid.createMemo. Preserve their
existing return behavior and do not change surrounding selection logic.

Sources: Coding guidelines, Learnings

@codspeed-hq

codspeed-hq Bot commented Aug 11, 2026

Copy link
Copy Markdown

Merging this PR will regress 11 benchmarks

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 13 improved benchmarks
❌ 11 regressed benchmarks
✅ 156 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory mem server error-paths unmatched (react) 434.1 KB 548.7 KB -20.89%
Memory mem client unique-location-churn (solid) 384.9 KB 449.4 KB -14.35%
Memory mem server server-fn-churn (vue) 327.8 KB 369.9 KB -11.38%
Memory mem server error-paths redirect (vue) 439.7 KB 493.9 KB -10.97%
Memory mem client interrupted-navigations (vue) 512.8 KB 541.6 KB -5.32%
Memory mem server error-paths redirect (solid) 366.3 KB 386.8 KB -5.28%
Simulation client-route-tree-scale navigation loop (vue) 253.8 ms 267.2 ms -5.03%
Memory mem server request-churn (react) 649.8 KB 678.6 KB -4.25%
Simulation client-history navigation loop (vue) 154.9 ms 160.8 ms -3.67%
Simulation client-history navigation loop (react) 133.6 ms 138.4 ms -3.47%
Simulation client-mount loop (react) 129.8 ms 134 ms -3.12%
Memory mem server request-churn (vue) 2,905.2 KB 862.6 KB ×3.4
Memory mem server serialization-payload (solid) 5.9 MB 4.6 MB +28%
Memory mem client unique-location-churn (vue) 1.3 MB 1.1 MB +22.12%
Memory mem server error-paths not-found (vue) 573.3 KB 471.8 KB +21.51%
Memory mem server aborted-requests (vue) 1,103 KB 1,007.1 KB +9.51%
Memory mem client navigation-churn (solid) 714.4 KB 658.2 KB +8.55%
Memory mem server peak-large-page (solid) 1.2 MB 1.1 MB +5.08%
Memory mem server error-paths not-found (react) 417.3 KB 399.4 KB +4.48%
Memory mem server error-paths unmatched (vue) 608.4 KB 586.4 KB +3.75%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ulrichstark:fix-issue-8037 (962cf68) with main (783f883)

Open in CodSpeed

@Sheraff

Sheraff commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

i actually kinda like this proposal. But this would be a breaking change, some people actually rely on useLocation tracking the URL and not the matches. We might have to delay this for the next major

@ulrichstark

Copy link
Copy Markdown
Contributor Author

Since the original issue is fixed for me, we can do anything you like with this PR. Do you want me to close it or turn it into a draft PR?

@Sheraff Sheraff added the v2 label Aug 12, 2026
@Sheraff

Sheraff commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Since the original issue is fixed for me, we can do anything you like with this PR. Do you want me to close it or turn it into a draft PR?

I'll keep it on the side for v2

  • replace the current useLocation with this proposed version (basically a location that has the exact same lifecycle as the matches, so it's always "what is rendered")
  • and if we really still need it: usePendingLocation would be the current implementation, tracking the URL instead of what is rendered
  • and we drop resolvedLocation unless we find someone who can justify the need for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useLocation returns pathname of route being navigated to

2 participants