Skip to main content
The TanStack Router Vite Plugin provides seamless integration with Vite, enabling automatic route generation, code splitting, and hot module replacement (HMR) for your routes.

Installation

Install the Vite plugin as a development dependency:
The @tanstack/router-plugin package provides plugins for multiple bundlers. For Vite-specific functionality, import from @tanstack/router-plugin/vite.

Basic Setup

Add the plugin to your vite.config.ts:
Place tanstackRouter() before your framework plugin (e.g., react(), vue(), solid()) in the plugins array.

Configuration

Configure the plugin with an options object:

Configuration Options

'react' | 'solid' | 'vue'
default:"react"
The framework you’re using with TanStack Router
string
default:"./src/routes"
The directory containing your route files
string
default:"./src/routeTree.gen.ts"
Where to output the generated route tree file
string
Optional prefix for route files (e.g., “route” matches “route.home.tsx”)
string
default:"-"
Files starting with this prefix will be ignored
string
A regex pattern for files to ignore
'single' | 'double'
default:"single"
Quote style for generated code
boolean
default:false
Whether to include semicolons in generated code
boolean
Automatically enable code splitting for your routes
boolean
default:false
Disable TypeScript type generation
boolean
default:false
Disable plugin logging output
boolean | string
default:false
Add file extensions to imports. Can be true for .js or a custom extension
CodeSplittingOptions
Advanced code splitting configuration (see below)

Code Splitting

The plugin supports automatic code splitting to optimize your bundle size:

Code Splitting Options

CodeSplitGroupings
Default grouping strategy for route components:
(params: { routeId: string }) => CodeSplitGroupings | undefined
Function to control splitting behavior per route. Return undefined to use defaultBehavior
Array<string>
Route properties to remove during code splitting (e.g., ['loader', 'action'])
boolean
default:true
Enable hot module replacement for code-split route components

Framework-Specific Configuration

React

Solid

Vue

Specialized Plugins

The package exports specialized plugins for specific use cases:

Route Generator Only

Only generate routes without code splitting:

Code Splitter Only

Only handle code splitting (requires pre-generated routes):

Route Auto-Import

Automatically import route files:

Environment-Specific Configuration

The plugin supports Vite’s environment API:

Hot Module Replacement (HMR)

The plugin automatically configures HMR for your routes:
  • Route components are hot-reloaded without full page refresh
  • Route configuration changes trigger route tree regeneration
  • Maintains router state during HMR updates
  • Works with code-split routes
No additional configuration required!

Using with TypeScript

The plugin generates TypeScript types automatically. Ensure the generated route tree is included in your tsconfig.json:

Alternative: Standalone Vite Plugin Package

You can also use the standalone Vite plugin package:
This package is a thin wrapper around @tanstack/router-plugin/vite. Both approaches are equivalent.

Migration from CLI

If you’re currently using the CLI (tsr watch), migrate to the Vite plugin: Before:
After:
Update your vite.config.ts:

Troubleshooting

Plugin order matters

Always place the router plugin before your framework plugin:

Routes not generating

  1. Verify routesDirectory path is correct
  2. Check that route files match the expected naming convention
  3. Ensure Vite dev server is running
  4. Check the console for plugin errors

HMR not working

  1. Verify the plugin is before your framework plugin
  2. Check that addHmr is not set to false in code splitting options
  3. Restart the Vite dev server

Type errors

  1. Ensure the generated route tree is included in tsconfig.json
  2. Restart your TypeScript server
  3. Run tsc --noEmit to check for errors

Code splitting issues

  1. Verify autoCodeSplitting is set to true
  2. Check your splitBehavior function returns valid groupings
  3. Ensure route components are exported correctly