storage.estimate Reports Quota Headroom

StorageManager.estimate() returns usage and quota for the origin, and persisted() reads whether storage survives eviction pressure.

TypescriptConfig Management

Code

typescriptsnippet
15 lines
// StorageManager.estimate() reports origin usage and quota asynchronously
const manager = navigator.storage;
if (!manager || typeof manager.estimate !== 'function') {
  console.log('StorageManager unavailable in this context');
} else {
  const { usage = 0, quota = 0 } = await manager.estimate();
  const used = (usage / 2 ** 20).toFixed(1);
  const cap = (quota / 2 ** 20).toFixed(1);
  const headroom = ((quota - usage) / 2 ** 20).toFixed(1);
  console.log('used MB:', used, '| quota MB:', cap, '| headroom MB:', headroom);

  // persisted() reads whether the origin already keeps storage under pressure
  const persisted = await manager.persisted();
  console.log('persisted storage:', persisted);
}

Output

Captured from a sandboxed run of this snippet in the build pipeline.

StorageManager unavailable in this context

How it works

The code’s whole job belongs to the functions used, cap and headroom.

The async parts await their results, which keeps ordering predictable.

What to watch for

  • It runs on exactly what the toolchain already ships — nothing extra.
  • This is browser-side code — navigator will not resolve in Node.

Putting it to use

The signature you will call most reads const used = (usage / 2 ** 20)

Output lines are prefixed (StorageManager unavailable in this context, used MB:, persisted storage:), so each step’s effect shows up clearly in the output.

Core lines

const { usage = 0, quota = 0 } = await manager.estimate();
const persisted = await manager.persisted();

Interactive Tools for This Topic

FAQ

Does this require external packages?
No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. The file defines `used`, `cap`, `headroom` at load time.