store.js icon indicating copy to clipboard operation
store.js copied to clipboard

QuotaExceededError on iOS with Safari and the newest releases not available from cdnjs

Open kireerik opened this issue 8 years ago • 9 comments

QuotaExceededError: The quota has been exceeded.

Also newest releases not available from cdnjs.

kireerik avatar Apr 20 '17 17:04 kireerik

Safari has both localStorage and sessionStorage in private browsing mode, however, they set the quota limit to 0, so you can't actually store anything. The way around this is to test if you can actually set an item using localStorage.

let hasLocalStorage = false;

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

Same goes for sessionStorage. If either one return false (they will in Safari private browsing), create a variable and store your data there. Here is a full example of what I did on a similar project below (I'm only testing for localStorage):

const data = {};
let hasLocalStorage = false;

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

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

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

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

const storageUtilities = new StorageUtilities();

export default storageUtilities;

Someone feel free to implement a similar solution here.

camloken avatar Jun 21 '17 16:06 camloken

I just came across this error today... I thought store.js was supposed to help deal with these issues... Does anyone have an idea how to handle this problem with store.js

dijs avatar Mar 05 '18 09:03 dijs

@dijs , what version of Safari? I just tried getting the old quota error on my Mac's Safari 11.0.3 and localStorage was working fine in private mode. I have been under the impression for a few months that Apple had fixed this problem.

nbubna avatar Mar 05 '18 16:03 nbubna

Here is my sentry error:

screen shot 2018-03-05 at 7 40 28 pm

dijs avatar Mar 05 '18 17:03 dijs

This works in private mode for me on my iPhone (11.2.6):

https://jsbin.com/nofunigoya/edit?js,output

Also works in Safari Private on my Mac.

nbubna avatar Mar 05 '18 18:03 nbubna

So if this isn't the iOS bug, it must be legitimately full? Or can you think of another reason why this would be throwing...

Even so, I thought store.js would catch this error and handle the storage with a fallback.

dijs avatar Mar 05 '18 18:03 dijs

I don't know of support for that in this library. https://github.com/nbubna/store has an "overflow" extension that provides non-persistent storage when you hit the quota, if that's of interest.

nbubna avatar Mar 05 '18 19:03 nbubna

I'm sure it's possible to write such an extension for this store library too.

nbubna avatar Mar 05 '18 19:03 nbubna

Thanks for the suggestions.

dijs avatar Mar 05 '18 19:03 dijs