Skip to main content

Server Rendering

React Router Framework Mode includes built-in server-side rendering (SSR) support through the Vite plugin.

Overview

SSR is enabled by default in Framework Mode. The Vite plugin handles:
  • Rendering React components to HTML on the server
  • Loading data via route loader functions before rendering
  • Hydrating the client-side application
  • Handling navigation on both server and client

Entry Files

Server Entry

Create app/entry.server.tsx:

Client Entry

Create app/entry.client.tsx:

SSR Configuration

Configure SSR in react-router.config.ts:

Development Server

The dev server handles SSR automatically:
The Vite plugin:
  1. Intercepts incoming requests
  2. Loads the server build
  3. Executes route loader functions
  4. Renders the React tree to HTML
  5. Returns the HTML response

Production Server

Using @react-router/serve

The simplest way to run your SSR app:

Custom Server

Create a custom server with Express:

Node.js Adapter

Convert Node.js requests to Web Fetch API:

SPA Mode

Disable SSR for Single Page Application mode:
In SPA mode:
  • Only the root route and HydrateFallback are rendered at build time
  • An index.html file is generated
  • All navigation happens client-side
  • No server is needed in production

Server vs. Client Code

Server-Only Modules

Code that should only run on the server:
The .server.ts suffix ensures this code is excluded from client bundles.

Client-Only Code

Use client-only imports:

Streaming SSR

Use React streaming APIs for better performance:

Middleware Mode

Integrate with existing servers:
Then in your server:

Environment Variables

Environment variables are handled automatically:

Server Build Output

After building, the server output is in build/server/:

Load Context

Pass server context to loaders:
Access in routes:

See Also