In SSR, code runs on Node.js, so these will crash:
window
document
localStorage
navigator
Correct way (Angular 21 compatible)
import { isPlatformBrowser } from '@angular/common';
import { PLATFORM_ID, Inject } from '@angular/core';
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
localStorage.setItem('key', 'value');
}
}
Step 6: SEO Optimization (Optional but Recommended)
Use Angular’s Meta & Title services:
import { Meta, Title } from '@angular/platform-browser';
constructor(private meta: Meta, private title: Title) {}
ngOnInit() {
this.title.setTitle('Angular 21 SSR Page');
this.meta.updateTag({ name: 'description', content: 'SEO friendly SSR page' });
}
These tags will be rendered on the server.
Step 7: Lazy Loading Works Automatically
SSR supports lazy-loaded modules out of the box. No extra config required.
Step 8: Deployment (Production)
Typical production flow:
npm run build:ssr
node dist/your-project/server/server.mjs
Common hosting options
Azure App Service
AWS EC2 / Elastic Beanstalk
Docker + Nginx
Vercel (with Node adapter)
One-Line Interview Summary
To enable SSR in an existing Angular 21 project, run ng add @angular/ssr, build using
npm run build:ssr, and serve the app via the generated Node.js Express server.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To enable Server-Side Rendering (SSR) on an existing Angular 21 project, you use Angular Universal.
What is SSR in Angular (Quick Context)
SSR renders Angular pages on the server (Node.js) instead of the browser
Benefits:
Step 1: Add Angular Universal (SSR) to Existing Project
Run this command from your project root:
This works for Angular 21 as well.
What this command does automatically
server.tsmain.server.tsapp.config.server.tsangular.jsonpackage.jsonStep 2: Verify Generated Files
After installation, you should see:
server.tsStep 3: Build & Run SSR App
Build SSR bundles
Start SSR server
App will run on:
Step 4: Confirm SSR is Working
Open View Page Source (not DevTools Elements).
If SSR is enabled, you’ll see:
<app-root></app-root>)Step 5: Handle Browser-Only APIs (IMPORTANT)
In SSR, code runs on Node.js, so these will crash:
windowdocumentlocalStoragenavigatorCorrect way (Angular 21 compatible)
Step 6: SEO Optimization (Optional but Recommended)
Use Angular’s Meta & Title services:
These tags will be rendered on the server.
Step 7: Lazy Loading Works Automatically
SSR supports lazy-loaded modules out of the box.
No extra config required.
Step 8: Deployment (Production)
Typical production flow:
Common hosting options
One-Line Interview Summary