Server and Hybrid Rendering
Angular applications can combine server-side rendering (SSR), build-time prerendering, also known as static site generation (SSG), and client-side rendering (CSR) on a per-route basis. Use server routes to control how each route renders and keep application code compatible with server rendering and hydration.
Server routes
Choose CSR (RenderMode.Client), SSG (RenderMode.Prerender), or SSR (RenderMode.Server) for each route with ServerRoute:
import {inject} from '@angular/core';
import {PrerenderFallback, RenderMode, ServerRoute} from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{
path: 'account',
renderMode: RenderMode.Client,
},
{
path: 'products/:id',
renderMode: RenderMode.Prerender,
fallback: PrerenderFallback.Server,
async getPrerenderParams() {
const catalog = inject(ProductCatalog);
const ids = await catalog.getProductIds();
return ids.map((id) => ({id: String(id)}));
},
},
{
path: '**',
renderMode: RenderMode.Server,
},
];Keep the existing provideServerRendering(withRoutes(serverRoutes)) wiring in the server application configuration.
Follow these rules:
- Put specific paths before the
**fallback. - Keep client router paths and server route paths aligned.
- Use
RenderMode.Prerenderonly when the required data is available at build time. - Call
inject()before the firstawaitingetPrerenderParams. - Return string values with keys that exactly match each route parameter.
- Choose
PrerenderFallback.Server,Client, orNonefor paths not generated at build time. The default isServer. - Set static
headerson server routes. Setstatuson CSR or SSR routes; SSG routes do not accept a status.
Hydration
Keep provideClientHydration() in configuration shared by the browser and server bootstraps.
For Angular 22 and newer, provideClientHydration() enables incremental hydration and event replay by default. Do not add the deprecated withIncrementalHydration(). Use withNoIncrementalHydration() only for a deliberate opt-out.
Add a hydrate trigger to an @defer block when its server-rendered content should remain dehydrated until needed:
@defer (on viewport; hydrate on interaction) {
<product-reviews />
} @placeholder {
<div class="reviews-placeholder"></div>
}The hydrate trigger controls the initial server-rendered page; the regular on viewport trigger controls later client-side renders.
Keep the server and browser DOM identical:
- Do not conditionally render different templates with
isPlatformBrowser. - Avoid direct DOM insertion, removal, or movement.
- Keep HTML valid. Leave
preserveWhitespacesat its defaultfalse; if set, keep browser and server configurations consistent. - Use
ngSkipHydrationon a component host only as a temporary last resort because Angular rerenders the entire component subtree.
Server-compatible code
Do not access browser-only APIs, such as DOM, storage, or layout APIs, while rendering on the server.
Prefer:
afterNextRender,afterEveryRender, orafterRenderEffectfor browser-only work.- The
DOCUMENTinjection token instead of the globaldocument. - Separate browser and server providers behind one application-owned token.
- Angular rendering APIs instead of manual DOM manipulation.
Do not store request-specific values in module-level variables or top-level useValue providers because those values can persist across requests. Create request-specific values with a factory.
Request data and transfer cache
Use the request tokens from @angular/core:
REQUESTfor the Web APIRequest.RESPONSE_INITfor dynamic response status and headers.REQUEST_CONTEXTfor adapter-provided context.
Treat these tokens as nullable. They are null during CSR, SSG, builds, and route extraction.
In current Angular versions, keep the HTTP transfer-cache defaults. Exclude user-specific requests with withHttpTransferCacheOptions({filter}) or transferCache: false. Do not opt authenticated, credentialed, private, non-cacheable, or Set-Cookie responses into the cache without reviewing the data-leak risk. Verify the defaults before relying on them in older Angular versions.
Use PendingTasks or pendingUntilEvent() when custom asynchronous work must finish before Angular serializes the response.
Keep an explicit production allowedHosts list. Do not use a wildcard or enable trustProxyHeaders unless a trusted proxy validates and replaces forwarded headers.