Guides
Offline and multiple tabs
Keep the data and unsaved changes in the browser, so the app opens instantly and works offline, and let tabs share one connection.
IndexedDB
"use client";
import { createDatabaseClient, indexedDb } from "@clientdb/client";
import { schema } from "./schema";
export const db = createDatabaseClient({
schema,
storage: indexedDb(),
});indexedDb() needs no options. Each signed-in user gets their own copy automatically: the client stores the session the server last verified (viewer, claims, read-only) next to the data. If the server later reports a different viewer, the client deletes the local data and loads the new user's. A change of claims keeps the data: the server's next sync removes the rows the session can no longer read.
On sign-out, call await db.$clear(). It deletes the local data and pending writes, so the next person on a shared computer doesn't inherit them. Every other open tab of the app drops its data too, and stops writing before the storage is cleared; each starts again, empty, on next use.
What's stored: every row on the device as last confirmed by the server (the eager models, and the lazy rows loaded so far), how far it has synced, and every change not yet confirmed (so nothing is lost if the tab closes). Rows evicted from memory by eviction are removed from storage too.
On reload:
- the client starts offline-first:
db.$ready()resolves from disk, before any network request, and lazy rows loaded before are there without a round trip; - it resumes from its stored sync id;
- unsent writes are re-applied and resent, and the server deduplicates them.
After a schema change: if a model's shape changed, only that model's rows are dropped and refetched (GET …/bootstrap?models=…) before the client resumes syncing. db.$ready() doesn't wait for the refetch. Until it succeeds, the model is marked stale on disk and the client retries with backoff, so a refetch that fails offline isn't lost.
Layout: fixed, independent of your schema. The Swift client uses the same logical layout in SQLite. A clientdb release that changes the layout drops the stored rows and loads them again, but keeps the unsent changes.
Multi-tab
Multiple tabs work automatically whenever storage is set; there's no option for it.
import { db } from "@/sync/db";
db.$role; // "leader" | "follower"How it works:
- Every tab saves its changes to the shared storage right away, so they survive the tab closing.
- One tab is elected leader with Web Locks. It owns the connection and the stored rows, sends what the journal holds, and broadcasts server packets to the other tabs over
BroadcastChannel. - Followers'
db.$sync()calls go through the leader. - A tab that opens asks the leader for its rows, so it doesn't miss a packet the leader broadcast before the tab listened but hadn't stored yet. When the leader loads afresh (after the server lost its position, or another viewer signed in), followers replace their rows with the leader's in the same way.
- When the leader tab closes, another tab takes over. Changes made in any tab survive both the tab closing and the handover.
Offline behaviour
- Reads of rows on the device always work: they come from memory. A lazy model's rows that were never loaded stay
loadinguntil the network is back. - Writes always work: they apply locally and wait until the network is back. (On a read-only client they throw, online or not.)
db.$statusisofflinewhile the network is down; the client retries with backoff and reports errors throughonError.- When access is lost while offline, transactions not yet sent that write rows evicted on reconnect are rejected whole with
AccessLost.