Skip to content

feat(router-core): expose destination location to route lifecycle hooks#7817

Open
FrancoKaddour wants to merge 1 commit into
TanStack:mainfrom
FrancoKaddour:feat/lifecycle-hooks-destination-location
Open

feat(router-core): expose destination location to route lifecycle hooks#7817
FrancoKaddour wants to merge 1 commit into
TanStack:mainfrom
FrancoKaddour:feat/lifecycle-hooks-destination-location

Conversation

@FrancoKaddour

@FrancoKaddour FrancoKaddour commented Jul 15, 2026

Copy link
Copy Markdown

Closes #7778

onEnter, onStay, and onLeave now receive a second argument exposing the resolved
destination location of the navigation that triggered them:

  • onEnter(match, { location })
  • onStay(match, { location })
  • onLeave(match, { location })

Implementation

  • router.ts: the hook dispatch loop in load() passes { location: next }, where next
    is the resolved destination location of the current navigation.
  • route.ts: the three UpdatableRouteOptions signatures accept the new ctx argument,
    typed { location: ParsedLocation } following the file's existing convention of not
    supplying the search schema in route-option locations.
  • For onLeave, the received location is the destination being navigated to, not the
    location being left — the match itself already carries the departed state.
  • Backward compatible: existing callbacks that only take match keep working.

Tests

  • Updated existing callbacks tests to assert the new argument.
  • Added a dedicated test verifying all three hooks receive the destination pathname
    and search state.
  • Full router-core suite passes (1194 tests, vitest typecheck clean, tsc 5.9 clean).

Docs

  • RouteOptionsType.md: updated the three signatures and documented ctx.location.

Summary by CodeRabbit

  • New Features

    • Route lifecycle callbacks now receive the resolved destination location, including its path, search parameters, and URL.
    • The location context is available when entering, leaving, or staying on a route.
  • Documentation

    • Updated route options documentation to describe the new callback context and location details.

onEnter, onStay, and onLeave now receive a second argument with the
resolved destination ParsedLocation of the navigation that triggered
them. Consumers previously had access to the route match but not the
destination URL or search state.

Closes TanStack#7778
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ecf1c140-6207-490a-bd7e-4c9e8b4e55c6

📥 Commits

Reviewing files that changed from the base of the PR and between 11a6a0d and 6b82c27.

📒 Files selected for processing (4)
  • docs/router/api/router/RouteOptionsType.md
  • packages/router-core/src/route.ts
  • packages/router-core/src/router.ts
  • packages/router-core/tests/callbacks.test.ts

📝 Walkthrough

Walkthrough

Route lifecycle hooks now receive a second context argument containing the resolved destination location. The callback types, runtime invocation, documentation, and tests cover onEnter, onStay, and onLeave.

Changes

Destination location lifecycle hooks

Layer / File(s) Summary
Callback contract and runtime delivery
packages/router-core/src/route.ts, packages/router-core/src/router.ts, docs/router/api/router/RouteOptionsType.md
Lifecycle hook types and documentation include ctx.location, and RouterCore passes { location: next } when invoking onEnter, onStay, and onLeave.
Lifecycle location coverage
packages/router-core/tests/callbacks.test.ts
Tests verify destination pathname, search, and href values for lifecycle callbacks across enter, leave, and stay navigations.

Estimated code review effort: 2 (Simple) | ~15 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RouterCore
  participant RouteLifecycleHook
  participant DestinationLocation
  RouterCore->>RouteLifecycleHook: invoke lifecycle hook with match and ctx
  RouteLifecycleHook->>DestinationLocation: read ctx.location
Loading

Suggested labels: package: router-core

Suggested reviewers: sheraff, schiller-manuel

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: exposing destination location to router lifecycle hooks.
Linked Issues check ✅ Passed The code and docs add { location } to onEnter, onStay, and onLeave as requested in #7778.
Out of Scope Changes check ✅ Passed The changes stay focused on lifecycle hook context, with matching typings, runtime wiring, tests, and docs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 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.

@LadyBluenotes LadyBluenotes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is looking good overall. The shared router-core implementation is a nice fit, and the runtime coverage looks solid.

Could you also add direct type coverage for the new callback argument in:

  • packages/react-router/tests/route.test-d.tsx
  • packages/solid-router/tests/route.test-d.tsx

Each lifecycle callback could accept ctx and assert that ctx.location is ParsedLocation. It would also be useful to pin the deliberately unparameterized ctx.location.search type.

The existing type suites pass, but those callbacks currently only inspect match, so this would make sure the new public API stays covered.

@nx-cloud

nx-cloud Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 6b82c27

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 21m 7s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 2m 15s View ↗

☁️ Nx Cloud last updated this comment at 2026-07-15 14:55:37 UTC

@pkg-pr-new

pkg-pr-new Bot commented Jul 15, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

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

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

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

@tanstack/history

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

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-rsc

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-fn-stubs

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

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

@tanstack/vue-start-client

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

@tanstack/vue-start-server

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

@tanstack/zod-adapter

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

commit: 6b82c27

@codspeed-hq

codspeed-hq Bot commented Jul 15, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 16.6%

⚠️ 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

⚡ 3 improved benchmarks
❌ 9 regressed benchmarks
✅ 168 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory mem client loader-data-retention (solid) 157.7 KB 581.8 KB -72.89%
Memory mem client loader-data-retention (react) 203.9 KB 645.3 KB -68.4%
Memory mem server error-paths redirect (vue) 304.3 KB 409.6 KB -25.71%
Memory mem server error-paths redirect (solid) 441 KB 527.2 KB -16.35%
Memory mem server error-paths unmatched (react) 641.2 KB 764.1 KB -16.09%
Memory mem client loader-data-retention (vue) 594.9 KB 707.6 KB -15.93%
Simulation client-preload interaction loop (react) 57.3 ms 60.7 ms -5.51%
Simulation ssr control-flow unmatched 404 (react) 59 ms 62.1 ms -4.97%
Memory mem client preload-churn (react) 610 KB 632.8 KB -3.6%
Memory mem server peak-large-page (react) 2,080.9 KB 947.8 KB ×2.2
Memory mem server serialization-payload (react) 5.3 MB 3.4 MB +53.24%
Simulation ssr streaming deferred (react) 73.1 ms 70.6 ms +3.59%

Tip

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


Comparing FrancoKaddour:feat/lifecycle-hooks-destination-location (6b82c27) with main (11a6a0d)

Open in CodSpeed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(router-core): expose destination location to route lifecycle hooks

2 participants