localStorage Quota Exceeded
快速摘要
你的 localStorage setItem call 失败因为 storage 限制 (通常 5-10MB) 有是 reached.
为什么发生
也 much 数据存储 in localStorage for 原点
大对象存储不使用 compression or pruning
旧数据不 是 cleaned 上, accumulating over 时间
最小示例
✗ 错误代码
localStorage.setItem("data", largeJSONString);
// QuotaExceededError if storage is full✓ 修复代码
try {
localStorage.setItem("data", largeJSONString);
} catch (e) {
if (e.name === "QuotaExceededError") {
// Remove old data and retry
localStorage.removeItem("oldData");
localStorage.setItem("data", largeJSONString);
}
}Wrap setItem in try/catch 到 handle quota 错误. 当 quota 是 exceeded, 移除旧 or 不必要数据 and retry, or inform 用户.
如何诊断
检查错误: QuotaExceededError or ' quota 有是 exceeded'
Measure current storage usage 使用 Storage API (navigator.storage.estimate())
Review 什么数据是 是存储 and whether 它能 是 reduced
如何修复
移除旧 or 不必要数据之前存储新 数据
存储仅 必要数据 — use 服务器-边数据库 for 大数据
Compress 数据之前存储 (e.g., shorten 键, 移除 redundant 字段)
Use IndexedDB for larger 客户端-边 storage 需要
如何预防
Wrap setItem in try/catch 到 handle quota 错误 gracefully
Implement 数据 cleanup 策略 for 旧条目
Use IndexedDB for 数据 larger than 少数 MB
相关资源
相关术语
相关课程
- Web Storage
学习 web storage.