Implementation: How to make AppComponent SSR-safe when it accesses document/...
Documentation for Sessions
⚙️ Implementation: How to make AppComponent SSR-safe when it accesses document/...
Date: 2026-02-18
Time: 19:42:26 UTC
Session: gspace1-119.3
Agent: angular
Confidence: 95%
Status: ✅ Documented
📋 Decision Context
Question:
How to make AppComponent SSR-safe when it accesses document/window in ngOnInit?
Task: Make AppComponent SSR-safe for prerendering
🎯 Decision Made
Chosen:
Add isPlatformBrowser check via PLATFORM_ID injection, early-return from ngOnInit and ngOnDestroy on the server
Options Considered:
- No alternatives documented
💭 Reasoning
AppComponent directly accesses document.addEventListener, window.addEventListener, document.getElementById in ngOnInit. During server-side prerendering, document exists (Angular SSR emulates it) but window does NOT. Rather than wrapping each individual line, the cleanest approach is a single guard at the top of ngOnInit: if (!this.isBrowser) return;. This skips ALL browser-only init on the server. The prerendered legal routes don't need any of AppComponent's initialization (data loading, event listeners, loader removal) — they just need the route to render. Same guard in ngOnDestroy prevents cleanup of listeners that were never added. The isBrowser flag is computed once at injection time: private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
📁 Files Involved
apps/gograb-space/src/app/app.component.ts
🔗 Related
- Decision ID:
dev_1771443746315_1cxl9mz - Session: 119
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation