File-based routing automatically generates your route tree from your file system structure, eliminating the need to manually define routes in code.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.
Overview
With file-based routing, you create route files in a designated directory (typicallysrc/routes/), and TanStack Router automatically generates the route configuration for you. This provides:
- Zero configuration - Routes are automatically discovered
- Type safety - Full TypeScript support with generated types
- Code splitting - Automatic code splitting per route
- Colocation - Keep route logic close to the route definition
Setup
File naming conventions
Standard routes
Dynamic parameters
Use$ prefix for path parameters:
Pathless routes
Use_ prefix for layout routes without adding to the path:
Route groups
Use parentheses for organizational grouping:Route options
Define route behavior using the route object:src/routes/posts/$postId.tsx
Code splitting
Separate route code from component code for better performance:src/routes/posts/$postId.tsx
src/routes/posts/$postId.lazy.tsx
Type generation
The plugin automatically generates type-safe route definitions:src/routeTree.gen.ts
Commit the generated
routeTree.gen.ts file to version control to enable type checking in CI/CD.Best practices
Keep route files focused
Keep route files focused
Route files should contain route configuration and data loading logic. Move complex components to separate files.
Use lazy loading for large components
Use lazy loading for large components
Split heavy components into
.lazy.tsx files to reduce initial bundle size.Leverage type safety
Leverage type safety
Use
Route.useParams(), Route.useSearch(), and Route.useLoaderData() for fully typed data access.Next steps
Data loading
Learn how to load data for your routes
Path params
Master dynamic route parameters