47ng / nuqs

Type-safe search params state manager for React frameworks - Like useState, but stored in the URL query string.
https://nuqs.47ng.com
MIT License
4.86k stars 104 forks source link

`BUG`: Using `urlKeys` option with history push makes state unresponsive to URL change #758

Closed hallucinogenizer closed 1 week ago

hallucinogenizer commented 1 week ago

Context

What's your version of nuqs?

"nuqs": "2.1.1"

What framework are you using?

Which version of your framework are you using? v14.2.0

-> Paste the relevant framework versions from your package.json here
"dependencies": {
    "next": "14.2.0",
    "nuqs": "2.1.1",
    "react": "^18",
    "react-dom": "^18"
  },

Description

If you use the urlKeys option along with history: "push", then on pressing the browser back button, the query params get removed from the URL but the state variable returned by useQueryStates still shows the old value.

This issue does not occur if you remove urlKeys.

Reproduction

https://codesandbox.io/p/devbox/tender-paper-8sd759

Example: Steps to reproduce the behavior:

  1. Open the code sandbox
  2. Go to /working route to see the correct behvaior. The steps to perform are written there. This page does not use urlKeys hence it works well.
  3. Now go to the /not-working route to see the incorrect behavior that happens when you add urlKeys. The steps to perform are mentioned there.
franky47 commented 1 week ago

Thanks for this great report!

I can reproduce the error and I think I see where it's coming from, I'll have a fix out shortly.

franky47 commented 1 week ago

Could you try this and let me know if it solves the problem on your end?

pnpm add https://pkg.pr.new/nuqs@759
hallucinogenizer commented 1 week ago

Yup, this fixes the problem in my codebase as well. Thanks!

github-actions[bot] commented 1 week ago

:tada: This issue has been resolved in version 2.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: