Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(iOS/Safari) localStorage in private mode has quota=0 and always throws on write #63

Open
jakub-g opened this issue Oct 28, 2015 · 0 comments

Comments

@jakub-g
Copy link

jakub-g commented Oct 28, 2015

Platforms:

Safari on iOS

Summary:

When a new tab is launched in private mode, localStorage has zero quota assigned and hence always throws when trying to write to it.

Example:

localStorage.setItem('test', 1) // throws QUOTA_EXCEEDED_ER

Workarounds:

The best bet could be to try-catch and silently fail when saving to localStorage (depending if localStorage is critical for the app to work or not).

Since it would be mundane and error-prone to always wrap any localStorage.setItem call with a try-catch, you could create a global localStorageWrapper (and ban the direct localStorage access in JSHint for instance) like this (snippet for Object.create-supporting browsers)

var StorageWrapperProto = function(underlyingStorage) {
    return {
      key: function(n) {
        return underlyingStorage.key(n);
      },

      clear: function() {
        underlyingStorage.clear();
      },

      getItem: function(key) {
        return underlyingStorage.getItem(key);
      },

      setItem: function(key, value) {
        try {
          underlyingStorage.setItem(key, value);
        } catch (e) {
          window.console.error("Error writing to the storage for key = " + key +
            "! (this is expected in private mode in Safari)");
        }
      },

      removeItem: function(key) {
        underlyingStorage.removeItem(key);
      }
    };
};

window.localStorageWrapper = Object.create(new StorageWrapperProto(window.localStorage));
window.sessionStorageWrapper = Object.create(new StorageWrapperProto(window.sessionStorage));

StackOverflow discussions:

http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-safari-quota-exceeded-err-dom-exception-22-an

@jakub-g jakub-g changed the title localStorage in Safari on iOS in private mode has quota=0 and always throws on write (iOS/Safari) localStorage in private mode has quota=0 and always throws on write Oct 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant