Skip to content
Discussion options

You must be logged in to vote

Solved.

Final solution:
Use the following code instead of const storage = window.localStorage; at the top level

function getLocalStorage() {
  if(typeof window !== "undefined" && window.localStorage) {
    return window.localStorage;
  }
  throw new Error("localStorage is not defined.");
}

In the functions that need to use localStorage, do:

let storage: Storage;
try {
  storage = getLocalStorage();
} catch {
  return ...;
}
// ...

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@icyJoseph
Comment options

@NriotHrreion
Comment options

Comment options

You must be logged in to vote
1 reply
@icyJoseph
Comment options

Answer selected by NriotHrreion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants