前言不同浏览器的存储配额与清理策略差异较大。本文提供评估与持久化申请的统一流程,以提升数据可靠性。能力检测与配额评估const supportsStorageManager = !!navigator.storage;

async function estimateQuota() {

const { usage, quota } = await navigator.storage.estimate();

return { usage, quota };

}

持久化申请async function ensurePersistentStorage() {

if (!supportsStorageManager) return false;

const persisted = await navigator.storage.persisted();

if (persisted) return true;

return navigator.storage.persist();

}

结合 OPFS 与 IndexedDB 的容量策略async function chooseStore(bytesNeeded) {

const { quota, usage } = await estimateQuota();

const free = quota - usage;

const supportsOPFS = !!(navigator.storage && navigator.storage.getDirectory);

if (supportsOPFS && free > bytesNeeded) return 'opfs';

return 'idb';

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部