Safari Private browsing mode appears to support localStorage, but doesn't
See original GitHub issueApparently this is by design. When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage is available, but trying to call .setItem throws an exception.
store.js line 73 “QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota.”
What happens is that the window object still exposes localStorage in the global namespace, but when you call setItem, this exception is thrown. Any calls to .removeItem are ignored.
I believe the simplest fix (although I haven’t tested this cross browser yet) would be to alter the function isLocalStorageNameSupported() to test that you can also set some value.
currently:
function isLocalStorageNameSupported() {
try { return (localStorageName in win && win[localStorageName]) }
catch(err) { return false }
}
proposed:
function isLocalStorageNameSupported() {
try {
var supported = (localStorageName in win && win[localStorageName]);
if (supported) { localStorage.setItem("storage", ""); localStorage.removeItem("storage");
return supported;
}
catch(err) { return false }
}
Of course it’s kind of silly to call this every time you want to check that storage is supported, so you could memoize it, but you get the idea.
Issue Analytics
- State:
- Created 11 years ago
- Comments:10 (8 by maintainers)
Top GitHub Comments
So I just ran the tests in safari private mode, and
store.disabled == true
as expected.Before you rely on any storage, you need to check that it is enabled and working.
store.disabled
&store.enabled
does this for you. Just check one of those flags. If storage isn’t enabled and your product depends on it, you need to notify your user that they must enable storage in order to use your product.Closing this again as I don’t see anything wrong.
Cheers! Marcus
Yeah 😃
There is no good alternative. Either persistance is possible or it is not. I don’t think there’s anything the library can do that’s better than simply letting you know that persistance is disabled.
BTW: If you feel up for helping the next guy out, I’d really appreciate it if you contributed a note in the README file about store.disabled and issued a pull request. The flag should very much be documented!
Cheers! Marcus