|
| 1 | +--- |
| 2 | +title: "@react-router/{adapter}" |
| 3 | +--- |
| 4 | + |
| 5 | +# Server Adapters |
| 6 | + |
| 7 | +## Official Adapters |
| 8 | + |
| 9 | +Idiomatic React Router apps can generally be deployed anywhere because React Router adapts the server's request/response to the [Web Fetch API][web-fetch-api]. It does this through adapters. We maintain a few adapters: |
| 10 | + |
| 11 | +- `@react-router/architect` |
| 12 | +- `@react-router/cloudflare` |
| 13 | +- `@react-router/express` |
| 14 | + |
| 15 | +These adapters are imported into your server's entry and are not used inside your React Router app itself. |
| 16 | + |
| 17 | +If you initialized your app with `npx create-react-router@latest` with something other than the built-in [React Router App Server][rr-serve] (`@react-router/serve`), you will note a `server/index.js` file that imports and uses one of these adapters. |
| 18 | + |
| 19 | +<docs-info>If you're using the built-in React Router App Server, you don't interact with this API</docs-info> |
| 20 | + |
| 21 | +Each adapter has the same API. In the future we may have helpers specific to the platform you're deploying to. |
| 22 | + |
| 23 | +## `@react-router/express` |
| 24 | + |
| 25 | +[Reference Documentation ↗](https://api.reactrouter.com/v7/modules/_react_router_express.html) |
| 26 | + |
| 27 | +Here's an example with express: |
| 28 | + |
| 29 | +```ts lines=[1-3,11-22] |
| 30 | +const { |
| 31 | + createRequestHandler, |
| 32 | +} = require("@react-router/express"); |
| 33 | +const express = require("express"); |
| 34 | + |
| 35 | +const app = express(); |
| 36 | + |
| 37 | +// needs to handle all verbs (GET, POST, etc.) |
| 38 | +app.all( |
| 39 | + "*", |
| 40 | + createRequestHandler({ |
| 41 | + // `react-router build` and `react-router dev` output files to a build directory, |
| 42 | + // you need to pass that build to the request handler |
| 43 | + build: require("./build"), |
| 44 | + |
| 45 | + // Return anything you want here to be available as `context` in your |
| 46 | + // loaders and actions. This is where you can bridge the gap between your |
| 47 | + // server and React Router |
| 48 | + getLoadContext(req, res) { |
| 49 | + return {}; |
| 50 | + }, |
| 51 | + }), |
| 52 | +); |
| 53 | +``` |
| 54 | + |
| 55 | +### Migrating from the React Router App Server |
| 56 | + |
| 57 | +If you started an app with the [React Router App Server][rr-serve] but find that you want to take control over the Express server and customize it, it should be fairly straightforward to migrate way from `@react-router/serve`. |
| 58 | + |
| 59 | +You can refer to the [Express template][express-template] as a reference, but here are the main changes you will need to make: |
| 60 | + |
| 61 | +**1. Update deps** |
| 62 | + |
| 63 | +```sh |
| 64 | +npm uninstall @react-router/serve |
| 65 | +npm install @react-router/express compression express morgan cross-env |
| 66 | +npm install --save-dev @types/express @types/express-serve-static-core @types/morgan |
| 67 | +``` |
| 68 | + |
| 69 | +**2. Add a server** |
| 70 | + |
| 71 | +Create your React Router express server in `server/app.ts`: |
| 72 | + |
| 73 | +```ts filename=server/app.ts |
| 74 | +import "react-router"; |
| 75 | +import { createRequestHandler } from "@react-router/express"; |
| 76 | +import express from "express"; |
| 77 | + |
| 78 | +export const app = express(); |
| 79 | + |
| 80 | +app.use( |
| 81 | + createRequestHandler({ |
| 82 | + build: () => |
| 83 | + import("virtual:react-router/server-build"), |
| 84 | + }), |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | +Copy the [`server.js`][express-template-server-js] into your app. This is the boilerplate setup we recommend to allow the same server code to run both the development and production builds of your app. Two separate files are used here so that the main Express server code can be written in TypeScript (`server/app.ts`) and compiled into your server build by React Router, and then executed via `node server.js`. |
| 89 | + |
| 90 | +**3. Update `vite.config.ts` to compile the server** |
| 91 | + |
| 92 | +```tsx filename=vite.config.ts lines=[6-10] |
| 93 | +import { reactRouter } from "@react-router/dev/vite"; |
| 94 | +import { defineConfig } from "vite"; |
| 95 | +import tsconfigPaths from "vite-tsconfig-paths"; |
| 96 | + |
| 97 | +export default defineConfig(({ isSsrBuild }) => ({ |
| 98 | + build: { |
| 99 | + rollupOptions: isSsrBuild |
| 100 | + ? { input: "./server/app.ts" } |
| 101 | + : undefined, |
| 102 | + }, |
| 103 | + plugins: [reactRouter(), tsconfigPaths()], |
| 104 | +})); |
| 105 | +``` |
| 106 | + |
| 107 | +**4. Update `package.json` scripts** |
| 108 | + |
| 109 | +Update the `dev` and `start` scripts to use your new Express server: |
| 110 | + |
| 111 | +```json filename=package.json |
| 112 | +{ |
| 113 | + // ... |
| 114 | + "scripts": { |
| 115 | + "dev": "cross-env NODE_ENV=development node server.js", |
| 116 | + "start": "node server.js" |
| 117 | + // ... |
| 118 | + } |
| 119 | + // ... |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## `@react-router/cloudflare` |
| 124 | + |
| 125 | +[Reference Documentation ↗](https://api.reactrouter.com/v7/modules/_react_router_cloudflare.html) |
| 126 | + |
| 127 | +Here's an example with Cloudflare: |
| 128 | + |
| 129 | +```ts |
| 130 | +import { createRequestHandler } from "react-router"; |
| 131 | + |
| 132 | +declare module "react-router" { |
| 133 | + export interface AppLoadContext { |
| 134 | + cloudflare: { |
| 135 | + env: Env; |
| 136 | + ctx: ExecutionContext; |
| 137 | + }; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +const requestHandler = createRequestHandler( |
| 142 | + () => import("virtual:react-router/server-build"), |
| 143 | + import.meta.env.MODE, |
| 144 | +); |
| 145 | + |
| 146 | +export default { |
| 147 | + async fetch(request, env, ctx) { |
| 148 | + return requestHandler(request, { |
| 149 | + cloudflare: { env, ctx }, |
| 150 | + }); |
| 151 | + }, |
| 152 | +} satisfies ExportedHandler<Env>; |
| 153 | +``` |
| 154 | + |
| 155 | +## `@react-router/node` |
| 156 | + |
| 157 | +While not a direct "adapter" like the above, this package contains utilities for working with Node-based adapters. |
| 158 | + |
| 159 | +[Reference Documentation ↗](https://api.reactrouter.com/v7/modules/_react_router_node.html) |
| 160 | + |
| 161 | +### Node Version Support |
| 162 | + |
| 163 | +React Router officially supports **Active** and **Maintenance** [Node LTS versions][node-releases] at any given point in time. Dropped support for End of Life Node versions is done in a React Router Minor release. |
| 164 | + |
| 165 | +[node-releases]: https://nodejs.org/en/about/previous-releases |
| 166 | +[web-fetch-api]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API |
| 167 | +[rr-serve]: ./serve |
| 168 | +[express-template]: https://github.com/remix-run/react-router-templates/tree/main/node-custom-server |
| 169 | +[express-template-server-js]: https://github.com/remix-run/react-router-templates/blob/main/node-custom-server/server.js |
0 commit comments