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
- tuts/web/apis Linked here; links to this page; shared tags: #apis, #platform, #web
- tuts/web/apis/fetch_and_networking Linked here; links to this page; shared tags: #apis, #platform, #web
- tuts/web/apis/observers_and_workers Linked here; links to this page; shared tags: #apis, #platform, #web
- tuts/web/security links to this page; shared tags: #platform, #web
- tuts/web/security/csp_cookies_and_mitigations links to this page; shared tags: #platform, #web
- tuts/javascript/builtins/json_math_and_date Linked here; shared tags: #web
- tuts/databases links to this page
- tuts/databases/nosql links to this page
- tuts/databases/nosql/nosql_data_models links to this page
- tuts/python3/tutorials/popular_application_domains/web_development_and_apis shared tags: #apis, #web