2

What happens when LocalStorage hits the maximum storage limit for a domain?

  • hit a hard limit and silently stop accepting new values
  • hit a hard limit and throw an error
  • hit a soft limit and the browser uses a pruning strategy to make room for new values

There is a similar StackOverflow question: What is the max size of localStorage values? which answers the question of what the browser's storage limit is, but that question does not ask or answer what exactly happens when the limit is reached. What the limit is doesn't matter too much to me, what is important to me is what exactly happens when the limit is hit, and if I need to take care of pruning the cache in my client code, or if the browser will do it for me automatically.

Community
  • 1
  • 1
limscoder
  • 3,037
  • 2
  • 23
  • 37
  • I believe it's implementation-specific and the general guideline is "prompt the user" – lc. Mar 28 '13 at 16:51

1 Answers1

4

Stating the Spec:

If it couldn't set the new value, the method must throw an QuotaExceededError exception. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.)

So theoretically you should always use a try-catch clause to prevent that error.

for (var i = 0;i<200000;i++){
    test += "sdfghyxcvbn123kjhajsdfkhuwehfkjs,dnfhkweufhkjsdf";
}
try{
    var result = window.localStorage.setItem("test",test);
} catch(e) {
    console.log(e.message);
}
Christoph
  • 50,121
  • 21
  • 99
  • 128