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
7.82k stars 574 forks source link

Default search params from validateSearch do not populate the URL #1184

Open efie45 opened 7 months ago

efie45 commented 7 months ago

Describe the bug

When using validateSearch, if you return a default value it becomes available via the useSearch hook in a component, but it does not appear in the URL.

Your Example Website or App

https://stackblitz.com/edit/github-cqxxd6?file=src%2Froutes%2Fabout.tsx

Steps to Reproduce the Bug or Issue

  1. Click on the 'about' link

Expected behavior

The default parameter from validateSearch is available via useSearch but does not show in the URL search params.

Screenshots or Videos

No response

Platform

Additional context

No response

ruiaraujo012 commented 5 months ago

Same thing here, any update on this? I'm having a strange behavior where my validateSearch run link 20 times or more when I navigate to the route (and run about 4 times in other routes)

amjed-ali-k commented 4 months ago

Your Example Website or App

stackblitz.com/edit/github-cqxxd6?file=src%2Froutes%2Fabout.tsx

This link is invalid

a1eksandrk commented 4 months ago

Raising the question. I also encountered this problem when using useNavigate, during validation the default parameters are returned, but they are not substituted in the URL.

collinosh commented 4 months ago

This is our biggest migration hurdle so far. Our users often share links to daily reports/metrics which aren't always opened the same day by the recipient- because the default state is omitted from the URL, there's no way to guarantee state consistency without forcing a redirect to update the search.

Would love to hear if anyone has a better solution, love the type safety of this library but I'm finding myself sinking far too many hours trying to reinvent the wheel.

PrimaMateria commented 3 months ago

I have made the following workaround until it gets fixed. Not nice, but it doesn't leave the URL without the search parameters.

  const navigate = useNavigate();
  useEffect(() => {
    const parameters = new URLSearchParams(document.location.search);
    if (parameters.size === 0) {
      void navigate({ search: defaultSearchParameters, replace: true });
    }
  }, [navigate]);
bhaugeea commented 2 months ago

This seems at least partly related to a new issue I just posted: #1928. Currently it appears that a full page load does populate these values but a router nav doesn't. Meanwhile the full load has other problems but the router nav works fine.

schiller-manuel commented 2 months ago

@efie45 can you please provide the reproducer again? this might be fixed by #1907

levrik commented 1 month ago

This works for me and I would actually like the exact opposite of not having default search params being set in the URL. Was surprised to find an issue complaining about the opposite.

lukaskoeller commented 1 month ago

@schiller-manuel Following up on the already existing discord discussion, I'll link my reproducer again here for completeness: https://github.com/lukaskoeller/tanstack-react-router-useEffect-navigate-reproduction?tab=readme-ov-file#reproduction-2---default-search-params-from-validatesearch-do-not-populate-the-url using v1.45.8

This seems at least partly related to a new issue I just posted: #1928. Currently it appears that a full page load does populate these values but a router nav doesn't. Meanwhile the full load has other problems but the router nav works fine.

This is basically the exact behavior I experience as well

lukaskoeller commented 1 month ago

Another related issue I noticed, is that the <Link> throws a typescript error, that

Property 'search' is missing

although all search params use optional from zod:

export const repro3Route = createRoute({
  getParentRoute: () => rootRoute,
  path: "/repro3",
  validateSearch: object({
    param1: number().optional().default(7),
    param2: string().optional().default("defaultValue1"),
  }),
  component: Repro3,
});
ts error: Property 'search' is missing
reihwald commented 1 week ago

This issue still happens for me. The different behaviour is strange. @schiller-manuel Have you maybe had the chance to look into this further?

I found something else that I find strange.

Shouldn't const search = useSearch({ from: null! }) and const searchFromLocation = useLocation({ select: (l) => l.search }) always return the same thing?

searchFromLocation seems to mirror what is seen in the url and search seems to always show the correct fallbacks. Here is is quick reproduction stackblitz: https://stackblitz.com/edit/github-2gwkq9?file=src%2Froutes%2Fparams.tsx