Offline-first for the REST API you already have.
Two devices and one server, in your browser. Take a device offline, edit on both, bring it back, and watch every edit survive.
People edit on trains, in basements and in the field. When they reconnect, most apps do one of two things:
- Write their copy back whole. Whatever a colleague changed in the meantime is silently gone, even in fields this user never touched.
- Adopt a sync engine. It merges well, and it brings its own server, protocol or database with it. That is a rewrite, and not an option when the API belongs to someone else.
stitchback is the third way, and its name is what it does: it stitches
offline edits back into the server's state. It keeps a local copy of any
JSON resource, lets people edit it with or without a connection, and on
reconnect merges their edits field by field with what changed on the
server. The server stays exactly as it is: if it speaks GET, POST,
PUT and DELETE, it works.
- No backend changes. No sync endpoint, no metadata, no new database.
- Nothing lost silently. Edits to different fields always merge. There are two kinds of conflict: two different edits to one field, and an edit to something the other side deleted. You are told about both.
- Small. Plain ES modules, no dependencies, no build step. The table under Development names every module and its one job.
Every resource is kept as three versions of the same document:
| Version | What it is |
|---|---|
base |
The server's state at the last successful sync |
ours |
The local copy, with every edit made since |
theirs |
What the server holds right now, fetched at sync time |
A sync is a three-way merge, the mechanism git merge uses. Each
document is flattened into records keyed by
JSON Pointer, such as
/1/title, and each record is decided on its own: changed on one side
only, that side wins; changed the same way on both, nothing to do; changed
differently on both, a conflict, settled by a resolver you supply. A
missing record is a value like any other, so a deletion is a change and
merges by the same rules.
Deleted while edited. One conflict spans many records: one side deletes a post, or replaces an object with a value, while the other edits something inside it. Record by record the deletion would win unnoticed, because the post's own record looks untouched on the editing side. So these are settled first, a whole subtree at a time: the conflict is reported at the post, and the resolver keeps the deletion or the edited post.
Entities, not positions. An array whose elements all carry a unique
id is keyed by that id, so /42/title means post 42 wherever it sits.
Inserting a post renames nothing, and additions and removals from both
sides merge as sets. Any other array is a single value.
Writing back. Only the difference between the merge and theirs is
sent, in the shape REST expects. A collection is written entity by entity:
POST added, PUT changed, DELETE removed. A listing has no ETag per
entity, so each entity is read again just before its PUT or DELETE:
if it changed since the listing, or is gone, someone wrote first. Anything
else is one PUT. Every PUT and DELETE carries If-Match when the
server sent a strong ETag, so a 412 also means someone wrote first.
Either way stitchback fetches, re-merges and retries, up to three
attempts. Each created entity is recorded the moment the server answers
with it, under the id the server chose, so a sync that fails after a
create does not post it again; Edges names the one gap.
Afterwards a fresh GET becomes the new base.
Distributed state has two famous answers. CRDTs make every operation commute, so replicas converge with no coordinator, at the price of metadata on every value and a sync layer that understands it. Operational transformation needs a central server that transforms every operation.
A REST API already is a central server. It serializes writes and owns
the truth, so a client does not need commutativity; it needs to know
what it changed. Keeping base gives exactly that. ours against
base is the user's intent, theirs against base is everyone else's,
and a three-way merge combines the two while naming the only real
conflicts. HTTP has always named the failure this prevents: the "lost
update" (RFC 9110 §13.1.1).
If-Match refuses the stale write; stitchback goes one step further and
merges it.
Checked against each project's documentation on 2026-10-22.
| Approach | What the server needs | Concurrent edits |
|---|---|---|
| Replay queued requests, e.g. TanStack Query's paused mutations | Nothing new | Replayed in order on reconnect; the docs leave conflicts to the app (mutations guide) |
| Replication protocol, e.g. RxDB | Pull and push handlers and a change stream (RxDB replication) | Resolved per document by a handler; the default keeps the server's state |
| Sync engine, e.g. Zero | Its own sync service plus a push endpoint (Zero mutators) | Server-authoritative mutators, re-run on the server |
| CRDT library, e.g. Automerge or Yjs | A sync layer that speaks its format | Converge automatically, at the cost of a CRDT document format |
| stitchback | Nothing new | Merged field by field; every conflict reported |
Where it fits: forms, settings, CRUD screens and field-work apps on an API you cannot or would rather not change, where edits are occasional and a lost one is expensive.
Where it does not: real-time co-editing of the same text, where CRDTs such as Yjs are the right tool, and very large collections, which are merged whole on every sync.
npm install stitchbackstitchback is plain ES modules with no dependencies and no build step, so a page can also import it straight from a CDN:
import { openstore, get, edit, sync } from
"https://cdn.jsdelivr.net/npm/stitchback@0.1.0/src/index.js";Types ship in the package.
import { openstore, get, edit, sync, autosync } from "stitchback";
const db = await openstore("my-app"); // one database per replica
const url = new URL("https://api.example.com/posts");
await get(db, url, render); // render(local) at once, then render(synced)
await edit(db, url, posts); // save an edit locally; works offline
const { data, conflicts } = await sync(db, url, resolve);
// Or sync again on every return online and to the page:
const stop = autosync(db, url, { onsync: ({ data }) => render(data) });openstore(name)opens, or creates, the IndexedDB database that holds one replica's resources.closestore(db)closes it.get(db, url, ondata)callsondatawith the local copy straight away, if there is one, then syncs and calls it again with the result.local(db, url)returns the stored{ url, base, ours }, orundefinedbefore the first sync. Whereoursdiffers frombaseis what has not reached the server yet.edit(db, url, data)replaces the local copy. The resource must have been fetched once, so there is abaseto merge against.sync(db, url, resolve)merges and writes back. It returns the local copy after the sync and the conflicts it settled. Offline, it rejects with fetch'sTypeErrorand leaves the local copy untouched. If the server keeps changing under it for three attempts, it rejects with aStaleError. Any other refused request rejects with aRemoteErrorcarrying the HTTP status. Call it whenever you like: syncs of one resource run one at a time, across tabs too, and an edit made while one runs is kept for the next.autosync(db, url, { resolve, onsync, onerror })syncs now, then again whenever the browser comes back online or the page is shown again. Being offline stays quiet; any other failure goes toonerror. It returns a function that stops it. It does not sync after each edit; callsyncfor that.resolve(conflict)receives{ pointer, base, ours, theirs }. Each side is a record ({ kind: "value", value },{ kind: "object" }or{ kind: "list", order }) orundefinedfor deleted.resolvereturnsconflict.oursorconflict.theirs, the side to keep. The default,keepours, keeps ours.
Each of these is a design choice worth knowing:
- Same-field edits need a resolver. The default keeps the local edit and reports the server's value it replaced.
- Without strong
ETags, a small race window remains. A write that lands between stitchback's last read of an entity and its own write can be overwritten. A server that sends strongETags closes it. A weak one,W/"…", cannot:If-Matchcompares strongly (RFC 9110 §13.1.1), so stitchback does not send it. - Each changed or deleted entity costs one extra
GET, the read that guards its write. - A create cut off mid-answer can repeat. If the connection drops
after the server accepted a
POSTbut before its answer arrived, stitchback cannot tell, and the next sync posts the entity again. A server that answers aPOSTwithout the created entity leaves a wider gap: the create is recorded only when its sync completes. - Arrays without ids are atomic.
["a", "b"]merges as one value. - The resource must round-trip. What
GETreturns is whatPUTaccepts, so views such as?_embed=are read-only for this purpose. In a collection, each element of the listing must be what aGETof that entity returns. A listing that shows fewer fields would be written back short, so the guard read refuses it, and the sync rejects with aStaleError, rather than delete those fields. - Across tabs needs Web Locks. Every current browser has them, but
only on HTTPS or
localhost. Elsewhere syncs run one at a time within each page only.
Requires Node 22.12 or newer.
npm install
npm start # the demo at http://localhost:3000/
npm testnpm start also serves a real
json-server from a working
copy, data.json, so the seed in demo/db.json stays clean; delete
data.json to start over.
http://localhost:3000/?api=http://localhost:3000/posts points both
devices of the demo at it instead of the server in the page.
The tests run on Node's built-in runner. They cover the flattening, every
merge rule, the demo's server, and sync between replicas through a real
json-server: offline, deletion, conflicts, concurrent syncs, edits made
during a sync, a create cut off by a failure, writes racing another
client, servers with strong and weak ETags, and autosync. The run fails
below 100% line coverage of src and demo. Every push runs them on
GitHub Actions under Node 22, which has no Web Locks, and Node 24, which
has them; a green main publishes the live demo. Publishing a GitHub
release tagged v plus the version in package.json publishes the
package to npm.
| Module | Responsibility |
|---|---|
src/index.js |
the public API, typed by src/index.d.ts |
src/json.js |
flatten a document into records and back |
src/merge.js |
three-way merge of records |
src/state.js |
get, local, edit, sync, autosync |
src/storage.js |
one IndexedDB store of { url, base, ours } |
src/lock.js |
one sync at a time, through Web Locks |
src/fetch.js |
HTTP with ETag and typed errors |
index.html, demo/ |
the live demo and its in-page REST server |
MIT. See LICENSE.