Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 17: Angular Universal (Server-Side Rendering)

Angular applications are Single Page Applications (SPAs) by default. This means the browser downloads JavaScript, then renders the UI dynamically. While this works well for interactivity, it can lead to problems:

  • Slower first paint: users see a blank screen until Angular bootstraps.
  • SEO issues: search engine crawlers may struggle to index dynamic content.
  • Poor performance on slow networks or devices.

Angular Universal solves these challenges by rendering the app on the server first, sending HTML to the client, and then letting Angular take over. This process is called Server-Side Rendering (SSR).


17.1 What is Server-Side Rendering?

  • The server generates HTML for the requested route.
  • The browser displays meaningful content immediately.
  • Angular bootstraps on top of the existing HTML (hydration).

✅ Faster perceived performance.
✅ Better SEO (search engines see full HTML).
✅ Useful for social sharing (link previews).


17.2 CSR vs SSR vs SSG

ApproachHow it worksProsCons
CSR (Client-Side Rendering)Rendered fully in browserFast navigation after load, simple hostingSlow first paint, SEO harder
SSR (Server-Side Rendering)HTML rendered on server, then hydratedFast initial load, SEO-friendlyMore complex build/deploy
SSG (Static Site Generation)Pre-render pages at build timeBest performance, no runtime server loadLimited for dynamic content

Angular Universal supports SSR and SSG (pre-rendering).


17.3 When Should You Use Angular Universal?

  • ✅ Content-heavy apps that need good SEO (blogs, e-commerce).
  • ✅ Apps targeting slow networks or devices.
  • ✅ Apps with social sharing requirements.
  • ❌ Purely internal dashboards may not need SSR.

17.4 The Rendering Lifecycle

  1. User requests /about.
  2. Server runs Angular, generates HTML for /about.
  3. Server sends HTML + JavaScript to client.
  4. Browser shows HTML immediately.
  5. Angular hydrates the page, making it interactive.

17.5 Hydration in Angular

Starting from Angular v17, Angular supports hydration by default:

  • Instead of re-rendering the app after download, Angular attaches event listeners to the server-rendered HTML.
  • This means no “flicker” or DOM duplication.

17.6 Benefits and Trade-Offs

Benefits:

  • Better SEO.
  • Faster Largest Contentful Paint (LCP).
  • Improved accessibility for crawlers.

Trade-offs:

  • Build and deployment complexity.
  • Need a Node.js server (unless using static pre-render).
  • Some client-only APIs (window, document) need special handling.

17.7 Practical Setup: Enabling Angular Universal

Now let’s add SSR step by step.

Step 1: Add Angular Universal

ng add @nguniversal/express-engine

This generates files for SSR support, including:

  • A server.ts file (Express server).
  • Updates to angular.json.
  • A main.server.ts entry point.

Step 2: Run the App with SSR

npm run dev:ssr

Open http://localhost:4200. You’ll see your app running with SSR.


Step 3: Pre-Rendering (Optional SSG)

For static sites, pre-render all routes at build time:

npm run prerender

This generates HTML files for specified routes (configured in angular.json).


17.8 Deployment Options

  • Node.js server: host on platforms like Heroku, AWS, or GCP.
  • Static hosting: pre-rendered output can be deployed to Netlify, Firebase, or GitHub Pages.
  • Hybrid: pre-render most pages, SSR dynamic ones.

17.9 Best Practices

  • Use TransferState to pass data from server to client, avoiding duplicate HTTP calls.
  • Be careful with browser-only APIs (localStorage, window). Use Angular’s isPlatformBrowser.
  • Pre-render as much as possible — fall back to SSR only when necessary.

17.10 Summary

  • Angular Universal enables Server-Side Rendering and Static Site Generation.
  • SSR improves SEO, performance, and user experience.
  • Setup is straightforward with ng add @nguniversal/express-engine.
  • Use hydration for seamless transitions between server-rendered and client-interactive states.