Browser storage is easy to underestimate. A developer starts with localStorage because it takes only a few lines. Then the app grows: records become larger, users expect offline access, and writes happen during important interactions. The original choice now shapes performance and reliability.

The practical rule: use localStorage for small string settings, IndexedDB for structured application data, and Cache Storage for network resources. None is a guaranteed backup or a replacement for a server database when data must follow a user across devices.

The quick answer

  • localStorage: theme, language and compact non-sensitive preferences.
  • IndexedDB: drafts, offline records, queued actions and larger structured datasets.
  • Cache Storage: app shells, scripts, stylesheets, images and selected network responses.
  • Server database: shared authoritative data requiring cross-device sync, server-side authorization or backups.

A PWA can use all three browser APIs together: Cache Storage for files needed to load, IndexedDB for user-created records, and localStorage for a simple display preference.

Where localStorage fits

localStorage is a persistent key/value store scoped to an origin. Keys and values are strings, so objects need serialization. Unlike sessionStorage, which is tied to a page session, localStorage normally remains after the browser closes.

// A small, non-sensitive UI preference
localStorage.setItem("theme", "dark");
const theme = localStorage.getItem("theme") ?? "light";

This is a reasonable fit for a theme choice or a compact onboarding flag. It is a poor fit for a growing collection of records or large JSON documents.

The performance catch: Web Storage reads and writes are synchronous. JavaScript waits for the operation, which can block the main thread. A few tiny values are usually fine; repeatedly parsing and rewriting a large JSON blob can make an interface feel slow. MDN recommends asynchronous alternatives such as IndexedDB for larger workloads.

The security catch: JavaScript executing in the page's origin can generally read localStorage. A cross-site scripting flaw can expose values stored there. Do not store passwords, session tokens, private keys or sensitive personal records in it. IndexedDB is not automatically a secret vault either: same-origin script access remains a concern.

Why IndexedDB is different

IndexedDB is an asynchronous, transactional database built into browsers. It stores structured records in object stores, supports indexes, and can handle datasets far beyond the small-string use case of Web Storage. It is useful for offline drafts, notes, queued form submissions, catalog records and data that the app needs to query.

// Example using the idb promise wrapper
import { openDB } from "idb";
const db = await openDB("raazim-app", 1, {
  upgrade(database) {
    if (!database.objectStoreNames.contains("drafts")) {
      database.createObjectStore("drafts", { keyPath: "id" });
    }
  }
});
await db.put("drafts", {
  id: "draft-001", title: "Offline notes", updatedAt: Date.now()
});
const draft = await db.get("drafts", "draft-001");

This example uses the third-party idb wrapper for a promise-based workflow. Native IndexedDB has a lower-level event-driven API; the wrapper is optional. Production code still needs to handle transaction failures, schema upgrades, blocked upgrades from other tabs, and quota errors.

Cache Storage is not your app database

The Cache API stores request/response pairs and is commonly managed by service workers. It is a good fit for an app shell, fonts, images, scripts, stylesheets and selected network responses. It is not a queryable record database. Use IndexedDB when you need to find or update structured objects by fields.

Quotas, eviction and the offline-data trap

There is no universal IndexedDB capacity that an application can safely assume. Browser quotas and eviction policies vary by browser, device, free disk space and browsing mode. Browser-managed data is generally best-effort by default: the browser may evict an origin's data under storage pressure, and users can clear site data themselves.

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

estimate() reports approximate usage and quota, not a guarantee of future capacity. persist() is a request, not a promise: the browser decides whether to grant it. Even persistent browser storage is not a backup. A lost phone, cleared browser profile or failed device can still mean lost local records.

Offline does not mean backed up.

If losing a record would hurt the user, synchronize it to a server when connectivity returns and provide an export or recovery path. Local browser storage should be treated as a working copy, not the only copy.

A practical decision guide

  • Theme or language preference: localStorage.
  • Long-form draft or offline notes: IndexedDB.
  • Offline product catalog: IndexedDB for records; Cache Storage for images and static files.
  • Installable PWA shell: Cache Storage managed by a service worker.
  • Data shared between phone and laptop: server database plus a local offline layer.
  • Large user-selected files: consider OPFS or the File System Access API where supported.

How to move beyond localStorage

  1. Inventory the data. Separate UI preferences, user records, authentication material and cached resources.
  2. Design a schema. Give records stable IDs and define indexes for actual lookup patterns.
  3. Put storage behind a module. Keep UI components independent of IndexedDB implementation details.
  4. Migrate once, safely. Read the old key, validate and transform it, write the new records in a transaction, then mark migration complete. Keep a recovery path if conversion fails.
  5. Handle failures honestly. Catch quota errors and do not show “saved” until the transaction completes.
  6. Test lifecycle behavior. Test offline mode, reloads, multiple tabs, schema upgrades, low storage, private browsing and export.

Mistakes worth avoiding

  • Storing the whole app database as one JSON string.
  • Assuming browser storage is permanent or synchronized.
  • Putting credentials or sensitive records in client-readable storage.
  • Using Cache Storage as a relational database.
  • Ignoring quota errors and silently losing user changes.
  • Adding offline mode without a conflict strategy for reconnecting edits.

Choose based on the data

Start with the shape and importance of the data, not the API that is easiest to type. Small preferences belong in localStorage. Structured offline records belong in IndexedDB. Network resources belong in Cache Storage. Shared and business-critical records need a server-side source of truth and a recovery plan.

For the next step, read our Progressive Web App guide to see how browser storage fits into an installable offline experience.

Sources and further reading

  1. MDN: Web Storage API — origin scoping and synchronous behavior.
  2. MDN: IndexedDB API — browser database interfaces.
  3. MDN: Storage quotas and eviction criteria — quotas, persistence and eviction.
  4. web.dev: Storage for the web — choosing storage mechanisms.
  5. MDN: Cache API — request/response caching.