Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer is returning an empty string on default search params in v2 #715

Closed
blechatellier opened this issue Oct 27, 2024 · 9 comments · Fixed by #720
Closed

Serializer is returning an empty string on default search params in v2 #715

blechatellier opened this issue Oct 27, 2024 · 9 comments · Fixed by #720
Labels

Comments

@blechatellier
Copy link

blechatellier commented Oct 27, 2024

Context

What's your version of nuqs?
[email protected]

What framework are you using?
✅ Next.js (app router)

Which version of your framework are you using?
[email protected]

Description

After upgrading to v2, the serializer returns an empty string when search params are using the default.

@blechatellier blechatellier added the bug Something isn't working label Oct 27, 2024
@franky47 franky47 removed the bug Something isn't working label Oct 27, 2024
@franky47
Copy link
Member

That's a consequence of the option clearOnDefault changing from false to true by default in v2.

It's true that this might be counter-intuitive for the serializer, if one wants to create a link that does hardcode the defaults in the URL.

Unfortunately for now, the serializer does not accept global options, like useQueryStates, but it should if we want to support urlKeys as well. In the mean time, the alternative would be to pass the clearOnDefault: false to each parser in the object when configuring the serializer:

const serialize = createSerializer({
  foo: parseAsString.withOptions({ clearOnDefault: false }),
  bar: parseAsInteger.withOptions({ clearOnDefault: false })
})

I'll see what I can do to add global options support this week.

@blechatellier
Copy link
Author

Thanks @franky47 - setting clearOnDefault to false indeed fix the issue. Somehow my next implementation doesn't re-render unless the params are hardcoded.

@franky47
Copy link
Member

Could you elaborate that last part? The useQueryState(s) hooks should follow the URL, and if a key isn't in the query string, return the default value if specified (or null otherwise).

@blechatellier
Copy link
Author

I'm passing the serialize function directly to the <Link href={serialize(...)} /> next component. When the params are matching the defaults, it sets the link to an empty string, preventing the page to re-render.

@kasperadk
Copy link

@blechatellier Thank you for posting this! I was struggling with the same problem.

@franky47
Copy link
Member

franky47 commented Oct 29, 2024

I've added a fix for this in #720.

createSerializer now accepts a second argument where you can specify both clearOnDefault: false (it still defaults to true to mirror useQueryState(s)' behaviour) and a urlKeys map to shorten your query keys (same as for useQueryStates).

You can try it here:

pnpm add https://pkg.pr.new/nuqs@720

@blechatellier
Copy link
Author

@franky47 awesome, cleaner this way.

@franky47
Copy link
Member

🎉 This issue has been resolved in version 2.1.0 🎉

The release is available on:

@blechatellier
Copy link
Author

Thanks @franky47 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants