23

I'm getting QuotaExceededError (DOM Exception 22): The quota has been exceeded. on Safari when I'm in incognito mode.

I went through similar questions like this: QuotaExceededError: Dom exception 22: An attempt was made to add something to storage that exceeded the quota

But they talk about setItem, I get this error somewhere else.

I get this error on this line: localStorage['gallery.extensions'] = JSON.stringify({}); or localStorage['asdf'] = 'asdfg';

I tried combining this answer and replacing every line like localStorage['asdf'] = 'asdfg'; to be localStorage.setItem('asdf', 'asdfg') and every access like localStorage['asdf'] to be localStorage.getItem('asdf') but that didn't help either.

Community
  • 1
  • 1
shinzou
  • 5,850
  • 10
  • 60
  • 124
  • 1
    `I get this error on this line` - that's just like using `setItem` - so same constraints – Jaromanda X Feb 26 '17 at 11:33
  • 1
    the accepted answer in the question you linked to http://stackoverflow.com/questions/21159301/quotaexceedederror-dom-exception-22-an-attempt-was-made-to-add-something-to-st applies to your situation – Jaromanda X Feb 26 '17 at 11:34
  • 1
    I added the code from here: http://stackoverflow.com/a/27081419/4279201 but it still threw this error on that line. – shinzou Feb 26 '17 at 11:34
  • 1
    But I need to test my app in incognito because I don't want to go and clean my data every time I change something. @JaromandaX – shinzou Feb 26 '17 at 11:36

2 Answers2

29

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.

Daniel Dewhurst
  • 2,533
  • 2
  • 21
  • 39
David Thomas
  • 2,264
  • 2
  • 18
  • 20
  • 1
    So how do developers on OSX test their app if they need to clean the cache and data each time? – shinzou Feb 26 '17 at 11:37
  • 1
    @kuhaku [This link](http://superuser.com/questions/186594/how-can-i-force-safari-to-perform-a-full-page-reload-without-using-the-mouse) may help. Personally when developing in Chrome by having the developer tools window open, the cache is cleared automatically on every page refresh. You could also add code to your web page to forcibly delete items from localstorage every time your page loads, just to ensure it's clean before your page loads. – David Thomas Feb 26 '17 at 11:43
  • 2
    With remote debugging of Mobile Safari you can clear the cache also with Command-Option-e: http://stackoverflow.com/questions/19249961/use-desktop-webinspector-to-clear-cache-in-mobile-safari (https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/GettingStarted/GettingStarted.html#//apple_ref/doc/uid/TP40007874-CH2-SW2) – iJames Apr 08 '17 at 19:43
  • 5
    Just tested now and I can totally use Local Storage in incognito. Come to think of it I always could (anyway this comment is 3.5 years after the last ones). I don't know where the `QuotaExceededError` is coming from - it happened to me on a colleague's machine, doesn't happen on mine. Go figure. Anyway my lesson was to wrap the `localStorage.setItem` and `localStorage.getItem` methods in `try` and `catch`. Always good practice (if the local storage isn't critical to the application). Note to self: apply also with other non-critical code that can unnecessarily crash the app. – Gilad Barner Aug 12 '20 at 10:05
  • 3
    localStorage can be use in incognito mode – Leccho Mar 22 '21 at 17:57
  • this means the localStrorage.setItem cannot save any thing. because of run out of cache storage – mohammad feiz Feb 27 '23 at 14:42
25

The accepted answer is incorrect. This happens when localStorage.setItem runs out of memory and throws an error. As another commenter wrote, always wrap setItem in try {} catch() {}

hedgehogrider
  • 1,168
  • 2
  • 14
  • 22
  • when it exceeds allowed memory limit (10 MB on chrome / firefox). – Safwat Fathi Mar 20 '21 at 14:38
  • To confirm that you can indeed use web storage on incognito is by using this: http://dev-test.nemikor.com/web-storage/support-test/ – Justsolarry Jul 07 '21 at 12:31
  • related question about localStorage limits: https://stackoverflow.com/questions/2989284/what-is-the-max-size-of-localstorage-values – kca Oct 12 '21 at 09:23