router icon indicating copy to clipboard operation
router copied to clipboard

Default search params from validateSearch do not populate the URL

Open efie45 opened this issue 1 year ago • 10 comments

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

  • OS: Windows
  • Browser: Chrome, Edge

Additional context

No response

efie45 avatar Feb 14 '24 14:02 efie45

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)

ruiaraujo012 avatar Apr 12 '24 09:04 ruiaraujo012

Your Example Website or App

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

This link is invalid

amjed-ali-k avatar May 08 '24 12:05 amjed-ali-k

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.

a1eksandrk avatar May 15 '24 08:05 a1eksandrk

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.

collinosh avatar May 17 '24 23:05 collinosh

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]);

PrimaMateria avatar Jun 04 '24 14:06 PrimaMateria

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.

bhaugeea avatar Jul 09 '24 18:07 bhaugeea

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

schiller-manuel avatar Jul 09 '24 18:07 schiller-manuel

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.

levrik avatar Jul 24 '24 12:07 levrik

@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 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 avatar Jul 26 '24 05:07 lukaskoeller

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

lukaskoeller avatar Jul 26 '24 05:07 lukaskoeller