TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
8.28k stars 664 forks source link

Default search becomes empty object when used in `search` #2799

Open OwenVey opened 1 week ago

OwenVey commented 1 week ago

Which project does this relate to?

Router

Describe the bug

I am using both the stripSearchParams and retainSearchParams middlewares to strip default values from the query params as well as removing any parameters not related to that route. This works as expected as shown in the reproduction URL below but the moment I use my DefaultSearch object in a Link like so:

<Link to={Route.fullPath} search={DefaultSearch}>
  Clear Search
</Link>

the DefaultSearch object itself becomes {} which then causes problems with the middlewares.

My guess is there is a bug somewhere where the object being passed to search is being overwritten.

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-fyxlu6?file=src%2Froutes%2F__root.tsx

Steps to Reproduce the Bug or Issue

  1. Open reproduction link and click the "Open Preview in new tab" button in the top right corner (so that we can see search params in URL)
  2. Notice that when switching between the "Home" and "About" tabs, the middlewares are working as expected and the DefaultSearch objects are being displayed as expected.
  3. Go to the __root.tsx file and uncomment lines 20 and 29
  4. Switch between the two tabs and notice that the DefaultSearch objects are displayed as {} and the search middlewards no longer function as expected.

Expected behavior

I expect my DefaultSearch object be unmodified and log out as originally created but I am seeing it log as an empty object when using my DefaultSearch in a <Link>'s serach.

Screenshots or Videos

Image

Platform

Additional context

Mentioned in the TanStack Discord here: https://discord.com/channels/719702312431386674/1306270495016095847/1306442814875242548