Full answer
In a client-only app the browser downloads JavaScript, runs it, then builds the page. With server-side rendering (SSR) the server runs the same Angular code and sends finished HTML. The page shows content early (better Largest Contentful Paint) and search engines see real content. Static prerendering (SSG) does the same at build time for pages that rarely change.
Without hydration the browser would destroy that HTML and render again, which flashes and wastes work. With provideClientHydration() Angular walks the existing DOM, matches it against the component tree, and reuses the nodes. It also restores state sent by the server. withEventReplay() records user clicks made before hydration finishes and replays them afterwards.
The server also uses HttpClient. With hydration enabled, GET responses are cached and transferred to the browser in the page, so the browser does not repeat the same calls on start-up. TransferState does the same for your own data.
In Angular 19 and 20 you choose how each route renders with ServerRoute definitions: RenderMode.Prerender, RenderMode.Server, or RenderMode.Client.
What goes wrong:
- Using
window,document, orlocalStoragedirectly. These do not exist on the server. UseafterNextRender,isPlatformBrowser, or injectDOCUMENT. - Changing the DOM by hand (
innerHTML, jQuery-style code, some third-party widgets). The server and client DOM differ, so Angular raises a hydration mismatch error (NG0500 series). Opt such a component out withngSkipHydration. - Invalid HTML nesting, such as a
divinside ap, which the browser repairs differently from the server output. - Values that differ per environment, such as
Date.now()or random numbers, rendered into the template.
Check by opening the browser console in development mode, which logs hydration mismatches and shows the first differing node.
// file: app.config.ts
import { provideHttpClient, withFetch } from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient(withFetch()), provideClientHydration(withEventReplay())],
};
// file: app.routes.server.ts
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{ path: '', renderMode: RenderMode.Prerender },
{ path: 'account', renderMode: RenderMode.Client },
{ path: '**', renderMode: RenderMode.Server },
];
// file: saved-theme.component.ts
import { Component, afterNextRender, signal } from '@angular/core';
@Component({
selector: 'app-saved-theme',
template: `<p>Theme: {{ theme() }}</p>`,
})
export class SavedThemeComponent {
readonly theme = signal('light'); // Same value on server and client at first render.
constructor() {
// Runs only in the browser, after the first render, so localStorage is safe.
afterNextRender(() => {
this.theme.set(localStorage.getItem('theme') ?? 'light');
});
}
}Angular 20.3 with @angular/ssr 20. In a CLI project the server entry files (`main.server.ts`, `server.ts`) are generated by `ng add @angular/ssr`. The code shown is the part you write yourself.
Key points
- SSR sends HTML for fast first paint and SEO. Hydration reuses that DOM instead of rebuilding it.
- Enable with `provideClientHydration(withEventReplay())` and the SSR build setup (`ng add @angular/ssr`).
- Browser-only code goes in `afterNextRender` or behind `isPlatformBrowser`.
- Manual DOM changes and invalid HTML are the usual mismatch causes.
Common mistakes
- Reading `window.innerWidth` in a constructor, which crashes on the server.
- Rendering `new Date()` or random values in the template, so server and client text differ.
- Treating SSR as a pure performance feature. It adds server cost and complexity, and often only SEO or first paint justify it.
Follow-ups
- How does the HTTP transfer cache avoid duplicate requests?
- When would you choose prerendering over per-request SSR?
- How would you debug an NG0500 mismatch error?
Tips
- Describe the full path: server HTML, then hydrate, then event replay. Candidates who stop at "SSR is good for SEO" sound junior.
- Mention how you keep browser-only code out of the server path.
- Be honest about costs: server hosting, cache strategy, and extra debugging.
Evaluation rubric
- Explains hydration versus re-rendering.
- Lists realistic mismatch causes with fixes.
- Knows render modes and the HTTP transfer cache.