Caching HTML and files gets your app to open offline. Letting people read and change data offline needs a local database and a sync plan.
Where to store data
| Storage | Good for | Notes |
|---|---|---|
| IndexedDB | Structured data, thousands of records, blobs | The main choice. Asynchronous, transactional, available in workers. |
| Cache API | HTTP responses | Great for files and pages, not for querying. |
| OPFS (Origin Private File System) | Large files, SQLite databases | Fast; works in all modern browsers. |
localStorage | Tiny settings | Synchronous and blocks the main thread; not available in service workers. |
IndexedDB's raw API is verbose, so most apps use a small wrapper:
- idb: a tiny promise wrapper that keeps the IndexedDB model.
- Dexie: a friendlier query API, live queries, and an optional sync service.
- localForage: a
localStorage-like API backed by IndexedDB.
import { openDB } from 'idb';
const db = await openDB('notes', 1, {
upgrade(db) {
const s = db.createObjectStore('notes', { keyPath: 'id' });
s.createIndex('updated', 'updated');
db.createObjectStore('outbox', { autoIncrement: true });
},
});
await db.put('notes', { id: crypto.randomUUID(), text: 'Buy milk', updated: Date.now() });
Writing offline: the outbox pattern
- Save the change locally first, and update the UI at once.
- Add the change to an outbox store.
- When online, send outbox items to the server in order, and remove each one when the server confirms.
async function save(note) {
await db.put('notes', note);
await db.add('outbox', { op: 'put', note });
flush();
}
async function flush() {
if (!navigator.onLine) return;
const tx = db.transaction('outbox', 'readwrite');
for await (const cursor of tx.store) {
const res = await fetch('/api/notes', { method: 'POST', body: JSON.stringify(cursor.value) }).catch(() => null);
if (!res || !res.ok) break;
await cursor.delete();
}
}
addEventListener('online', flush);
In Chromium browsers, Background Sync can run flush from the service worker even after the tab is closed. Elsewhere, flush on online, on visibilitychange and on app start.
Conflicts
When the same record changes in two places, you need a rule:
- Last write wins (by timestamp): simple, and fine for personal data.
- Server wins, and the app shows the server version.
- Field-level merge: merge different fields, flag real clashes.
- CRDTs: data types that merge automatically. Yjs and Automerge power collaborative editors that work offline.
Full sync engines
If your app is mostly data, a sync-capable database saves months: RxDB (reactive, with replication plugins), PouchDB (syncs with CouchDB), Dexie Cloud, or SQLite in the browser via SQLite Wasm on OPFS.
Tell people what is happening
Show "Saved on this device" and "Synced" states, and a count of unsent changes. People trust offline apps when they can see what will happen to their data.
ब्राउज़र सपोर्ट
| फ़ीचर | Chrome / Edge desktop | Chrome Android | Safari iOS / iPadOS | Safari macOS | Firefox desktop | Firefox Android | Samsung Internet |
|---|---|---|---|---|---|---|---|
| IndexedDB | Chrome / Edge desktop: हाँ | Chrome Android: हाँ | Safari iOS / iPadOS: हाँ | Safari macOS: हाँ | Firefox desktop: हाँ | Firefox Android: हाँ | Samsung Internet: हाँ |
| Origin Private File System | Chrome / Edge desktop: हाँ | Chrome Android: हाँ | Safari iOS / iPadOS: हाँ | Safari macOS: हाँ | Firefox desktop: हाँ | Firefox Android: हाँ | Samsung Internet: हाँ |
| Background Sync | Chrome / Edge desktop: हाँ | Chrome Android: हाँ | Safari iOS / iPadOS: नहीं | Safari macOS: नहीं | Firefox desktop: नहीं | Firefox Android: नहीं | Samsung Internet: हाँ |