Guides
URL State Parameters with NUQS
Guide for managing URL state parameters using NUQS
Guide: URL State Management with nuqs
Purpose
This guide covers how we use nuqs
for type-safe URL state management in Next.js 15+ applications. It provides server-side URL parameter parsing with seamless client-side integration.
Key Principles
1. Framework Adapter Required
- Always wrap your app with
NuqsAdapter
fromnuqs/adapters/next
- Required for Next.js 15+ compatibility
2. Server-First Approach
- Parse URL parameters on the server using
createSearchParamsCache
- Always await
searchParams
in Next.js 15+ page components - Use
Promise.all
for concurrent async operations
3. Type Safety
- Define parsers with proper types and defaults
- Use appropriate parser types:
parseAsString
,parseAsInteger
,parseAsBoolean
- Always provide default values
4. Hybrid Usage
- Share parser configurations between server and client components
- Use
useQueryStates
hook for client-side parameter manipulation
Implementation Overview
Setup
Install and configure the framework adapter:
Basic Pattern
1. Create Search Params Configuration
2. Use in Page Components
3. Client-Side Updates
Best Practices
URL Structure
- Use short, semantic parameter names
- Consider URL key mapping for better UX:
pageSize
→size
- Keep URLs readable and shareable
Type Safety
- Always use appropriate parsers for data types
- Add validation constraints when needed
- Provide meaningful default values
Performance
- Initialize cache at the page level only
- Use concurrent operations with
Promise.all
- Handle async operations properly in Next.js 15+
Common Patterns
Filtering and Pagination
Boolean Toggles
Breaking Changes in nuqs 2
- Framework Adapters: Must wrap app with
NuqsAdapter
- Default Behaviors:
clearOnDefault
is nowtrue
by default - JSON Parser: Requires validation function
- Testing: Use
NuqsTestingAdapter
for tests
Key Takeaways
- Always use framework adapter - Required for Next.js 15+
- Server-first parsing - Parse parameters on server, use on client
- Type safety matters - Use proper parsers and defaults
- Keep URLs clean - Use semantic names and consider key mapping
- Handle async properly - Await searchParams and use Promise.all
This approach gives us type-safe, server-rendered URL state that works seamlessly with client-side interactions.