Skip to main content

Vite Plugin

The React Router Vite plugin integrates React Router with Vite to enable Framework Mode with server-side rendering, type generation, and optimized builds.

Installation

Basic Configuration

Add the plugin to your vite.config.ts:

Plugin Features

The Vite plugin provides:
  • Server-side rendering (SSR) - Automatic SSR setup and configuration
  • Route-based code splitting - Automatic code splitting by route
  • Type generation - TypeScript types generated from your routes
  • Development server - Hot module replacement (HMR) for routes
  • Build optimization - Optimized production builds
  • Server bundles - Split server code into multiple bundles

Configuration Options

Configure React Router behavior in react-router.config.ts:

Directory Structure

The plugin expects this structure:

Virtual Modules

The plugin creates virtual modules for internal use:
  • virtual:react-router/server-build - Server build exports
  • virtual:react-router/server-manifest - Server-side route manifest
  • virtual:react-router/browser-manifest - Client-side route manifest

Build Process

Development

Start the dev server:
The plugin:
  1. Creates a Vite dev server
  2. Sets up HMR for route modules
  3. Generates TypeScript types on file changes
  4. Handles SSR requests automatically

Production

Build for production:
The plugin:
  1. Builds the client bundle with all assets
  2. Builds the server bundle(s) for SSR
  3. Generates the route manifest
  4. Optimizes and code-splits by route
  5. Moves server-only assets to client directory

Environment API

With Vite 6+, you can enable the Environment API:
This enables:
  • Better multi-environment support
  • Improved server bundle handling
  • More efficient builds

Route Module Optimization

The plugin automatically removes server-only exports from client bundles: Server-only exports:
  • loader
  • action
  • middleware
  • headers
Client exports:
  • default (component)
  • ErrorBoundary
  • HydrateFallback
  • Layout
  • clientLoader
  • clientAction
  • clientMiddleware
  • handle
  • meta
  • links
  • shouldRevalidate

CSS Handling

CSS Code Splitting

By default, CSS is code-split by route:
Disable CSS code splitting in vite.config.ts:

CSS Modules

CSS modules are fully supported:

Source Maps

Configure source maps:
Or in vite.config.ts:

Advanced Configuration

Custom Vite Config

Combine with other Vite plugins:

Build Hooks

Run code after the build completes:

Plugin Order

The React Router plugin should generally be first:

See Also