3

I'm getting this error on my iPhone's safari, when doing localStorage.setItem('user',some string here):

Error: The quota has been exceeded.
setItem@[native code]

It is not private mode! What other circumstances can make localStorage not work?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ice_vita
  • 79
  • 1
  • 2
  • 7
  • Possible duplicate of [html5 localStorage error with Safari: "QUOTA\_EXCEEDED\_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota."](http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-safari-quota-exceeded-err-dom-exception-22-an) – Alex W Apr 28 '17 at 20:22

2 Answers2

2

I created this class to help get around private browsing. However, storage will be blown away when you refresh the browser.

const data = {};
let hasLocalStorage = false;

if (localStorage) {
  try {
    const x = 'storageTest';
    localStorage.setItem(x, x);
    localStorage.removeItem(x);
    hasLocalStorage = true;
  } catch (e) {
    hasLocalStorage = false;
  }
}

class StorageUtilities {
  setItem(key, value) {
    if (hasLocalStorage) {
      localStorage.setItem(key, value);
    } else {
      data[key] = value;
    }
  }

  getItem(key) {
    if (hasLocalStorage) {
      return localStorage.getItem(key);
    }
    return data[key];
  }

  removeItem(key) {
    if (hasLocalStorage) {
      localStorage.removeItem(key);
    } else {
      data[key] = null;
    }
  }
}

const storageUtilities = new StorageUtilities();

export default storageUtilities;
camloken
  • 131
  • 1
  • 6
  • This is very helpful. Thank you. As an addition I would convert the value to String if it is not null at the setItem function to get a more consistent behaviour. – Felix Gertz Jun 24 '20 at 16:23
1

Actually it was Private mode. Looks like it is enabled by default on new iphones.

ice_vita
  • 79
  • 1
  • 2
  • 7
  • Wait now. What is this private mode? I'm just running into this problem where Chrome's working fine but Safari is not and I just figured out how to do Web Inspector from my iPhone. (https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/GettingStarted/GettingStarted.html#//apple_ref/doc/uid/TP40007874-CH2-SW2) But maybe some additional information here would help folks understand why we've got this private mode hurdle to get over and how to avoid it? I don't want my users having to modify vanilla Safari Mobile settings. – iJames Apr 08 '17 at 19:35
  • 1
    And thanks Apple for such a useful error message. :-( Angular and Satellizer both figure it out "o = k(b) || (h.warn("This browser does not support Web Storage!"), {" But Quota Exceeded? Really? – iJames Apr 08 '17 at 19:48