question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular Universal Support

See original GitHub issue

As i tried out this (really nice and helpful) library i got surprised with the fact, that Angular Universal threw an Error. lcoalstorage is not defined

It seems that this library doesnt support Angular Universal. I think many would appreciate support for it.

I solved it in the meanwhile with:

const customStorage: Storage = {
  length: 0,
  clear: function (): void {
    if (window && window.localStorage) {
      window.localStorage.clear();
      this.length = window.localStorage.length;
    }
  },
  getItem: function (key: string): string | null {
    try{
      return window.localStorage.getItem(key);
    }catch{
      return null;
    }
  },
  key: function (index: number): string | null {
    try {
      return window.localStorage.key(index);
    }catch{
      return null;
    }
  },
  removeItem: function (key: string): void {
    try {
      window.localStorage.removeItem(key);
      this.length = window.localStorage.length;
    }catch{
        return ;
      }
  },
  setItem: function (key: string, data: string): void {
    try{
      window.localStorage.setItem(key, data);
      this.length = window.localStorage.length;
    }catch{
        return ;
      }
  }
};

export function localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
  return localStorageSync({keys: [{auth: ["token"]},{shopping_cart: ["products"]}], rehydrate: true, storage: customStorage})(reducer);
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:19
  • Comments:7

github_iconTop GitHub Comments

9reactions
bufkecommented, Feb 26, 2019

I think this is a better way to support it. Just add it conditionally to the metareducers if window is undefined. For example:

const metaReducers: Array<MetaReducer<any, any>> = [];
const localStorageSyncReducer = (
  reducer: ActionReducer<any>
): ActionReducer<any> => {
  return localStorageSync({ keys: ["foo"], rehydrate: true })(reducer);
};
if (typeof window !== "undefined") {
  metaReducers.push(localStorageSyncReducer);
}

If this works for people - I think the best solve is to just document not to add the meta reducer when running in node. I can’t think of any reason someone would want localstorage sync to happen server side.

Update never mind this doesn’t work. It works without --prod but localstorage sync just doesn’t happen with --prod. I tried rearranging it and it seems no method that involves conditionally adding the metareducer works.

1reaction
magnatticcommented, Mar 26, 2018

Building on @Joniras solution from above, I simply faked the localStorage for the whole application in my server.ts during the universal startup, similar to how I faked window and document with domino:

const fakeStorage: Storage = {
  length: 0,
  clear: () => { },
  getItem: (_key: string) => null,
  key: (_index: number) => null,
  removeItem: (_key: string) => { },
  setItem: (_key: string, _data: string) => { }
};

const domino = require('domino');

const win = domino.createWindow(template);
(global as any)['window'] = win;
(global as any)['document'] = win.document;
(global as any)['navigator'] = win.navigator;
(global as any)['localStorage'] = fakeStorage;

This way you don’t have to change anything in your metareducer. All window.localStorage calls during SSR will simply go against the fake.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side rendering (SSR) with Angular Universal
This guide describes Angular Universal, a technology that renders Angular applications on the server. A normal Angular application executes in the browser, ...
Read more >
angular/universal: Server-side rendering and Prerendering for ...
The Angular Universal project is a community driven project to expand on the core APIs from Angular (platform-server) to enable developers to do...
Read more >
Angular Universal: Complete Practical Guide
This post will be a complete practical guide for getting started with Angular Universal. We are going to go start with an existing...
Read more >
Angular Universal - server-side rendering for Angular.
Angular Universal is tool which allows server to pre-render Angular application while user hits your website for first time.
Read more >
an adventure. Add angular universal support to… - Medium
1. Use the CLI. The angular CLI now supports generate universal which should save you a lot of typing time. In your application...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found