Skip to content

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

@andyGallagher

Description

@andyGallagher

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions