Skip to main content
Search middleware allows you to intercept and transform search parameters during navigation. This is useful for retaining certain params across routes, removing default values, or applying custom transformations.

Overview

Search middleware functions run during navigation to modify search parameters before they’re applied to the URL. They receive the current and next search params and can return modified values.

Middleware Signature

A search middleware is a function with the following signature:

Built-in Middleware

TanStack Router provides two built-in middleware functions:

retainSearchParams

Retain specific search params across navigations.
With this middleware, the usersView param will be retained when navigating to child routes or within this route tree.

Retain All Params

Pass true to retain all search params:

Retain Multiple Params

stripSearchParams

Remove optional or default-valued search params to keep URLs clean.

Strip All Optional Params

If there are no required params, pass true to strip all:

Strip Specific Optional Params

Pass an array to always remove specific optional keys:

Combining Middleware

You can chain multiple middleware functions:
Middleware executes in array order: each middleware receives the output of the previous one.

Custom Middleware

Create custom middleware for advanced transformations:

Middleware Execution

Middleware runs during navigation, after validation but before the new search params are applied:
  1. User initiates navigation with new search params
  2. Router validates search params using validateSearch
  3. Router runs search middleware in order
  4. Router applies final search params to URL
  5. Components re-render with new params

Use Cases

Persistent Filters

Keep filter state when navigating to detail views:

Clean URLs

Remove default values to keep URLs minimal:

Multi-Step Forms

Retain form state across steps:

Tab State Persistence

Keep tab selection when navigating:

Middleware Context

The middleware context provides:
  • search: Current search params (from the current route)
  • next: Function that returns the next search params (after navigation)
Call next(search) to get the incoming search params, modify them, and return the final values.

Type Safety

Middleware is fully type-safe when used with TypeScript:

Performance Considerations

  1. Keep middleware simple - Complex operations can slow down navigation
  2. Avoid async operations - Middleware should be synchronous
  3. Use memoization - Cache expensive computations
  4. Minimize transformations - Only modify params when necessary

Debugging Middleware

Add logging to understand middleware behavior:

beforeLoad vs Middleware

Understand the difference between these two concepts: Search Middleware:
  • Modifies search parameters
  • Runs during navigation
  • Synchronous only
  • Returns modified search params
  • Focused on URL state
beforeLoad Hook:
  • Prepares route context and data
  • Can be asynchronous
  • Can redirect or throw errors
  • Returns context additions
  • Focused on route lifecycle

Best Practices

  1. Apply at the highest route - Place middleware on parent routes to affect all children
  2. Document behavior - Comment why middleware is needed
  3. Test edge cases - Ensure middleware handles missing or invalid params
  4. Keep it pure - Middleware should not have side effects
  5. Use built-in middleware - Prefer retainSearchParams and stripSearchParams over custom solutions
  6. Consider inheritance - Child routes inherit parent middleware
  7. Order matters - Place middleware in logical execution order