Sub topic 1
Client rendering, server rendering, and prerendering compared
Angular can produce an application's HTML in three places: in the browser, on a server for each request, or at build time. This chapter compares the three models so you can see what problem server-side rendering (SSR) and hydration solve. Later chapters set them up.
Prerequisites
This chapter assumes you know the following:
- Angular components, templates, and dependency injection.
- Standalone components and
ApplicationConfig. - Angular Router basics and
HttpClientbasics. - Basic Node.js and npm usage.
- How the browser builds the DOM from HTML.
The three models
Client-side rendering (CSR) is the default for an Angular CLI project. The server sends a nearly empty HTML page and a JavaScript bundle. The browser downloads and runs the bundle, Angular builds the DOM, and the user sees content.
Server-side rendering (SSR) runs the Angular application on a Node.js server for each request. The server sends complete HTML for the requested URL. The browser can paint it before any JavaScript arrives.
Prerendering, also called static site generation (SSG), runs the same render step once at build time. The output is a static HTML file per route. A web server or CDN serves these files, and no Angular code runs per request.
In every model the browser still needs the JavaScript bundle to make the page interactive. SSR and prerendering change what the user sees first. They don't remove the bundle.
What the browser receives
With CSR, the first response contains an empty application root:
<body>
<app-root></app-root>
<script src="main.js" type="module"></script>
</body>With SSR or prerendering, the same response contains rendered content inside app-root:
<body>
<app-root ng-version="20.0.0">
<h1>Products</h1>
<ul>
<li>Keyboard</li>
<li>Mouse</li>
</ul>
</app-root>
<script src="main.js" type="module"></script>
</body>The second page shows its content as soon as the HTML is parsed. The first page shows a blank screen until the bundle has loaded, run, and fetched its data.
Comparing them
| Aspect | CSR | SSR | Prerendering |
|---|---|---|---|
| HTML is produced | In the browser | On the server, per request | At build time |
| First content visible | After bundle and data load | After HTML arrives | After HTML arrives |
| Server needed at runtime | Static file host | Node.js server | Static file host |
| Data freshness | Fetched on each visit | Fetched on each request | Frozen at build time |
| Per-request cost | None | Server CPU for each render | None |
| Fits | Dashboards behind a login | Personalized or frequently changing pages | Marketing pages, docs, blogs |
Why the choice matters
Rendering on the server or at build time helps in three common cases:
- Search and link previews. Crawlers and social previews read the HTML they receive. Some crawlers run JavaScript, but many don't, or do so late. Full HTML makes your content and meta tags reliably visible.
- Perceived speed. Users see content before the bundle finishes. This usually improves Largest Contentful Paint, a measure of when the main content appears.
- Slow devices and networks. The device paints finished HTML instead of running the whole application first.
The costs are real:
- You must run and scale a Node.js server for SSR.
- Your code runs in two environments. Code that touches
windowordocumentcan fail on the server. - The rendered HTML is not interactive until Angular takes it over in the browser.
That last cost is why hydration exists. Without it, Angular would discard the server's HTML and rebuild the DOM from scratch, which causes a visible flicker and wastes the server's work. Hydration lets the browser reuse the existing DOM and attach listeners to it. The following chapters cover it.
Choosing a model
Ask these questions for each page:
- Does the content differ per user or change between requests? If so, prerendering would serve stale or wrong content. Use SSR or CSR.
- Is the page public and indexed by search engines? Prefer prerendering or SSR.
- Is the page behind a login with no search value? CSR is often enough and is the simplest to host.
A single application can mix all three models. Angular lets you pick a render mode per route, which a later chapter covers.
Exercise
A documentation site changes only when the team publishes a new release. A user account page shows data that changes every minute and is visible only after login. Which rendering model fits each page best, and why?
Show answer
Prerender the documentation pages. Their content is fixed between releases, search engines need the HTML, and static files cost nothing per request. Use client-side rendering for the account page. It is private, so search visibility doesn't matter, and its data would be stale in a prerendered file. SSR also works for the account page if you want fast first paint, but it adds server cost.
Exercise
A page is server-rendered and the user sees its content immediately. Is the page already interactive? Explain.
Show answer
No. The HTML is visible, but the Angular code that handles clicks and other events hasn't loaded and taken over yet. Interactivity arrives after the JavaScript bundle runs and hydrates the page.