前言不同浏览器的存储配额与清理策略差异较大。本文提供评估与持久化申请的统一流程,以提升数据可靠性。能力检测与配额评估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'; }

发表评论 取消回复