Skip to content

Lesson 4 of 9 · 28m

IndexedDB as the Local System of Record

Design local persistence around object stores, transactions, schema versions, and user trust rather than treating IndexedDB as a bigger localStorage.

Offline-first starts when you stop asking how to cache responses and start asking where truth lives while disconnected. localStorage is too small, synchronous, stringly typed, and operationally fragile. IndexedDB is awkward, but it gives transactions, indexes, larger quotas, and versioned migrations. It is the local database your sync layer will either respect or corrupt.

Design around user work, not API responses

Store domain objects and intent, not raw endpoint dumps:

notes          local note records, keyed by id
outbox         mutations waiting for network delivery
metadata       schema version, sync cursors, device id
attachments    binary payloads and upload state

A note record carries both product data and sync metadata:

{
  "id": "01JDFZQ8N8M7M6N6QJ0D0F6S3H",
  "workspaceId": "ws_123",
  "title": "Launch checklist",
  "revision": 14,
  "deletedAt": null,
  "dirty": false
}

The client-generated id matters. If creation waits for a server ID, offline creation becomes a temporary-object mapping problem. ULIDs or UUIDs let the client own identity while the server remains the authority on acceptance and ordering.

Transactions are the safety boundary

IndexedDB transactions auto-commit when control returns to the event loop. Await outside a transaction and you can silently lose atomicity:

const tx = db.transaction(['notes', 'outbox'], 'readwrite');
await tx.objectStore('notes').put(updatedNote);
await tx.objectStore('outbox').put({
  mutationId: crypto.randomUUID(),
  entity: 'note',
  entityId: updatedNote.id,
  op: 'update',
  baseRevision: updatedNote.revision,
  state: 'pending',
});
await tx.done;

The core guarantee is that visible local state and durable intent commit together. If the note updates but the outbox write fails, the user sees work the sync engine cannot explain.

Migrations are application code

IndexedDB schema changes happen in onupgradeneeded with explicit versions. Never assume users migrate directly from the previous version; someone can open the app after eight months:

const db = await openDB('fieldnotes', 3, {
  upgrade(db, oldVersion, newVersion, tx) {
    if (oldVersion < 1) db.createObjectStore('notes', { keyPath: 'id' });
    if (oldVersion < 3) tx.objectStore('notes').createIndex('by_workspace', 'workspaceId');
  },
});

Write migrations as cumulative steps and test upgrade paths, not only fresh installs.

Indexes and eviction are product concerns

If the UI lists notes by workspace and updated time, create an index that supports it instead of scanning every object in memory. Browsers can evict storage under pressure; navigator.storage.persist() is a useful request, not a guarantee. The real protection is syncing promptly and explaining failures clearly.

Field notes

  • Surface quota, transaction-inactive, and constraint error classes in diagnostics without exposing private record content.
  • Binary attachments belong in object stores or the Origin Private File System, not base64 strings in JSON records.
  • Test storage eviction by filling quota on a real Android device; desktop browsers are far more forgiving.
  • Deletion should usually be a tombstone until the server acknowledges it.
  • crypto.randomUUID() requires a secure context and modern webview support.

Was this lesson helpful?

Sign in to rate