Sub topic 18
Building and deploying to production
This chapter assumes you have built a framework mode app with loaders, actions, and sessions. You should know Node.js and npm basics and how environment variables work.
What the build produces
Run the build from your project root:
npm run buildThe build creates a build/ directory with two parts:
build/client/holds static assets: JavaScript bundles, CSS, and any prerendered HTML. Files here have hashed names, so you can cache them for a long time.build/server/holds the server bundle. It exports a request handler, and the same handler serves every route that needs server rendering.
If ssr: false is set in react-router.config.ts, there is no build/server/ directory. You deploy build/client/ to any static host.
Running the server build
The default template includes @react-router/serve, a small production server. Start it with the script from package.json:
{
"scripts": {
"dev": "react-router dev",
"build": "react-router build",
"start": "react-router-serve ./build/server/index.js",
"typecheck": "react-router typegen && tsc"
}
}Then run it:
npm run build
npm run startBy default the server listens on port 3000. Set the PORT environment variable to change it.
Configuration for production
Secrets such as SESSION_SECRET must come from the environment at runtime. Never commit them to your repository. The session example from the previous chapters reads process.env.SESSION_SECRET, and it fails if the variable is missing. Set it on your host before the first request.
Check these before you deploy:
NODE_ENVisproduction. The session cookie'ssecureflag depends on it, and browsers only send secure cookies over HTTPS.SESSION_SECRETis a long random string, different from your development value.- Your host serves the app over HTTPS.
- The
ssrandprerendervalues inreact-router.config.tsmatch how you plan to host.
Deploying with Docker
A container works on most platforms. This multi-stage Dockerfile installs dependencies, builds, and then copies only what the server needs:
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM node:22-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --from=build /app/build ./build
EXPOSE 3000
CMD ["npm", "run", "start"]Pass SESSION_SECRET when you start the container, for example with docker run -e SESSION_SECRET=... -p 3000:3000 your-image.
Other hosting targets
| Target | What you deploy | Notes |
|---|---|---|
| Node server or container | `build/` with `react-router-serve` | Supports all features |
| Static host or CDN | `build/client/` | Needs `ssr: false`, or prerender every route |
| Serverless or edge platform | The server build plus a platform adapter | Check the platform's React Router template |
Common errors
- Blank page or 404 for assets after deploy. The host is not serving
build/client/. Serve it at the root path. - Sessions reset on each request. The cookie is
securebut the site is served over plain HTTP, orSESSION_SECRETchanges between restarts. - Server-only code in the client bundle. Keep it in
.server.tsfiles, as covered earlier. The build fails if client code imports one.
Exercise
You set ssr: false and deploy only build/client/ to a static host. A route you wrote has a loader. Does it still work, and why?
Show answer
No. With ssr: false there is no server at runtime, so a server loader cannot run. Use a clientLoader instead, or enable ssr: true and deploy the server build.