Skip to main content

Overview

The createLoader function creates a type-safe loader function for parsing search parameters from various input types. It’s designed for one-off parsing operations in loaders, API routes, getServerSideProps, or any server-side context where you need to parse search params.

Basic Usage

Function Signature

Parameters

ParserMap
required
An object mapping search param keys to their parser configurations. Each parser defines how to parse and serialize values.
CreateLoaderOptions
Optional configuration object.

Accepted Input Types

The loader function accepts multiple input types:

String

URL

URLSearchParams

Request

Record Object

Promise (Next.js 15+)

Loader Options

The loader function accepts an optional second parameter for runtime options:
boolean
default:false
When true, the loader will throw an error if any parser fails to parse its value. When false, failed parsers return null or their default value.

Strict Mode Example

Framework Examples

Next.js App Router

Server Component

API Route

Remix

React Router

Next.js Pages Router (getServerSideProps)

URL Keys Mapping

You can map internal state keys to different URL query parameter names:

Type Inference

TypeScript automatically infers the return type based on your parsers:

Sharing with Client Code

You can share parser configurations between server loaders and client hooks:

Best Practices

1

Use withDefault for required values

If a search param should always have a value, use .withDefault() to avoid null checks:
2

Enable strict mode for validation

Use strict: true when you need to validate that search params are well-formed:
3

Share parser configurations

Define parsers once and reuse them in both server loaders and client hooks for consistency.

Cache

Use createSearchParamsCache for server components

Parsers

Learn about available parser types