Angular · Senior · Question 27 of 75
How do you safely use browser-only APIs (window, localStorage) in an SSR app?
- ✓Correct answer: Run them in
afterNextRenderor guard withisPlatformBrowser(inject(PLATFORM_ID)) - 2Wrap them in
try/catchand ignore errors - 3Use them in the constructor; Node.js provides
window
Explanation
afterNextRender and afterEveryRender never run on the server. For values needed in both environments, use an injectable abstraction with a server and a browser implementation.