Search code examples
angularserver-side-renderingpre-rendering

Angular 17 window not defined


Recently after updating my angular cli from 16 to 17 I have been facing performance issues and I had no idea what is SSR and prerendering so I left them enabled. But the root cause was ssr.

  • After installing materialize-css I was getting an error M is not defined. Then I imported it from materialize-css.

  • Again I was getting an error window is not defined


Solution

  • When using Server side rendering (SSR) and/or prerendering, the app will run on a node environment where the global window object isn't defined.

    If you want to keep using SSR/prenrender and still access window you can use afterNextRender() which will be invoked on the browser side only (the callback will be run outside the Angular zone, you also might need ngZone.run().)