-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Which project does this relate to?
Router
Describe the bug
When navigating between two links that have the same base path but different search params (e.g. /a?test=test to /a?test=test2), useMatchRoute returns the match running match({ to: '/a' }). However, with the retainSearchParams middleware enabled, this same match function returns false on initial render and then the match on the second render.
This initial false render with retainSearchParams enabled is unexpected, since we are still always on route A. I've dug a bit into the code and it looks like with retainSearchParams enabled, the search comparison fails because buildLocation uses latestLocation (with new search params) while baseLocation uses resolvedLocation (with stale search params), causing a mismatch between two different snapshots of the router state.
The workaround here could be supplying includeSearch: false to the match function, but this feels a bit wrong and unintuitive.
Your Example Website or App
https://stackblitz.com/edit/tanstack-router-9kfrmysz?file=src%2Fmain.tsx
Steps to Reproduce the Bug or Issue
- Create a route with search parameter validation and middleware
const rootRoute = createRootRoute({
validateSearch: (search): { test?: string } => ({
test: search.test
}),
search: {
middlewares: [retainSearchParams(['test'] as const)]
}
})- Add a component that uses
useMatchRoute
function MyComponent() {
const matchRoute = useMatchRoute()
const result = matchRoute({ to: '/route-a' })
console.log('Match result:', result)
return <div>...</div>
}- Create a route at
/route-a(child of the component usinguseMatchRoute) - Add two links that navigate to the same route with different search params
<Link to="/route-a" search={{ test: 'foo' }}>Link 1</Link>
<Link to="/route-a" search={{ test: 'bar' }}>Link 2</Link>- Load the app and navigate to
/route-a?test=foo - Click the second link to navigate to
/route-a?test=bar - Observe the console logs:
- First log: `Match result: false` ❌ (incorrect)
- Second log: `Match result: {}` ✅ (correct)Expected behavior
useMatchRoute should consistently return match since you're already on the route.
Screenshots or Videos
No response
Platform
- Router / Start Version: 1.139.6
- OS: macOS
- Browser: Chrome
- Browser Version: Latest
- Bundler: Vite
- Bundler Version: 7.1.7
Additional context
No response