On this page
  1. Storage & State
  2. Contents
  3. Choosing Client Storage
  4. localStorage & History API Examples

Storage & State

Prev: Fetch & Networking | Up: apis | Next: Observers & Web Workers
Rel: JSON, Math, BigInt & Date, Fetch & Networking
Ref: MDN Web Docs โ€” Web Storage API
Tags: #public #web #platform #apis #storage


Contents


Choosing Client Storage

API Async Typical Capacity Best For
localStorage No (sync) ~5โ€“10 MiB Small string preferences (theme, UI flags)
sessionStorage No (sync) ~5โ€“10 MiB Per-tab transient state
IndexedDB Yes Large (origin quota) Structured records, blobs, offline datasets
Cache API Yes Large (origin quota) HTTP Request/Response caching (Service Workers)

localStorage & History API Examples

// Persist lightweight JSON preferences
const prefs = { theme: "dark", compact: true };
localStorage.setItem("app:prefs", JSON.stringify(prefs));
const loaded = JSON.parse(localStorage.getItem("app:prefs") ?? "{}");

// Update URL query state without a full page reload
const nextUrl = new URL(window.location.href);
nextUrl.searchParams.set("tab", "details");
history.pushState({ tab: "details" }, "", nextUrl);

Linked from

Related notes