Html5 Localstorage Error With Safari: "Quota_Exceeded_Err: Dom Exception 22: an Attempt Was Made to Add Something to Storage That Exceeded the Quota."

QuotaExceededError: Dom exception 22: An attempt was made to add something to storage that exceeded the quota

This can occur when Safari is in private mode browsing. While in private browsing, local storage is not available at all.

One solution is to warn the user that the app needs non-private mode to work.

UPDATE: This has been fixed in Safari 11, so the behaviour is now aligned with other browsers.

html5 localStorage error with Safari: QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota.

Apparently 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.

https://github.com/marcuswestin/store.js/issues/42

function isLocalStorageNameSupported() 
{
var testKey = 'test', storage = window.sessionStorage;
try
{
storage.setItem(testKey, '1');
storage.removeItem(testKey);
return localStorageName in win && win[localStorageName];
}
catch (error)
{
return false;
}
}

html5 localstorage error safari

What the issue is

The localStorage size is limited to a relatively small size (a few MB tops). In Safari this exception occurs in two cases:

  • In private browsing mode no localStorage access is allowed so calling localStorage.set will always cause this error.
  • In regular browsing this exception happens when the quota is exceeded - that is, either the localStorage contains too many things. Web browsers make no guarantees about the actual size of localStorage

Fixing it

The simplest solution except storing less things on the client-side is to use something like IndexedDB which has much much higher quotas. It also performs better because it performs non-blocking io.

QuotaExceededError (DOM Exception 22): The quota has been exceeded on Safari in incognito

You can't use local storage in incognito mode. By wrapping your setItem or getItem calls in a try/catch just helps your code handle the failed usage of local storage, and then alert the user that they need to use your application in a non-private mode.

The error you're getting is by design.

EDIT 2021: You can now use localStorage in Incognito mode. This error can also occur when you run out of the allowed storage space limit per app/domain. At the time of writing most browsers limit to 10mb.



Related Topics



Leave a reply



Submit