Server-side rendering and hydration
Rendering an Angular app's first paint on the server, and handing off to the client without throwing that work away.
Server-side rendering and hydration
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A pure client-rendered Angular app sends the browser a nearly-empty HTML shell — the real content only appears once JavaScript downloads, parses, and runs, which costs real time on a slow connection or device.
- Server-side rendering runs the same Angular app on the server first, sending the browser real, already-rendered HTML — visible content immediately, before any JavaScript has even arrived.
- Non-destructive hydration reuses that server-rendered DOM directly once the client-side app takes over, instead of the older approach of throwing it away and re-rendering everything from scratch.
What a client-rendered app actually sends the browser first
Every example so far in this roadmap has assumed the browser downloads a small HTML shell, then JavaScript takes over and renders everything from there. Open the raw HTML a client-rendered Angular app sends before any script runs, and there’s almost nothing in it — a root element, waiting. The real content, everything a user actually came to see, doesn’t exist in that HTML at all; it only appears once the JavaScript bundle has been downloaded, parsed, and executed. On a fast connection and a fast device, that gap is imperceptible. On a slow connection, an older phone, or a search engine crawler that doesn’t execute JavaScript the way a real browser does, that gap is a real, visible, or functionally significant problem.
Rendering the same app once on the server, first
// app.config.server.ts
export const serverConfig: ApplicationConfig = {
providers: [provideServerRendering(), /* ... */],
};
Server-side rendering runs your actual Angular app — the same components, the
same templates — on the server, producing real, fully-rendered HTML before it
ever reaches the browser. provideServerRendering() wires this into the
Angular CLI’s built-in SSR support, which runs your app in a Node environment
on request, and sends back genuine content-filled HTML. A user (or a search
engine crawler) sees the real page immediately, with no JavaScript execution
required just to see something.
Hydration: not throwing that work away
Sending pre-rendered HTML solves the first-paint problem, but raises an obvious follow-up: the client-side Angular app still needs to boot eventually, to make the page interactive (click handlers, bindings, everything this roadmap has covered). The crude way to handle that — what earlier approaches to SSR effectively did — is to let the client-side app render everything again from scratch once it boots, discarding the server’s HTML entirely. That wastes the server’s work and typically causes a visible flash as the real DOM gets replaced.
Non-destructive hydration is current Angular’s answer: instead of re-rendering, the client-side app reuses the exact DOM elements the server already produced, attaching event listeners and wiring up reactivity to what’s already there. The visible content never flashes or gets rebuilt — it was already correct; hydration just makes it interactive.
The part SSR adds real complexity to: data fetching
SSR isn’t free of its own genuinely tricky considerations, chief among them: whatever data a component needs has to be fetched during the server render for that content to actually appear in the initial HTML, and ideally that same data shouldn’t be re-fetched again once the client takes over — doing so would mean an extra, redundant request right after the page already had the data it needed. This is one of the more involved parts of setting SSR up correctly in a real app, and worth expecting rather than being surprised by.
This lesson’s video covers the current provideServerRendering setup and
hydration directly, on a recent Angular version; the linked “deeper” video, from
a long-running Angular-focused podcast, goes deeper into the SSR/hydration
story if you want more time spent on the topic than this roadmap’s tighter pick
covers.
Check yourself
Without SSR, what does a user on a slow connection actually see in the time between the HTML document arriving and Angular's JavaScript finishing its download and execution?
Essentially nothing — a near-empty shell, since a pure client-rendered app's initial HTML contains little more than a root element waiting for Angular to boot and render into it. The real content doesn't exist in the HTML at all until the client-side JavaScript has run.
Why does "non-destructive" matter in non-destructive hydration — what was the alternative it's contrasted against?
The alternative — what earlier SSR approaches effectively did — was to throw away the server-rendered DOM entirely once the client-side app booted, and re-render the whole page from scratch, client-side, as if SSR had never happened. That wastes the server's rendering work and causes a visible flash as the real DOM gets replaced. Non-destructive hydration instead reuses the exact DOM the server already produced, attaching Angular's event listeners and reactivity to the existing elements rather than rebuilding them.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.