Implementation: How to make provideAppInitializer callbacks SSR-safe in app....

Documentation for Sessions

⚙️ Implementation: How to make provideAppInitializer callbacks SSR-safe in app....

Date: 2026-02-18
Time: 19:42:21 UTC
Session: gspace1-119.2
Agent: storage
Confidence: 95%
Status: ✅ Documented


📋 Decision Context

Question:

How to make provideAppInitializer callbacks SSR-safe in app.config.ts?

Task: Make provideAppInitializer SSR-safe for prerendering


🎯 Decision Made

Chosen:

Add isPlatformBrowser(inject(PLATFORM_ID)) guard at the top of each browser-dependent initializer, returning Promise.resolve() to satisfy TypeScript return types

Options Considered:

  1. isPlatformBrowser guard in each initializer
  2. Separate server config without merging
  3. afterNextRender in initializers
  4. Create SSR-safe wrapper utility function

💭 Reasoning

During prerendering, Angular bootstraps the app in Node.js. ALL provideAppInitializer callbacks execute — including those that access browser APIs (localStorage, IndexedDB, window, document) or make HTTP calls (to backends that don't exist at build time). Without guards, these either throw (crashing the prerender) or hang forever (Promise never resolves, build hangs). The guard pattern: if (!isPlatformBrowser(inject(PLATFORM_ID))) return Promise.resolve(); — this short-circuits immediately on the server, letting the prerender proceed. Key: return Promise.resolve() (not void) when the function otherwise returns a Promise, to avoid TypeScript TS7030 'not all code paths return a value'. For synchronous initializers, plain return; is fine.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation