Skip to content
Optimal Lab

Frontend · interactive

Browser storage

Browsers offer five places to keep data. They differ in size, lifetime, whether they're sent to the server, and whether scripts can read them. That last one matters most for security. Pick what you're storing.

sizesent to serverAPIreadable by JSlasts
Cookies~4 KB eachwith every requestsync (document.cookie)can be hidden (HttpOnly)until expiry
localStorage~5 MBneversyncalways readable by JSuntil cleared
sessionStorage~5 MBneversyncalways readable by JSuntil the tab closes
IndexedDBhundreds of MB+neverasyncalways readable by JSuntil cleared or evicted
Cache APIlargeneverasync (service workers)readable by JSuntil cleared or evicted

An HttpOnly, Secure, SameSite cookie is sent automatically and can't be read by injected scripts. Never put tokens in localStorage.

You need to store

Step 1 of 4

  1. 1

    Secrets don't belong where scripts can read them

    Any XSS can read localStorage. HttpOnly cookies are invisible to JavaScript.

    Set-Cookie: sid=…; HttpOnly; Secure; SameSite=Lax

  2. 2

    localStorage and sessionStorage

    Simple string key-value stores. Synchronous, so keep them small.

    Wrap access in try/catch: private modes can throw.

  3. 3

    IndexedDB for lots of structured data

    Async, indexed, transactional. Use a wrapper like idb or Dexie.

    Ask navigator.storage.persist() to avoid eviction.

  4. 4

    Cache API with a service worker

    Store whole responses so the app loads offline.

    Workbox simplifies strategies.

In production

Tools and technologies

Every web app stores something in the browser.

Not tied to one language — use these names when you search docs or design a system.