TanStack Router provides a powerful, type-safe routing system that supports both file-based and code-based routing approaches. At its core, the router matches URLs to route definitions, loads data, and renders the appropriate components.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tanstack/router/llms.txt
Use this file to discover all available pages before exploring further.
How Routing Works
The router operates by matching the current browser location against a tree of route definitions. Each route can:- Define a path pattern to match against
- Load data before rendering
- Validate and parse URL parameters
- Render components with full type safety
Route Trees
Routes are organized in a hierarchical tree structure, where child routes inherit context from their parents:Creating a Router
The router is created with a route tree and various configuration options:Key Router Options
The route tree that defines all routes in your application.
The base path for the entire router, useful for mounting at a subpath.
Custom history implementation. Defaults to
createBrowserHistory().false- No preloading'intent'- Preload on hover or touch'viewport'- Preload when in viewport'render'- Preload when rendered
Whether route matching should be case-sensitive.
How to handle trailing slashes in URLs:
'always'- Add trailing slash if not present'never'- Remove trailing slash if present'preserve'- Keep as-is
'root'- Show not found at root level'fuzzy'- Show not found at deepest matching parent
Route Matching
The router uses a sophisticated matching algorithm that:- Parses the pathname into segments
- Matches static segments exactly
- Extracts path parameters from dynamic segments
- Handles wildcards for catch-all routes
- Validates search params using validators
- Selects the best match when multiple routes could match
Path Patterns
Router State
The router maintains state that includes:Navigation Lifecycle
When navigating to a new route, the router:- Matches routes against the new URL
- Validates search and path params on each matched route
- Calls
beforeLoadhooks in sequence - Loads data via
loaderfunctions in parallel - Updates state and renders components
- Handles errors through error boundaries
Router Events
Subscribe to navigation lifecycle events:onBeforeNavigate- Before URL changesonBeforeLoad- Before data loading startsonLoad- After data loading completesonResolved- After route is resolvedonRendered- After components render
Protocol Allowlist
For security, the router validates URL protocols:Rewriting URLs
Advanced use cases can rewrite URLs between the browser and router:Best Practices
Use Type-Safe Routing
Use Type-Safe Routing
Enable strict TypeScript settings and use route type helpers for full type safety across params, search, and context.
Structure Routes Hierarchically
Structure Routes Hierarchically
Organize routes to reflect your UI hierarchy. Child routes inherit context and layouts from parents.
Configure Preloading
Configure Preloading
Set
defaultPreload: 'intent' to automatically preload routes when users hover over links.Handle Errors Gracefully
Handle Errors Gracefully
Define
errorComponent on routes to catch and display errors in a user-friendly way.Next Steps
Routes
Learn how to define and configure routes
Navigation
Navigate between routes programmatically
Path Params
Extract and validate path parameters
Search Params
Work with URL search parameters