Here’s a pretty quick fix to get
localStorage available in your Angular Server-Side Rendering application.
This article assumes you followed the Angular Server-Side Rendering Story: https://github.com/angular/angular-cli/wiki/stories-universal-rendering
npm install localstorage-polyfill
Add this to your
global['localStorage'] = localStorage;
Rebuild your project.
npm run build:ssr and serve locally as
npm run serve:ssr. All
localStorage errors should be suppressed now.
You don’t have to change ANY part of your actual Angular code. The above approach makes sure
localstorage-polyfillis available for global use.
If you’re wondering how to run your SSR (Server Side Rendering) app in production using Nginx, check out this other article I wrote: Serving Angular SSR via Nginx Proxy