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.

Thoughts on missing localStorage?

See original GitHub issue

In certain contexts you may encounter browsers which have disabled localStorage.

If you’re blocking cookies in Safari, for instance, simply trying to access localStorage (even for a typeof check) will throw an error.

In other cases/browsers, attempting to actually set an item will yield a quota exceeded error (they set the max quota size to 0 bytes).

An actual check for whether or not localStorage is “available and usable” would look something like:

const hasLocalStorage = (() => {
  try {
      if (typeof localStorage === 'undefined') {
          return false
      }

      // Can we actually use it? "quota exceeded" errors in Safari, etc
      const mod = '__lscheck'
      localStorage.setItem(mod, mod)
      localStorage.removeItem(mod)
  } catch (err) {
      return false
  }

  return true
})()

Even with such a check, the “solution” isn’t obvious - what should a library such as this one do in this case? Throw? Warn? Pretend like it isn’t happening?

Given that you often use this as a “drop-in” replacement for setState, I would at least expect it to fall back to using in-memory state - but in that case, how far do you go? Try to implement postMessage calls for cross-tab/window polyfilling of the missing storage events?

I’m raising this mostly to get your perspective, but I also feel that perhaps we should try to implement something that prevents this hook from potentially blocking the page from being rendered should the user have disabled localStorage.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rexxarscommented, Apr 28, 2020

I like it! Leave it up to the app developer whether or not they care 😃

0reactions
astoilkovcommented, May 4, 2020

Perfect. I am closing this issue then. If you have more valuable feedback please don’t hesitate to write it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Please Stop Using Local Storage - DEV Community ‍ ‍
To keep it short, here's the only situation in which you should use local storage: when you need to store some publicly available...
Read more >
Lost data stored in localStorage issue - Stack Overflow
Local storage work only with strings. MDN: Window.localStorage. The keys and the values stored with localStorage are always in the UTF-16 ...
Read more >
Is LocalStorage safe to use? - Snyk
Local storage has caught the attention of developers as a lightweight solution for data ... Here are a few thoughts from the folks...
Read more >
LocalStorage, sessionStorage - The Modern JavaScript Tutorial
The localStorage is shared between all windows with the same origin, so if we set the data in one window, the change becomes...
Read more >
Disks missing from local storage - Citrix Discussions
Now I've joined the third server to the pool, and I'm attempting to reattach the local storage to the child server. All attempts...
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