TanStack / TanStack/router

`useMatchRoute` returns false on first render when navigating same route with different search params when `retainSearchParams` middleware is present

Open
#5,984 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

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
  1. 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)]
     }
   })
  1. 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>
   }
  1. Create a route at /route-a (child of the component using useMatchRoute)
  2. 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>
  1. Load the app and navigate to /route-a?test=foo
  2. Click the second link to navigate to /route-a?test=bar
  3. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior in the linked StackBlitz example, then inspect packages/router-core/src/router.ts around lines 2548-2573, where the issue identifies the location mismatch. Compare the latest and resolved location snapshots during same-route search-param navigation. Done means useMatchRoute consistently returns a match on the initial render with retainSearchParams enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.