What SSR buys, and what it costs
Rendering on the server sends HTML with content in it. That helps first meaningful paint on slow devices, and it gives crawlers and link unfurlers something to read without executing your app.
The cost is that you now run your UI code in two environments with different capabilities, and you own a server. Before going further, be honest about which problem you have — many "we need SSR" conversations are really about a slow bundle or a missing meta tag.
The two-bundle model
This is the part that surprises people: SSR is two builds from one source tree.
vite build # client bundle -> dist/client
vite build --ssr src/entry-server.js # server bundle -> dist/server
// src/entry-client.js — runs in the browser, attaches to existing markup
hydrateRoot(document.getElementById('app'), <App />)
// src/entry-server.js — runs in Node, returns a string
export function render(url) {
return renderToString(<App url={url} />)
}
The request flow: server calls render, injects the HTML string into a template alongside the client bundle's script tags, and sends it. The browser paints the markup, then the client bundle loads and hydrates — walking the existing DOM and attaching event handlers rather than rebuilding it.
Until hydration finishes the page looks interactive but is not. Buttons visible in the first paint do nothing yet, which is a real user-facing consequence of choosing SSR.
Code that cannot run on the server
There is no window, document, or localStorage in Node. Module-level access to any of them crashes the server render:
// crashes during SSR — runs at import time
const theme = localStorage.getItem('theme')
// safe — runs only in the browser, after mount
useEffect(() => {
setTheme(localStorage.getItem('theme'))
}, [])
Vite gives you import.meta.env.SSR to branch, and ssrLoadModule in middleware mode so the dev server can transform server code with HMR. Both are useful; neither removes the need to know which environment a line of code runs in.
ssr.noExternal
By default Vite leaves dependencies external during an SSR build — Node requires them from node_modules at runtime, which keeps the server bundle small and startup fast.
That default breaks for some packages. ssr.noExternal forces a dependency to be bundled and processed by Vite instead:
export default defineConfig({
ssr: {
noExternal: ['some-esm-only-ui-kit'],
},
})
| Situation | Setting |
|---|---|
| Plain CommonJS or dual-published package | Leave external (default) |
| Package ships syntax or imports Node cannot load directly | ssr.noExternal |
| Package imports CSS or assets that need Vite's pipeline | ssr.noExternal |
| Package must be a single shared instance with the app | ssr.noExternal, and check for duplicates |
The symptom that sends you here is usually an unexpected-token or unknown-extension error thrown by Node at server start, naming a file inside node_modules.
Hydration mismatches
A mismatch means the server HTML and the client's first render disagree. React warns, discards the affected markup, and re-renders on the client — so you lose the performance benefit you built SSR for, and can flash different content.
Usual causes:
| Cause | Why it differs |
|---|---|
Date.now(), new Date(), timezone formatting | Server clock and locale differ from the visitor's |
Math.random(), generated ids | Different value per render |
Reading window, matchMedia, or localStorage during render | Absent on the server, present on the client |
| User or session state available on only one side | Server rendered anonymous, client knows the user |
| Invalid HTML nesting | The browser repairs the tree, so the DOM no longer matches |
That last one catches people: a <div> inside a <p> gets restructured by the parser, and the mismatch is reported on markup you wrote correctly-looking code for.
The fix is almost always to make the first client render identical to the server render, then update in an effect. Environment-dependent output belongs after mount, not during render.
The security-adjacent version of this is worth flagging: if you render per-user content on the server, be sure your HTML is not cached by a CDN or shared proxy. A response containing one user's data cached at the edge is served to the next visitor. Per-user HTML needs private, no-store caching, and it is an easy mistake to make because the same header config works fine for an anonymous page.
When to use a framework instead
Hand-rolled SSR means you own routing, data loading, the two-build pipeline, streaming, per-route caching, error boundaries on the server, and deployment of a live Node process. That is a substantial ongoing surface.
| Situation | Reasonable choice |
|---|---|
| Dashboard behind a login, SEO irrelevant | Client-only SPA. Do not add SSR. |
| Content site, mostly static pages | Static generation or a static site generator |
| Marketing pages needing crawlable content and fast first paint | A framework such as Next or Remix |
| Complex per-request rendering with data loading per route | A framework |
| A specific, contained rendering need in an existing app | Vite SSR directly |
Vite's SSR support is intentionally low-level — it is the primitive that frameworks build on. Reaching for it directly is right when you need control and are prepared to maintain the surrounding machinery. If your requirement is "SEO and fast first paint for a normal app," a framework has already solved the parts you have not thought of yet, and choosing it is engineering judgement rather than a lack of ambition.
Exercise: A page renders correctly on the server but React logs a hydration warning and the visible text changes right after load. The component displays a formatted timestamp. Explain the mechanism and the change you would make.