Skip to main content

Overview

The createSerializer function creates a type-safe serializer for generating URLs with search parameters. It’s useful for creating links, redirects, and programmatic navigation with properly formatted query strings.

Basic Usage

Function Signature

Parameters

ParserMap
required
An object mapping search param keys to their parser configurations.
CreateSerializerOptions
Optional configuration object.

Serialize Function

The returned serializer function has two overloads:

Generate Query String

Generates a query string from the provided values:

Append to Base URL

Appends or amends the query string of a base URL:

Null Values

Passing null for a value removes it from the URL:

Next.js

Remix

React Router

Programmatic Navigation

Server-Side Redirects (Next.js)

API Routes

Default Value Behavior

By default, values matching their parser’s default are omitted from the URL:
Disable this behavior with clearOnDefault: false:

URL Keys Mapping

Map internal keys to different URL parameter names:

Complex Serialization Example

Sharing with Hooks and Cache

Share the same parser configuration across serializers, loaders, caches, and hooks:

Type Inference

The serializer is fully type-safe:

Post-Processing

Use processUrlSearchParams to add custom logic:

Best Practices

1

Share parser configurations

Define parsers once and reuse them in serializers, loaders, caches, and hooks for consistency.
2

Use URL keys for clean URLs

Map verbose internal names to short URL parameter names:
3

Leverage clearOnDefault

Keep URLs clean by omitting default values (enabled by default).
4

Use null to remove parameters

Explicitly remove parameters by passing null as their value.

Loaders

Parse search params with createLoader

Cache

Access params in server components

Parsers

Learn about available parser types