QuotaExceededError thrown inconsistently across browsers when localStorage quota exceeded
0 reputation · 25 Feb 2021, 12:36 UTC
0 reputation · 25 Feb 2021, 12:36 UTC
The HTML5 Web Storage specification defines QuotaExceededError as the failure condition when an origin exceeds its allocated localStorage quota, but leaves the exact quota size and eviction strategy implementation-defined. This creates an unresolved decision about how browsers signal quota exhaustion when setItem would exceed the limit.
It is unclear whether a setItem call that would exceed the quota throws immediately or after a best-effort write attempt, and whether the exception is consistently signaled as a DOMException with the name QuotaExceededError across user agents. Synchronous storage access blocks the main thread, so the timing of the throw affects page responsiveness under storage pressure.
Privacy modes and browser updates further vary the effective quota, making it difficult to predict the failure threshold without per-browser measurement.
setItem throw synchronously before any write attempt when the payload would exceed the quota, or after a partial write?DOMException with name === "QuotaExceededError" in every conforming browser?35275 reputation · 25 Feb 2021, 23:22 UTC
In all mainstream modern browsers (Chrome, Firefox, Safari, and Edge), localStorage.setItem() throws a QuotaExceededError synchronously before any data is written to the disk. If the payload would exceed the origin's allocated quota, the operation fails immediately, and the storage state remains unchanged; no partial write occurs.
The exception is consistently thrown as a DOMException. To reliably catch this across different user agents, you should verify the name property of the error object rather than the error message, as messages are localized and vary by browser.
try {
localStorage.setItem('key', largeData);
} catch (e) {
if (e.name === 'QuotaExceededError') {
// Handle quota exhaustion
}
}
Private or Incognito modes do not change the type of error thrown, but they significantly alter the threshold at which it occurs. While standard contexts typically allow 5MB to 10MB, private contexts may enforce much stricter limits or use ephemeral storage that triggers the exception sooner. Because the exact limit is implementation-defined, you cannot predict the failure point based on a fixed byte count.
To verify the quota and exception behavior in your specific environment, use the following scoped approach in the browser console:
catch block triggers and that localStorage.getItem('testKey') returns null, confirming no partial write occurred.Diagnostic Note: Are you targeting legacy browsers (e.g., Internet Explorer) or specific mobile web-views? These may exhibit non-standard "best-effort" writes or different exception names that would require a broader catch strategy.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.