Mirror / foldkit-mirror
mirror
You want a todo filter, page number, open panel, or unsaved draft in your Foldkit app to survive a reload or live in a shareable URL like ?filter=active&page=2. Putting that state directly in location.search or localStorage as a second store forces components to read two places and risks async storage overwriting newer keystrokes; keeping it only in memory loses it when the page refreshes.
foldkit-mirror keeps selected fields of your Foldkit Model mirrored in the URL, a KeyValueStore, or a browser cookie while your Model stays the single source of truth. Your update writes to the Model as usual, a Subscription writes changes out when the encoded slice changes, and startup or navigation reads them back in.
See a filter mirrored in the URL in the live todo demo, or read Mirrored state for the full guide.
Choose between Mirror, Sync, and Remote
A mirror is a secondary representation for disposable, per-device state such as filters, sort order, page numbers, open panels, preferences, and drafts. It is last-write-wins with no durable log; use foldkit-sync when edits from multiple devices must survive offline and converge, and foldkit-remote when the server owns the data.
| State | Owner | Representation |
|---|---|---|
| Filter, sort, page, search text, open panel | local Model | URL via Mirror.url |
| Preference, draft, collapsed sidebar | local Model | KeyValueStore via Mirror.kv |
| Cart or preference rendered by the server | local Model | cookie via Mirror.cookie |
| Shared/offline edits that must converge | foldkit-sync / Durable | Sync journal |
| Server-derived facts | server / foldkit-remote | Remote cache |
Every field keeps one owner: a mirror observes a local field and writes restored values into that field without claiming ownership away from your update.
How URL and store mirrors update the Model
URL and key-value mirrors share the same ownership rule, but read back into the Model at different times:
- URL mirror: When your
Modelchanges the filter,Filters.subscriptionsupdates the URL viahistory.pushStateorreplaceState. On navigation, the runtime sendsUrlChanged, andFilters.urlChanged(model, url)reduces the URL back into theModel. The initial URL is already available at startup. - Key-value mirror: On a cold load, the
Prefs.restoreCommand reads theKeyValueStoreand sendsMirrorRestored, whichPrefs.reduce(model, message)applies to fields still at their initial values. Each laterModelchange writes outward throughPrefs.subscriptions.
Every mirror integrates through three seams: reduce reads an external representation back into the Model, restore requests a store's cold-load value, and subscriptions writes Model changes outward.
Install
pnpm add foldkit-mirrorfoldkit and effect are peer dependencies; foldkit-surface comes with the package.
A first URL mirror: keep a filter in the URL
This mirrors filter and page into the query string, omitting fields still at their defaults and reading a URL back into the Model:
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Url } from 'foldkit/url'
import { Surface } from 'foldkit-surface'
import { Mirror } from 'foldkit-mirror'
const Model = Schema.Struct({
filter: Schema.Literals(['all', 'active', 'done']),
page: Schema.Number,
})
const Message = defineMessageUnion({ UrlChanged: { url: Url } })
const initial: typeof Model.Type = { filter: 'all', page: 1 }
const App = Surface.application({
Model,
Message,
initial,
update: (model: typeof Model.Type) => ({ model }),
})
const Filters = Mirror.url(App, {
name: 'filters',
fields: [App.model.filter, App.model.page],
})
const activeUrl = Filters.href(initial, { filter: 'active', page: 2 }, '/todos')
// '/todos?filter=active&page=2'
const restored = Filters.reduce(initial, '/todos?filter=done&page=3')
// { filter: 'done', page: 3 }What happened:
Mirror.urlderives parameter names, default values ('all'and1), and text codecs directly fromApp.model.filterandApp.model.page.Filters.hrefproduces'/todos?filter=active&page=2'; when fields return to their defaults ({ filter: 'all', page: 1 }),Filters.hrefomits them and produces'/todos'.Filters.reducedecodes'/todos?filter=done&page=3'back into{ filter: 'done', page: 3 }.
When a mirror writes to the browser URL, Foldkit emits a UrlChanged echo for that write; Filters.urlChanged tracks every in-flight write and returns the Model unchanged so echoes never overwrite newer keystrokes or trigger a second render.