Sub topic 1
Client rendering, server rendering, and prerendering compared
Prerequisites
This chapter assumes you already know the following:
- Angular components, templates, and dependency injection.
- Standalone components and application configuration with
ApplicationConfig. - Angular Router basics.
HttpClientbasics.- Basic Node.js and npm usage.
- How the browser builds the DOM from HTML.
You do not need to have used @angular/ssr yet. This chapter builds the mental model that the later chapters rely on. It has no setup steps. The next chapter adds SSR to a project.
Why the rendering model matters
Every Angular application ends up as DOM nodes in a browser. The question is where and when the HTML for those nodes is produced. Three models answer it differently:
- Client-side rendering (CSR) builds the HTML in the browser, after the JavaScript has loaded.
- Server-side rendering (SSR) builds the HTML on a server for each request, then sends it to the browser.
- Prerendering, also called static site generation (SSG), builds the HTML once at build time and serves it as a static file.
Each model changes what the user sees first, how much work the server does, how fresh the content is, and what search engines and link previews can read. None is best everywhere. Many real applications mix all three, route by route. A later chapter covers that choice in detail.
Client-side rendering
A default Angular application created without SSR uses CSR. The server returns a small HTML shell. The shell contains an empty root element such as <app-root></app-root>, plus script tags.
<!doctype html>
<html lang="en">
<head>
<title>Shop</title>
<base href="/" />
</head>
<body>
<app-root></app-root>
<script src="main.js" type="module"></script>
</body>
</html>The browser then does the following work:
- Downloads and parses the HTML shell.
- Downloads, parses, and runs the JavaScript bundles.
- Bootstraps Angular, which creates components and renders their templates.
- Sends any
HttpClientrequests the components make. - Updates the DOM when the responses arrive.
Until step 3 finishes, the user sees a blank page. Until step 5 finishes, they see a page without data.
CSR has real strengths. Hosting is cheap, because the output is only static files that any CDN can serve. There is no server code to run, scale, or secure. After the first load, navigation is fast, because the router swaps views without asking the server for new documents.
Its weaknesses come from the same design. The first meaningful paint waits for JavaScript to download and run, which is slow on weak devices and slow networks. Crawlers and link-preview bots that do not run JavaScript see an empty shell. Crawlers that do run it may index the page later or less reliably.
Server-side rendering
With SSR, the server runs your Angular application in Node.js when a request arrives. Angular renders the component tree for the requested URL into an HTML string. The server sends that string, already filled with content, as the response.
The browser can paint this HTML immediately, before any JavaScript has loaded. The page is visible, but it is not yet interactive. Clicks do nothing until the JavaScript arrives and Angular starts in the browser.
The sequence looks like this:
The last step in the diagram is hydration. Instead of discarding the server's DOM and building a new one, Angular in the browser attaches to the existing DOM and reuses it. Chapter 4 covers how that works. For this chapter, remember that SSR alone produces the first paint, while hydration produces interactivity.
What the server does during rendering
The Angular server runtime is not a browser. It runs your application code in Node.js, using a server-side DOM implementation. Several consequences follow:
- Browser globals such as
window,document, andlocalStorageare not available as they are in a real browser. Code that touches them directly can fail on the server. Chapter 5 covers safe patterns. - Rendering waits for the application to become stable. Angular tracks pending work such as
HttpClientrequests and router navigation, and serializes the HTML after that work finishes. Data loaded in a component's initialization is therefore part of the output. - Rendering is per request. Each request gets its own application instance, so state must not leak between requests through module-level variables.
Strengths and costs of SSR
SSR improves the time to first content, because the HTML arrives ready to paint. It gives crawlers and link previews complete markup. It can also help users on slow devices, because the first view does not depend on their CPU running a large bundle.
The costs are operational. You need a running Node.js server, or a platform that runs one for you. Every request spends server CPU on rendering. The time to first byte (TTFB) increases, because the server must finish rendering, including data fetching, before it sends the response. A slow API makes the whole page slow. You also have two environments to keep your code correct in.
Prerendering
Prerendering runs the same server rendering process, but once, during the build. The output is a static HTML file for each route, such as index.html or about/index.html. A web server or CDN delivers those files without running Angular.
Compared with SSR, prerendering has these properties:
- No per-request rendering cost, so TTFB is as low as static hosting allows.
- No Node.js server is needed at runtime for those routes.
- Content is frozen at build time. If the data changes, you must rebuild and redeploy.
- Every URL must be known at build time. A route like
products/:idneeds the list of ids to render. - The HTML cannot depend on the request, for example on cookies, headers, or the logged-in user.
Prerendered pages still hydrate in the browser, just like server-rendered ones. The difference is only when the HTML was produced.
Prerendering suits pages whose content is the same for every visitor and changes rarely: a home page, documentation, a marketing page, a blog post. It does not suit a personalized dashboard or a page showing live stock levels.
Comparing the three models
| Aspect | CSR | SSR | Prerendering (SSG) |
|---|---|---|---|
| HTML produced | In the browser | On the server, per request | At build time |
| First paint | After JavaScript runs | When HTML arrives | When HTML arrives |
| TTFB | Very low | Higher, includes rendering | Very low |
| Content freshness | Fetched at runtime | Fresh on each request | Fixed until next build |
| Runtime server | Static files only | Node.js required | Static files only |
| Request-specific content | Fetched by the client | Available during rendering | Not available |
| Crawler-readable HTML | Empty shell | Complete | Complete |
| Server cost | Lowest | Highest | Low |
Two columns look alike on first paint, but they differ on freshness and cost. Choose between SSR and prerendering by asking whether the HTML can be the same for everyone until the next deploy. If yes, prerender. If no, use SSR.
Choosing between the models
The following questions guide the decision for a given route:
- Does the route need to be indexed or previewed? If not, such as a page behind a login, CSR is often enough.
- Does the content depend on the request or change often? If yes, SSR. If no, prerender.
- Can you run a Node.js server? If not, use CSR and prerendering only.
- Is the first paint on slow devices a priority? SSR or prerendering help here.
Angular lets you answer these per route rather than for the whole application. You declare a render mode for each route, so a prerendered home page, a server-rendered product page, and a client-rendered account area can live in one project. Chapter 7 shows the configuration.
Common misunderstandings
SSR does not make the page interactive. The HTML paints early, but event handlers exist only after the JavaScript runs. A user who clicks a button in that gap either gets no response or, with event replay, has the click replayed later. Chapter 10 covers replay.
SSR does not replace the client application. The whole Angular application still ships to the browser and runs there. SSR adds a rendered first view in front of it. Bundle size is largely unchanged.
Prerendering is not a separate feature from SSR. It uses the same renderer. A project set up for SSR can prerender some routes and server-render others.
Server rendering does not remove the need for browser-safe code. Code that works in a CSR application can fail once it also runs in Node.js. The typical failures are direct references to window or document, and code that assumes the DOM already exists at construction time.
Duplicate data fetching is a default cost. Without extra configuration, the server fetches data to render, and then the browser fetches the same data again after bootstrapping. Chapter 6 shows how the HTTP transfer cache removes this repeat.
How the pieces connect
The rest of this tutorial builds on the three models in this order:
- Setup adds the
@angular/ssrpackage and the server entry point (chapters 2 and 3). - Hydration lets the browser reuse the server's DOM (chapter 4).
- Platform-safe code and transfer of server data keep both environments consistent (chapters 5 and 6).
- Render modes let each route pick CSR, SSR, or prerendering (chapter 7).
- Diagnosis, opt-outs, event replay, and incremental hydration refine the behavior (chapters 8 to 11).
- Measurement tells you whether the added complexity pays off (chapter 12).
Summary
CSR builds the page in the browser and gives you the simplest hosting but the slowest first content. SSR builds the page on the server for each request, which gives early content and fresh data at the cost of a running server and a higher TTFB. Prerendering builds the page once at build time, which gives early content and static hosting, but only for routes whose HTML is the same for every visitor. Angular can combine the three in one application, and it hydrates server-produced HTML in the browser instead of rebuilding it.
Exercise
A news site has three kinds of pages: a home page that editors update once a day and that is the same for all visitors, an article page whose view counter and recommended articles change on every request, and a reader settings page behind a login that no search engine should see. Which rendering model fits each page, and why?
Show answer
Home page: prerendering. The HTML is identical for all visitors and changes only daily, so a rebuild each day produces static files with the lowest TTFB and no per-request cost. Article page: SSR. The content depends on live data and changes per request, so it must be rendered on the server each time while still giving crawlers complete HTML. Settings page: CSR. It is private, needs no indexing, and depends on the logged-in user, so a client-rendered page avoids server cost and keeps the server simple.
Exercise
A learner says, "With SSR the page is fully working as soon as the HTML arrives." What is wrong with this statement, and what does the user experience in the gap before the JavaScript has loaded?
Show answer
SSR delivers rendered HTML, so the content is visible, but the event handlers and application logic exist only after the JavaScript downloads, bootstraps, and hydrates. In the gap the page looks complete but is not interactive. Clicks may do nothing, unless event replay is enabled, in which case Angular records them and replays them once hydration completes.
Exercise
Name two reasons that a route like products/:id is a poor fit for prerendering unless extra information is supplied.
Show answer
First, prerendering needs every URL at build time, and :id is a parameter, so the build must be given the full list of ids to render. Second, prerendered content is frozen until the next build, so product data that changes (price, stock) would become stale, and the HTML cannot vary by request.