Thoughts on missing localStorage?
See original GitHub issueIn 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top GitHub Comments
I like it! Leave it up to the app developer whether or not they care 😃
Perfect. I am closing this issue then. If you have more valuable feedback please don’t hesitate to write it.