Menu

Mock browser APIs via injection tokens for SSR, or just use afterNextRender?

#angular#redditangular#safe#browser#mock
Reading 0:00
15s threshold

Hello,

I’m making my Angular code SSR-safe and I’m torn between two approaches for browser APIs (localStorage, matchMedia, ResizeObserver, etc.):

  1. Provide an injection token for the API (derived from DOCUMENT, or a platform check) with a safe mock/fallback on the server, and inject it everywhere.
  2. Use afterNextRender, which only runs in the browser, so the plain browser API inside it is safe and no mock is needed.

How do you handle this in real projects?

Thanks!

submitted by /u/khalilou88
[link] [comments]