Deploy a React Router site
Get started with React Router on Netlify right away by clicking the button above
React Router 7+ can be used as a framework, giving you a server and browser runtime that focuses on performance and excellent user experiences. You get a number of built-in tools to build better websites, such as nested routes, parallel data requests, and robust built-in error handling.
Deploy a React Router site
Get started with React Router on Netlify right away by clicking the button above
These features provide important benefits for React Router projects, including those built by and deployed with Netlify.
To create a React Router app and deploy it on Netlify, use our Netlify starter template with the command: npx create-react-router@latest --template netlify/react-router-template.
You’ll get all you need to deploy to Netlify, including a netlify.toml file with common build settings.
You can use the command line to scaffold a new project based on the Netlify starter template for React Router. This can streamline the process of getting your project up and running.
npx create-react-router@latest --template netlify/react-router-template.If you have an existing React Router 7+ project that isn’t deployed on Netlify and you want to change the deploy target to Netlify, install Netlify’s React Router Vite plugin and add it to your Vite config:
npm install @netlify/vite-plugin-react-routerimport { reactRouter } from "@react-router/dev/vite";import { defineConfig } from "vite";import tsconfigPaths from "vite-tsconfig-paths";// ↓ add thisimport netlifyReactRouter from "@netlify/vite-plugin-react-router";
export default defineConfig({ plugins: [ reactRouter(), tsconfigPaths(), netlifyReactRouter() // ← add this ]});import { reactRouter } from "@react-router/dev/vite";// ↓ add thisimport netlifyPlugin from "@netlify/vite-plugin-react-router";
export default { plugins: [ reactRouter(), netlifyPlugin() // ← add this ]};By default, @netlify/vite-plugin-react-router deploys your React Router app to Netlify Serverless Functions (Node.js runtime). You can optionally deploy to Netlify Edge Functions (Deno runtime) instead, keeping in mind a few considerations.
First, set the edge option to true in your Vite config:
import { reactRouter } from "@react-router/dev/vite";import { defineConfig } from "vite";import tsconfigPaths from "vite-tsconfig-paths";import netlifyReactRouter from "@netlify/vite-plugin-react-router";
export default defineConfig({ plugins: [ reactRouter(), tsconfigPaths(), netlifyReactRouter({ edge: true }) // ← deploy to Edge Functions ]});Second, you must provide an app/entry.server.tsx (or .jsx) file. Create a file with the following content:
export { default } from 'virtual:netlify-server-entry'Finally, if you have your own Netlify Serverless Functions (typically in netlify/functions) for which you’ve configured a path, you must exclude those paths to avoid conflicts with the generated React Router SSR handler:
export default defineConfig({ plugins: [ reactRouter(), tsconfigPaths(), netlifyReactRouter({ edge: true, excludedPaths: ['/ping', '/api/*', '/webhooks/*'] }) ]});On your next deploy, page renders, loaders, and actions will run on Netlify’s global edge network.
Before deploying to Edge Functions, review the Netlify Edge Functions documentation for important details:
To switch from Edge Functions back to Serverless Functions, you must:
edge: true option from your vite.config.tsapp/entry.server.tsx file (React Router will use its default Node.js-compatible entry)With the Netlify Vite plugin, your React Router dev server gives you parity with Netlify’s production environment. This gives you the same platform primitives locally that your site uses in production.
With the module enabled, you or an agent can access and build with the following Netlify features when running react-router dev:
Install the Netlify Vite plugin:
npm install -D @netlify/vite-pluginThen add it to your project’s Vite config file:
import { reactRouter } from "@react-router/dev/vite";import { defineConfig } from "vite";import tsconfigPaths from "vite-tsconfig-paths";import netlifyReactRouter from "@netlify/vite-plugin-react-router";// ↓ add thisimport netlify from "@netlify/vite-plugin";
export default defineConfig({ plugins: [ reactRouter(), tsconfigPaths(), netlifyReactRouter(), netlify() // ← add this ]});React Router introduced a stable middleware feature in v7.9.0.
To use middleware, opt in to the feature via future.v8_middleware and follow the docs.
This requires requires v2.0.0+ of @netlify/vite-plugin-react-router.
You can access the Netlify context
in your middleware by using the fully type-safe RouterContext Netlify provides:
import { netlifyRouterContext } from "@netlify/vite-plugin-react-router/serverless"// NOTE: if setting `edge: true`, import from /edge instead
import type { Route } from "./+types/home"
const logMiddleware: Route.MiddlewareFunction = async ({ request, context }) => { const country = context.get(netlifyRouterContext).geo?.country?.name ?? "unknown" console.log(`Handling ${request.method} request to ${request.url} from ${country}`)}
export const middleware: Route.MiddlewareFunction[] = [logMiddleware]
export default function Home() { return <h1>Home</h1>}Your feedback helps us improve our docs.