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.usersView param will be retained when navigating to child routes or within this route tree.
Retain All Params
Passtrue 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, passtrue to strip all:
Strip Specific Optional Params
Pass an array to always remove specific optional keys:Combining Middleware
You can chain multiple middleware functions:Custom Middleware
Create custom middleware for advanced transformations:Middleware Execution
Middleware runs during navigation, after validation but before the new search params are applied:- User initiates navigation with new search params
- Router validates search params using
validateSearch - Router runs search middleware in order
- Router applies final search params to URL
- 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)
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
- Keep middleware simple - Complex operations can slow down navigation
- Avoid async operations - Middleware should be synchronous
- Use memoization - Cache expensive computations
- 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
- Prepares route context and data
- Can be asynchronous
- Can redirect or throw errors
- Returns context additions
- Focused on route lifecycle
Best Practices
- Apply at the highest route - Place middleware on parent routes to affect all children
- Document behavior - Comment why middleware is needed
- Test edge cases - Ensure middleware handles missing or invalid params
- Keep it pure - Middleware should not have side effects
- Use built-in middleware - Prefer
retainSearchParamsandstripSearchParamsover custom solutions - Consider inheritance - Child routes inherit parent middleware
- Order matters - Place middleware in logical execution order