Sync / foldkit-sync
sync
When a user checks off a todo or edits a note while offline or on a flaky connection, waiting for the server makes the UI stall—while building your own local queue, optimistic cache, retry loop, and reconciliation reducer beside Foldkit's update splits your state machine in two.
foldkit-sync lets you pick which fields of your Foldkit Model are shared across devices and which existing Messages change them. Each edit runs through your update immediately, persists to a local IndexedDB outbox, syncs to the server journal when connected, and replays cleanly on top of commits from other devices—keeping Model, Message, and update as your single state machine.
See offline sync running live in the registry demo and the collaborative pages demo, or read Replicated state for client and server end to end (foldkit-durable provides the matching SQLite server journal).
Choose between Sync, Remote, and Durable
| State | Owner | Package |
|---|---|---|
| A route, selection, or transient error | local Model | plain Foldkit |
| Facts the server owns and clients cache | server | foldkit-remote |
| User edits that survive offline and converge | durable log | foldkit-sync |
| Authoritative commit order on the server | server journal | foldkit-durable |
Use foldkit-remote when refetching recovers server state, and foldkit-sync when client-authored edits live in the outbox and replay is recovery. foldkit-sync orders every document through one authoritative server log (for peer-to-peer merging without a server, use a CRDT instead).
How optimistic edits rebase onto server commits
Your visible state is always the server's committed snapshot with your pending local edits replayed on top.
Suppose the server has committed A, B, C, and your tab edits D, E offline; you immediately see replay(A, B, C, D, E). If another device commits X before D reaches the server, your next sync receives X and replays your pending edits on top: replay(A, B, C, X, D, E). That rebase also handles rejections: drop the rejected edit from pending and replay the rest.
Each edit passes three observable milestones:
| Milestone | Where | What it proves |
|---|---|---|
| visible | Model, at once | Shown on screen right away |
| saved | local outbox | Reloads and dropped connections keep it |
| committed | server journal | Every device sees it in this order |
Show "saved" from local persistence and "shared" from commit status; see what to show the user.
Install
pnpm add foldkit-syncfoldkit and effect are peer dependencies; foldkit-surface comes with the package. Browser storage uses IndexedDB, and the server uses foldkit-durable or a custom implementation of the exchange.
A first synced todo list
This declares a Foldkit todo app whose todos list syncs via CreatedTodo while selectedTodoId and lastError stay local to this tab:
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { MessageSet, Projection, Surface } from 'foldkit-surface'
import { DocumentId, forApplication } from 'foldkit-sync'
const Model = Schema.Struct({
todos: Schema.Array(Schema.Struct({ id: Schema.String, title: Schema.String })),
selectedTodoId: Schema.Option(Schema.String),
lastError: Schema.Option(Schema.String),
})
const Message = defineMessageUnion({
CreatedTodo: { id: Schema.String, title: Schema.String },
SelectedTodo: { id: Schema.String },
})
const initial: typeof Model.Type = { todos: [], selectedTodoId: Option.none(), lastError: Option.none() }
const App = Surface.application({
Model, Message, initial,
update: (model, message) => Message.match(message, {
CreatedTodo: ({ id, title }) => ({ model: { ...model, todos: [...model.todos, { id, title }] } }),
SelectedTodo: ({ id }) => ({ model: { ...model, selectedTodoId: Option.some(id) } }),
}),
})
const TodoSync = forApplication(App).make({
documentId: DocumentId.make('todos'),
shared: Projection.pick(App.model.todos),
durable: MessageSet.make(App, [Message.CreatedTodo]),
})
TodoSync.durable(Message.CreatedTodo({ id: 't1', title: 'Milk' })) // true
TodoSync.journalContract().empty() // { todos: [] }What TodoSync defines:
| Member | Scope | Behavior |
|---|---|---|
todos | shared | Replicated, persisted, journaled |
selectedTodoId, lastError | local | Kept in this tab only |
CreatedTodo | durable | Queued in outbox, committed by server |
SelectedTodo | local | Runs through update locally |
From that single declaration, make derives the shared codecs, empty snapshot ({ todos: [] }), wire envelope, deterministic replay via your update, and the server's journal contract. TodoSync is a pure declaration; opening IndexedDB and mounting the app happen next.