Server-Side Rendering (SSR) with Angular
SSR renders Angular on the server, sends full HTML to the browser. Better SEO, faster content visibility, and improved social sharing.
Normal Angular (CSR): Browser downloads empty HTML โ loads JS โ Angular bootstraps โ renders content โ user sees page.
SSR: Browser requests URL โ Node.js server renders Angular โ sends full HTML โ user sees content immediately โ JS loads โ hydration adds interactivity.
"SSR = pre-cooked biryani โ server pe bana ke bhejo, client pe sirf garam karo."
Key benefit: SEO. Google sees your full content, not an empty placeholder page.
Why SSR matters for your app:
- SEO: Google crawls rendered HTML. Without SSR, Google may see an empty page. "Bina SSR = blank page dikhata hai โ ranking zero."
- Performance: First Contentful Paint happens in milliseconds instead of seconds. Users see content while JS downloads.
- Social sharing: Facebook, Twitter, WhatsApp see your meta tags and content immediately in the link preview.
Hyderabad companies: TCS, Wipro, Infosys projects often require SSR for client-facing apps. SSR is not optional for production Angular apps.
Adding SSR to an existing Angular app is one command:
ng add @angular/ssr"Ek command โ SSR setup ho gaya."
This command does everything:
- Adds
@angular/ssrpackage to your project - Creates
src/app/app.config.server.tsโ server config - Creates
src/main.server.tsโ server entry point - Updates
angular.jsonwith SSR configuration - Adds
server.tsโ Express server for serving the app
Build and run: ng build generates browser + server bundles. Run with node dist/project-name/server/server.mjs.
How SSR works step by step:
- Request comes to Node.js server
- Angular renders the component tree on the server (just once, no browser needed)
- Server sends FULL HTML to browser โ user sees content immediately
- Browser downloads JS bundles (in background)
- Angular "hydrates" โ attaches event listeners to existing HTML, activates app
"Server = painter (paints the wall), Browser = electrician (adds switches/lights)."
Hydration is the process of reusing the server-rendered HTML and adding client-side interactivity. No content flash or re-render.
SSR gotchas to watch out for:
window/documentare NOT available on the server. Accessing them crashes the server. Check platform:inject(PLATFORM_ID)+isPlatformBrowser(platformId)localStoragenot available on server. Wrap all localStorage access inif (isPlatformBrowser(...))- Third-party libraries that manipulate DOM may break. Check SSR compatibility before using.
- Heavy components: use
@deferto skip server rendering for non-critical parts.
private platformId = inject(PLATFORM_ID);
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
// Safe to use window, document, localStorage
}
}Key Takeaways
- SSR renders Angular on the server โ sends full HTML to browser
- Critical for SEO: Google sees complete content, not empty page
- ng add @angular/ssr โ one command sets up everything
- Hydration attaches event listeners to server-rendered HTML
- Always use isPlatformBrowser() before accessing window/document/localStorage
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login