Sub topic 20
Choosing SSR, prerendering, or SPA mode
This chapter assumes you can read app/routes.ts, write a loader and an action, and know the difference between code that runs on a server and code that runs in a browser. You choose how your app is rendered with two settings in react-router.config.ts: ssr and prerender. The route modules you write stay almost the same in every mode.
The three strategies
- Server-side rendering (SSR) runs a Node server. Each request is matched to routes, the loaders run, and the server returns HTML. The browser then hydrates the page, which means React attaches event handlers to the existing HTML.
- Prerendering produces HTML files at build time for URLs you list. Loaders run once during the build, not on every request.
- SPA mode produces a single
index.htmlshell. All data loading happens in the browser, so the output is plain static files.
| Setting | Server needed at runtime | `loader` and `action` | Best for |
|---|---|---|---|
| `ssr: true` (default) | Yes | Run on the server per request | Personalized or frequently changing data |
| `ssr: true` with `prerender` | Yes, for unlisted paths | Run on the server. Listed paths also run once at build time. | Mostly dynamic apps with a few static pages |
| `ssr: false` with `prerender` | No | Server loaders run at build time for listed paths only | Marketing sites, docs, blogs |
| `ssr: false`, no `prerender` | No | Not allowed. Use `clientLoader` and `clientAction`. | Apps behind a login on static hosting |
Mixing SSR and prerendering
The default is ssr: true. Adding prerender keeps the server and also writes static HTML for the listed paths:
import type { Config } from "@react-router/dev/config";
export default {
ssr: true,
prerender: ["/", "/about"],
} satisfies Config;At build time React Router renders / and /about. It runs their loaders and saves the HTML and the loader data to build/client. Requests for those paths are then served as static files. Every other path still reaches the server. This costs little, and it makes the pages that never change cheap to serve.
Fully static output
Set ssr: false and keep prerender to get a site with no server:
import type { Config } from "@react-router/dev/config";
export default {
ssr: false,
prerender: ["/", "/about", "/contacts"],
} satisfies Config;Server loaders still work here, but only on the prerendered routes, and they run at build time. A loader that reads a database runs when you run react-router build. The output will not change until you build again.
Dynamic URLs such as /contacts/:contactId cannot be listed by hand if there are many. Pass an async function instead. It receives getStaticPaths, which returns every path in your route config that has no dynamic segments:
import type { Config } from "@react-router/dev/config";
import { getContacts } from "./app/contacts.server";
export default {
ssr: false,
async prerender({ getStaticPaths }) {
const contacts = await getContacts();
return [
...getStaticPaths(),
...contacts.map((c) => `/contacts/${c.id}`),
];
},
} satisfies Config;SPA mode
With ssr: false and no prerender, the build renders only the root route to index.html. That page shows the root HydrateFallback while the browser loads the JavaScript and runs your client loaders. A route in this mode uses clientLoader instead of loader:
import type { Route } from "./+types/detail";
export async function clientLoader({ params }: Route.ClientLoaderArgs) {
const res = await fetch(`/api/contacts/${params.contactId}`);
if (!res.ok) {
throw new Response("Contact not found", { status: 404 });
}
return { contact: await res.json() };
}
export default function ContactDetail({ loaderData }: Route.ComponentProps) {
return <h1>{loaderData.contact.name}</h1>;
}Your data now comes from a separate API, because the static host has no server code. The build fails if a route in SPA mode exports a server loader or action. This is intentional, because that code could never run.
How to decide
Ask these questions in order:
- Do you need cookies, sessions, or user-specific data on the server? Then use SSR. The sessions chapter relies on this.
- Could the same HTML be served to every visitor and rebuilt on each deploy? Then prerender.
- Are you limited to static hosting, with an existing API elsewhere? Then use SPA mode.
Edge cases and common errors
- A prerendered page is frozen at build time. If the data changes, you must rebuild and redeploy.
- Prerendered routes cannot read request cookies or headers in a meaningful way, because no real request exists during the build.
- In SPA mode, search engines and link previews see only the shell. Pages that need good SEO should be prerendered or server rendered.
- Static hosts must serve
index.htmlfor unknown paths in SPA mode. Otherwise a browser refresh on/contacts/3returns a 404 from the host. - Prerendering a path that no route matches fails the build.
Exercise
Your site is a blog with 200 posts stored in a database. You deploy to a static file host and have no server. Which ssr and prerender settings do you use, and how do you cover every post URL?
Show answer
Set ssr: false and give prerender an async function. The function fetches the posts, then returns [...getStaticPaths(), ...posts.map((p) => /blog/${p.slug})]. The server loader for each post runs at build time, so the output is plain static files. New posts appear only after the next build.