liveBook · Manning

Storage api

As explained in Vanilla Web

The Storage API is a browser API for inspecting web-storage usage, estimating quotas, and requesting persistence.

An offline-capable app needs to know whether its local data is nearing a storage limit—and whether the browser may evict that data. The Storage API provides origin-level usage estimates, quota estimates, and persistence controls. It does not store data; use a storage API suited to the data, such as IndexedDB, Cache Storage, or OPFS. section 12.4.3 section 12.5

if (navigator.storage?.estimate) {
  const { usage, quota } = await navigator.storage.estimate();
  console.log(`Using ${usage} of ${quota} bytes`);
}

Check for support before calling `navigator.storage.estimate()`. Its `usage` and `quota` values are approximate, not exact measurements or fixed allocations; available quota varies with the browser, device, mode, and settings. section 12.4.3

Most origin storage is best effort: the browser may evict it under storage pressure or after inactivity. The quota bucket commonly includes IndexedDB, Cache Storage, OPFS, service worker registrations, and—in most browsers—local storage. It usually excludes cookies, sessionStorage, the regular HTTP cache, and files explicitly chosen by the user. section 12.4.3

if (navigator.storage?.persisted) {
  const alreadyPersistent = await navigator.storage.persisted();
  console.log("Already persistent:", alreadyPersistent);
}

if (navigator.storage?.persist) {
  const granted = await navigator.storage.persist();
  console.log("Persistent storage:", granted);
}

If offline data is important, check `persisted()` and request persistence with `persist()` where available. Treat the result as a grant decision, not an assumption. Browsers may use heuristics to decide; Firefox may ask the user. section 12.4.3

Persistence protects against eviction solely due to storage pressure, not user deletion, profile loss, device resets, or app bugs. For important local data, provide an export or synchronization path; plan for quota errors and validate restored data. section 12.4.3 section 12.5

How Vanilla Web covers it

How deep it goes
Shows guarded calls for estimates and persistence, then explains approximate quotas and eviction limits; it does not implement a storage engine.
Examples
Uses `navigator.storage.estimate()`, `persisted()`, and `persist()` checks, including usage logging and grant-result logging.
What you can do after
Operate/configure origin storage checks and persistence requests while planning for quota limits, denied grants, and data loss.
How it connects
Connects storage quotas and persistent storage to IndexedDB, Cache Storage, OPFS, and Web Storage.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.