← Back to Angular, page 9

angular-0179 · Medium mid

What was Angular Universal, how did server-side rendering change in Angular 17 to 20, and what does the modern setup look like?

Angular Universal was the separate project (@nguniversal) for server-side rendering. In Angular 17 it moved into the CLI as @angular/ssr, with the application builder, built-in prerendering and non-destructive hydration. Later versions added event replay, incremental hydration, and route-level render modes (server, client, prerender), so one app can mix SSR, SSG and CSR.

Full answer

Server-side rendering (SSR) means the server sends finished HTML for the first view, so users and search engines see content before the JavaScript loads. In the Angular world this was called Angular Universal.

History in short:

  • Early versions: Universal was a separate project and a separate set of packages (@nguniversal/express-engine and others). You wrote a server.ts file, an extra AppServerModule, and extra build targets.
  • Hydration: before Angular 16, the browser threw the server HTML away and rendered again, which caused a flash and wasted work. Angular 16 introduced hydration as a preview, and Angular 17 made it stable. provideClientHydration() reuses the server DOM.
  • Angular 17: Universal was folded into the CLI. ng new --ssr and ng add @angular/ssr set it up. The new application builder (esbuild and Vite) builds browser and server code, and supports prerendering (static site generation) at build time.
  • Angular 18 and 19: event replay (clicks made before hydration finished are replayed), incremental hydration with @defer (hydrate on viewport), route-level render modes in the server routes file (RenderMode.Server, Client or Prerender) and a new request handling API (AngularNodeAppEngine, AngularAppEngine) that also runs on edge platforms. Several of these were preview features and became stable by Angular 20.

What the modern setup has:

  1. @angular/ssr and @angular/platform-server.
  2. app.config.server.ts that merges the app config with provideServerRendering().
  3. app.routes.server.ts with a render mode for each route: Prerender for pages that rarely change, Server for personal pages, Client for dashboards behind login.
  4. provideClientHydration(withEventReplay()) in the browser config.
  5. TransferState (automatic for HttpClient GET requests) so data is not fetched twice.

Rules of thumb: use prerendering when content is the same for everybody, SSR when content changes per request, and plain client rendering when SEO and first paint do not matter. Keep browser-only code in afterNextRender, and avoid direct window access.

Interview tip: say "Universal is now part of Angular itself (@angular/ssr)", and name hydration as the key change.

typescript
import { RenderMode, ServerRoute } from '@angular/ssr';

// app.routes.server.ts
export const serverRoutes: ServerRoute[] = [
  { path: '', renderMode: RenderMode.Prerender }, // same for everybody
  { path: 'courses/:id', renderMode: RenderMode.Server }, // changes per request
  { path: 'dashboard', renderMode: RenderMode.Client }, // behind login, no SEO
  { path: '**', renderMode: RenderMode.Server },
];

// app.config.server.ts (outline):
//   const serverConfig = { providers: [provideServerRendering(withRoutes(serverRoutes))] };
//   export const config = mergeApplicationConfig(appConfig, serverConfig);
// app.config.ts: provideClientHydration(withEventReplay())

Angular 20.3 with `@angular/ssr` 20.3, type-checked. The server routes file shows the real API shape. The server was not started here, so no page was rendered.

Key points

  • Universal was a separate SSR project. Since Angular 17 it is part of the CLI as `@angular/ssr`.
  • Hydration reuses server HTML. Event replay and incremental hydration reduce lost clicks and work.
  • Route-level render modes mix server, client and prerendered pages in one app.
  • Browser-only code belongs in `afterNextRender`.

Common mistakes

  • Referring to `@nguniversal` packages for new projects.
  • Using SSR for pages that never change, where prerendering is cheaper.
  • Reading `window` or `localStorage` during server rendering.

Follow-ups

  • When would you prerender and when use SSR?
  • What does event replay solve?
  • How does `TransferState` avoid duplicate HTTP calls?

Tips

  • Tell the story in four steps: Universal, hydration, CLI integration, render modes.
  • Match render mode to page type.
  • Mention the browser-only rule.

Evaluation rubric

  • Gives a correct short history.
  • Describes the modern setup and its parts.
  • Chooses render modes sensibly.