reactRouter
ThereactRouter Vite plugin enables Framework Mode with type-safe routing, automatic code splitting, and server-side rendering.
Import
Usage
Configuration
ThereactRouter plugin does not accept direct options. Instead, configuration is managed through:
- react-router.config.ts - Application-level configuration
- routes.ts - Route structure and organization
- Vite config - Build and dev server settings
Configuration Files
react-router.config.ts
react-router.config.ts
See react-router.config.ts for application configuration options including:
- App directory location
- Build output settings
- SSR and prerendering
- Server bundles
- Future flags
routes.ts
routes.ts
See routes.ts for route configuration including:
- Route definitions with
route(),index(),layout() - File-system routing with
flatRoutes() - Nested route hierarchies
- Custom route IDs
vite.config.ts
vite.config.ts
Standard Vite configuration applies:
Features
ThereactRouter plugin provides:
Automatic Type Generation
Generates TypeScript types for your routes in.react-router/types/:
Code Splitting
Automatically splits routes into separate chunks for optimal loading:- Each route module is a separate chunk
- Shared dependencies are extracted
- Critical CSS is inlined
Dev Server
Provides a development server with:- Hot Module Replacement (HMR)
- Fast refresh for route modules
- Server-side rendering in development
- Automatic route discovery
Production Build
Optimizes your application for production:- Client and server bundles
- Asset manifest generation
- CSS extraction and optimization
- Tree shaking of unused exports
Common Patterns
Basic Setup
import { reactRouter } from "@react-router/dev/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [reactRouter()],
});
Multiple Server Bundles
Configure server bundles inreact-router.config.ts:
Custom Base Path
Prerendering Static Pages
Environment Variables
Vite’s environment variable system works automatically:TypeScript
The plugin automatically generates types. Ensure yourtsconfig.json includes:
Plugin Order
ThereactRouter plugin should be placed first in most cases:
Some plugins like
cloudflareDevProxy must come before reactRouter.