TanStack / TanStack/router

`stringifySearchWith` does not call `stringify` method on strings

Open
#3,254 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Router

Describe the bug

When using stringifySearchWith as stringifySearch option on the router instance with just the first argument supplied, strings do not get passed to the stringify method if not given a 2nd undocumented (https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization) parse method. By default JSON.parse is getting passed in there which results in strings only containing numbers to be encoded as "123" instead of just 123. A few weeks ago I supplied a custom stringifySearchWith to simply support encoding Set instances as well. This changed the behavior of how strings are encoded which in the end led to numeric string params to be ignored completely as I had a Zod validator defined with .string().optional().catch(undefined). Removing the .catch(undefined) revealed that it was receiving a number instead of a string.

Your Example Website or App

https://stackblitz.com/edit/github-qmrnwt4n?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue
  1. Click "Add numeric search param"
  2. It crashes
  3. Remove stringifySearch from router and reload
  4. Click "Add numeric search param" again
  5. It works as expected
Expected behavior

Difficult to say. I saw that this 2nd parse argument was introduced in https://github.com/TanStack/router/issues/654 but I don't really understand the background. At least this 2nd parameter should get documented or if not given, stringify should still be called?

Screenshots or Videos

https://github.com/user-attachments/assets/ad46d899-d078-4ee2-96be-2a4c1e7d8c24

Platform
  • OS: macOS
  • Browser: Edge
  • Version: 1.95.3
Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/main.tsx in the linked StackBlitz and inspect the router's stringifySearchWith/stringifySearch configuration, then compare it with the default router behavior. Confirm the intended handling of numeric strings and whether the missing parse argument should be documented or change the serialization behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend, search, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.