router: Default search params from validateSearch do not populate the URL

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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 15 (6 by maintainers)

Most upvoted comments

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

@allan-cannon please create a new issue and include a complete minimal example

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