Migration guide to v2
How to update your code to use [email protected]
Here’s a summary of the breaking changes in [email protected]
:
- Enable support for other React frameworks via adapters
- Behaviour changes
- ESM-only package
- Deprecated exports have been removed
- Renamed
nuqs/parsers
tonuqs/server
- Debug printout detection
- Dropping
next-usequerystate
- Type changes
Adapters
The biggest change is that [email protected]
now supports other React frameworks,
providing type-safe URL state for all.
You will need to wrap your app with the appropriate adapter for your framework or router, to let the hooks know how to interact with it.
Adapters are currently available for:
- Next.js (app & pages routers)
- React SPA
- Remix
- React Router
- Testing environments (Vitest, Jest, etc.)
If you are coming from nuqs v1 (which only supported Next.js), you’ll need to
wrap your app with the appropriate NuqsAdapter
:
Next.js
Minimum required version: next@>=14.2.0
Early versions of Next.js 14 were in flux with regards to shallow routing.
Supporting those earlier versions required a lot of hacks, workarounds, and
performance penalties, which were removed in [email protected]
.
App router
Pages router
Unified (router-agnostic)
If your Next.js app uses both the app and pages routers and the adapter needs to be mounted in either, you can import the unified adapter, at the cost of a slightly larger bundle size (~100B).
Other adapters
Albeit not part of a migration from v1, you can now use nuqs in other React frameworks via their respective adapters.
However, there’s one more adapter that might be of interest to you, and solves a long-standing issue with testing components using nuqs hooks:
Testing adapter
Unit-testing components that used nuqs v1 was a hassle, as it required mocking the Next.js router internals, causing abstraction leaks.
In v2, you can now wrap your components to test with the NuqsTestingAdapter
,
which provides a convenient setup & assertion API for your tests.
Here’s an example with Vitest & Testing Library:
Behaviour changes
Setting the startTransition
option no longer sets shallow: false
automatically.
This is to align with other frameworks that don’t have a concept
of shallow/deep routing.
You’ll have to set both to keep sending updates to the server and getting a loading state in Next.js:
The "use client"
directive was not included in the client import
(import {} from 'nuqs'
). It has now been added, meaning that server-side code
needs to import from nuqs/server
to avoid errors like:
ESM only
[email protected]
is now an ESM-only
package. This should not be much of an issue since Next.js supports ESM in
app code since version 12, but if you are bundling nuqs
code into an
intermediate CJS library to be consumed in Next.js, you’ll run into import issues:
If converting your library to ESM is not possible, your main option is to
dynamically import nuqs
:
Deprecated exports
Some of the v1 API was marked as deprecated back in September 2023, and has been
removed in [email protected]
.
queryTypes
parsers object
The queryTypes
object has been removed in favor of individual parser exports,
for better tree-shaking.
Replace with parseAsXYZ
to match:
subscribeToQueryUpdates
Next.js 14.1.0 makes useSearchParams
reactive to shallow search params updates,
which makes this internal helper function redundant. See #425 for context.
Renamed nuqs/parsers
to nuqs/server
When introducing the server cache in #397, the dedicated export for parsers was
reused as it didn’t include the "use client"
directive. Since it now contains
more than parsers and probably will be extended with server-only code in the future,
it has been renamed to a clearer export name.
Find and replace all occurrences of nuqs/parsers
to nuqs/server
in your code:
Debug printout detection
After the rename to nuqs
, the debugging printout detection logic handled either
next-usequerystate
or nuqs
being present in the localStorage.debug
variable.
[email protected]
only checks for the presence of the nuqs
substring to enable logs.
Update your local dev environments to match by running this once in the devtools console:
Dropping next-usequerystate
This package started under the name next-usequerystate
, and was renamed to
nuqs
in January 2024. The old package name was kept as an alias for the v1
release line.
nuqs
version 2 and onwards no longer mirror to the next-usequerystate
package name.
Type changes
The following breaking changes only apply to exported types:
- The
Options
type is no longer generic - The
UseQueryStatesOptions
is now a type rather than an interface, and is now generic over the type of the object you pass touseQueryStates
. parseAsJson
now requires a runtime validation function to infer the type of the parsed JSON data.