Routing
TanStack Router provides a powerful and flexible routing system that supports both file-based and code-based routing patterns. At its core, routing is about matching URL patterns to route definitions and rendering the appropriate components.Why Routing Matters
Routing is the backbone of any single-page application (SPA). TanStack Router takes routing beyond simple URL matching by providing:- Type-safe navigation - Full TypeScript inference for paths, params, and search
- Nested routing - Parent-child route relationships with shared layouts
- Automatic code splitting - Route-level lazy loading out of the box
- Framework agnostic core - Works with React, Solid, and other frameworks
Route Tree Structure
Routes in TanStack Router are organized as a tree structure, with each route potentially having child routes. This creates a hierarchy that matches your application’s UI structure./posts- Shows the posts list layout/posts/123- Shows post 123 detail inside the posts layout
File-Based Routing
TanStack Router excels at file-based routing, where your file structure automatically defines your routes. This is powered by the route generator.File-based routing requires the TanStack Router plugin for your bundler (Vite, Webpack, etc.)
File Route Conventions
File routes use a naming convention to define route structure:$prefix indicates a path parameter_prefix creates a layout route without adding to the path.separates nested route segmentsindexcreates the default route for that level
Creating File Routes
File routes are created usingcreateFileRoute:
createFileRoute must match the file location for type safety.
Path Matching
TanStack Router matches URLs to routes using several strategies:Static Paths
Exact string matches:Path Parameters
Dynamic segments that capture values:useParams() or in route options.
Optional Parameters
Parameters that may or may not be present:{- syntax makes the parameter optional.
Splat Routes
Capture remaining path segments:_splat parameter.
Route Ranking
When multiple routes could match a URL, TanStack Router uses a ranking system:- Static segments rank highest
- Path parameters rank in the middle
- Splat routes rank lowest
/posts/new matches before /posts/$postId.
Basepath
Mount your entire router at a subpath:/posts becomes /app/posts.
Trailing Slashes
Control how trailing slashes are handled:Case Sensitivity
By default, routes are case-insensitive. Make them case-sensitive globally:Best Practices
Use file-based routing for scalability
Use file-based routing for scalability
File-based routing keeps your routes organized and co-locates route logic with route definitions. It scales better than managing a large route tree manually.
Keep route trees shallow when possible
Keep route trees shallow when possible
Deep nesting can make navigation complex. Consider flattening routes with layout routes (underscore prefix) instead of deeply nested paths.
Use meaningful route names
Use meaningful route names
Route IDs are generated from paths. Clear, semantic paths lead to better type inference and developer experience.
Next Steps
Routes
Learn about route options, lifecycle, and configuration
Navigation
Discover how to navigate between routes
Type Safety
Explore TanStack Router’s type-safe routing features
Search Params
Master type-safe search parameter handling