Mirror / foldkit-mirror
The generic store seam
Mirror.url and Mirror.kv are built on Mirror.make(App, store, config), where a MirrorStore provides read and write:
ts
import type { Effect } from 'effect'
import type { Encoded } from 'foldkit-mirror'
interface MirrorStore<R = never> {
readonly read: Effect.Effect<Encoded | undefined, never, R>
readonly write: (
write: {
set: Encoded
remove: ReadonlyArray<string>
intent: 'push' | 'replace'
},
) => Effect.Effect<void, never, R>
}Built-in stores include MirrorStore.url(...) (browser URL), MirrorStore.kv(...) (Effect KeyValueStore), and MirrorStore.memory(...) (in-memory for tests), alongside applyToHref(href, { set, remove }) for pure URL updates. Custom stores hold writes until restore settles, just like Mirror.kv. Most applications should start with Mirror.url, Mirror.kv, or Mirror.cookie.
Limits
- Last-write-wins per store; two tabs writing one KV document overwrite rather than merge. Use
foldkit-syncwhen convergence matters. - Arrays and objects serialize as JSON in URLs by default; supply a custom
codecfor friendlier strings. - Multiple mirror kinds (such as URL and cookie) may observe the same field; cold-load precedence follows URL, then KV, then initial.
- URL mirrors touch only their declared keys, composing cleanly with Foldkit's
route.query(schema)on separate keys. - Cookies are sent with every request to their path and capped near 4 KB by browsers; keep cookies to small identifiers or preferences.
Next
- Read the conceptual guide and package-choice rules: Mirrored state.
- Persist offline edits with server convergence:
foldkit-sync. - Fetch and cache server-owned records:
foldkit-remote. - Inspect a live app using a linkable filter and remembered draft:
examples/todo-app.