Storage Limits
localStorage has a limit of approximately 5MB per origin. Exceeding it throws a QuotaExceededError. Check available space and handle errors gracefully.
localStorage has a limit of approximately 5MB per origin. Exceeding it throws a QuotaExceededError. Check available space and handle errors gracefully.
// Handle quota exceeded
function safeStore(key, value) {
try {
localStorage.setItem(key, JSON.stringify(value));
return true;
} catch (e) {
if (e.name === "QuotaExceededError") {
console.warn("Storage full — clearing old cache");
localStorage.clear();
return false;
}
throw e;
}
}
// Estimate storage usage (Chrome only)
const estimate = await navigator.storage.estimate();
console.log(`Used: ${estimate.usage} / ${estimate.quota} bytes`);
5MB sounds like a lot, but large JSON objects fill it fast — store only what you truly need in localStorage.
More in JavaScript