Skip to main content
File-based routing automatically generates your route tree from your file system structure, eliminating the need to manually define routes in code.

Overview

With file-based routing, you create route files in a designated directory (typically src/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

1

Install the plugin

Install the Vite plugin (or your bundler’s equivalent):
2

Configure the plugin

Add the plugin to your Vite config:
vite.config.ts
3

Create your route files

Create route files in src/routes/ using the createFileRoute function:
src/routes/__root.tsx
src/routes/index.tsx

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

Route files should contain route configuration and data loading logic. Move complex components to separate files.
Split heavy components into .lazy.tsx files to reduce initial bundle size.
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