Sub topic 7
Choosing a render mode for each route
This chapter assumes you have already added SSR with the Angular CLI and enabled hydration. It also assumes you know how the Angular Router defines routes. Earlier chapters compared client rendering, server rendering, and prerendering for a whole application. Here you pick one of those strategies for each route.
Why choose per route
Most applications mix page types. A marketing home page rarely changes. A product page depends on the URL and fresh data. A dashboard sits behind a login and gains little from server HTML. Angular lets each route use the strategy that fits it, so you don't have to apply one strategy to the whole app.
The choice is made in a separate server route file, usually app.routes.server.ts. The browser router in app.routes.ts stays unchanged.
The three render modes
The RenderMode enum from @angular/ssr has three values:
| Mode | When HTML is produced | Typical use |
|---|---|---|
| `RenderMode.Prerender` | At build time, once | Pages that are the same for every visitor |
| `RenderMode.Server` | On each request, on the server | Pages that depend on request data or change often |
| `RenderMode.Client` | In the browser only | Private or highly interactive pages |
Here is a server route file that uses all three:
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{ path: '', renderMode: RenderMode.Prerender },
{ path: 'products/:id', renderMode: RenderMode.Server },
{ path: '**', renderMode: RenderMode.Client },
];The server config passes this array to provideServerRendering(withRoutes(serverRoutes)). Without that call, the render modes are never read.
A way to decide
Ask two questions about each route. Does the HTML differ between users or requests? Does a crawler or a first-time visitor need meaningful HTML?
Prerendered pages are plain files, so a CDN can serve them quickly and no server work is needed per visit. Server-rendered pages cost CPU on every request and need a running Node server. Client-rendered pages cost the server nothing, but the first paint waits for JavaScript.
Prerendering routes with parameters
A route like products/:id has many possible URLs. To prerender it, tell Angular which parameter values exist by adding getPrerenderParams. It returns an array of objects, one per page.
import { inject } from '@angular/core';
import { RenderMode, ServerRoute } from '@angular/ssr';
import { ProductService } from './product.service';
export const serverRoutes: ServerRoute[] = [
{
path: 'products/:id',
renderMode: RenderMode.Prerender,
async getPrerenderParams() {
const products = inject(ProductService);
const ids = await products.listIds();
return ids.map((id) => ({ id }));
},
},
{ path: '**', renderMode: RenderMode.Client },
];At build time Angular renders one HTML file for each returned object. inject works inside getPrerenderParams because Angular runs it in an injection context. ProductService is your own service, and listIds() must return a promise of ID strings.
A URL that was not in the list, such as a product added after the build, needs a fallback. You can set fallback on the route to PrerenderFallback.Server to render it on request. The alternatives are PrerenderFallback.Client and PrerenderFallback.None.
Common errors
- Missing catch-all. If no entry matches a URL, the router may still work in the browser, but you have not chosen how that URL renders on the server. Add a
**entry and keep it last. - Prerendering personalized pages. Prerendered HTML is built once, so every visitor gets the same content. A page that shows the current user's name must not use
Prerender. - Missing parameters. A parameterized route set to
PrerenderwithoutgetPrerenderParamsfails the build. - Slow build. Each prerendered page is a full render. Thousands of product pages can make builds long, and
Servermode may suit them better.
Exercise
Exercise
A site has a public pricing page that changes once a month, an order history page that shows the logged-in user's orders, and a blog with 40 posts at blog/:slug. Choose a render mode for each and name one extra step the blog needs.
Show answer
Pricing: RenderMode.Prerender, because the HTML is identical for all visitors and can be built once. Order history: RenderMode.Client (or Server if it must return HTML at first load), because the content is per user and must not be baked in at build time. Blog: RenderMode.Prerender with getPrerenderParams returning one { slug } object per post, so Angular knows which 40 pages to build.