@stesura/collab-adapter-local
An offline collab backend with no server: the authority runs in the browser against IndexedDB,
and tabs of one browser co-edit in real time. Mount it with useLocalCollabEditor from
@stesura/collab-adapter-react; behaviour and caveats are in
Local (offline, multi-tab).
Backend
createLocalCollabBackend
useLocalCollabEditor uses a shared backend per schema by default. Create your own to set
dbName, snapshotInterval, migrations or createInitialDoc, and keep it referentially stable:
import { createLocalCollabBackend } from "@stesura/collab-adapter-local";
const backend = createLocalCollabBackend({ schema, dbName: "my-app", schemaVersion: 2, migrate });The fully offline collab backend: the pitter-patter engine running entirely
in the browser, with IndexedDB in place of Postgres, BroadcastChannel in
place of Redis, and createLocalAuthority in place of
@pitter-patter/collab-server. Multiple tabs on the same dbName co-edit
with the same guarantees server collab gives separate clients.
| Name | Type | Default |
|---|---|---|
__0 | LocalCollabBackendConfig |
LocalCollabBackendConfig
Options for createLocalCollabBackend.
LocalCollabBackend
What createLocalCollabBackend returns: the in-browser counterpart of the server backend.
MigrateFn
Upgrades a stored document from schema version from to to (schemaVersion), run when a doc
is read under a newer schemaVersion than it was stored with. The doc’s commits are discarded.
| Name | Type | Default |
|---|---|---|
docJSON | NodeJSON | |
from | number | |
to | number |
Other exports
| Export | Description |
|---|---|
DEFAULT_DB_NAME | "stesura-collab-local", the database used when dbName is omitted. |
DocListEntry | One row of listDocs(): { docId, version, updatedAt }. |
DocSnapshot | { docJSON, version, lastUpdatedTimestamp }, the same shape as the server’s. |
DocRow, SeededDocRow | A stored doc row: a snapshot at snapshotVersion, the head version and schemaVersion; seeded rows always carry their lineage nonce. |
LocalListener | The CommitsListener the hook polls with; it surfaces DocDeletedError and StaleClientError. |
LocalCommitBatch | What listenForCommit resolves to: commits plus the row’s lineage nonce. |
Errors
Each sets name, so match by name rather than instanceof across bundles. useLocalCollabEditor
handles the ones it can recover from.
| Error | Meaning |
|---|---|
SchemaMigrationRequiredError | Stored schemaVersion differs and no migrate applies |
DocMigratedError | Another tab migrated the doc under this one; reload (the hook does) |
DocDeletedError | The doc row is gone (hook: docStatus: "deleted") |
StaleClientError | The client is behind the retained commits; reload (the hook does) |
LocalStoreCorruptError | The retained commits can’t rebuild the head; terminal |
LocalStoreConfigError | The schema can’t build an initial document; configuration error |
LocalBackendClosedError | Used after close() |
LocalContentionError | Lost the IndexedDB version race past the retry budget; treat as a bug |
SnapshotNotFoundError | A history version has no snapshot: never written, or deleted with its doc |