pages/ directory structure).
Installation
1
Install nuqs
First, install nuqs in your Next.js project:
2
Add the adapter to _app.tsx
Wrap your application with the
NuqsAdapter in your custom App component:pages/_app.tsx
If you don’t have a custom
_app.tsx, create one following the Next.js documentation.3
Use nuqs hooks in your pages
Now you can use
useQueryState and useQueryStates in any page component:pages/search.tsx
Version Requirements
- Next.js:
>=14.2.0 - React:
>=18.2.0or^19.0.0-0
Features
Shallow Updates (Default)
By default, URL updates don’t triggergetServerSideProps to re-run:
Server-Side Updates
Opt into re-runninggetServerSideProps by setting shallow: false:
Dynamic Routes
The adapter automatically handles dynamic route segments, preserving them during URL updates:pages/post/[id].tsx
id) from search params (page).
How It Works
The Pages Router adapter:- Uses
useRouter()fromnext/compat/routerto access the router - Reads search params from
router.query - Updates the URL using
router.push()orrouter.replace() - Automatically extracts and preserves dynamic route segments
- Listens to
routeChangeStartandbeforeHistoryChangeevents to reset queues
Server-Side Rendering
You can access and parse search params ingetServerSideProps:
pages/products.tsx
Catch-All Routes
The adapter supports catch-all and optional catch-all routes:pages/docs/[...slug].tsx
Troubleshooting
Search params conflict with dynamic route segments
If you have a search param with the same name as a dynamic segment, the dynamic segment takes precedence:getServerSideProps not re-running
Make sure you’re passingshallow: false when updating state:
Not working with Next.js 14.1 or older
The Pages Router adapter requires Next.js 14.2.0 or newer. For older versions:- Upgrade to Next.js 14.2.0+, or
- Use nuqs v1.x which has built-in support for older Next.js versions