SSR / foldkit-ssr

State the slice cannot carry

Parts: another package's state

Packages like foldkit-remote supply a part that captures only what active Surfaces read and restores it in the browser:

ts
const Post = SSR.plan(App, {
  id: 'post',
  state: Projection.pick(App.model.route),
  surfaces: [PostPageAt],
  parts: [Remote.resume(Data)],
})

Remote.resume serializes selected fields, relations, connection boundaries, and live cursors without re-fetching on boot. SSR.render verifies each part can restore cleanly before serving (UnrestorablePart).

Static regions: parts of the page the server owns

Wrap content that no Message updates (such as article prose or highlighted code) in SSR.static so the browser adopts the server's HTML without shipping its fields in state:

ts
const view = (model: Model, h: HtmlBuilder<Message>) => ({
  title: 'Post',
  body: h.main(
    [],
    [
      SSR.static('post-copy', ih => [
        ih.h1([], [model.post.title]),
        ih.p([], [model.post.body]),
      ]),
      PostActionsView(model, h),
    ],
  ),
})

ih is typed as HtmlBuilder<never>, preventing event handlers inside static markup (rendering a handler fails with BindingInStaticRegion). When styling a static region with foldkit-mixins Slots, strip the parent Message type from attributes using SSR.unbound:

ts
SSR.static('post-title', ih => [ih.h1(SSR.unbound(slots.title.attrs()), [model.post.title])])

If a later Message reveals a static region absent from the served HTML, the browser renders it and reports StaticRegionMissing.

What only the browser has: prepare

Browser-only state (such as a localStorage write queue or per-tab session id) joins in two stages so the first client render matches the server's HTML:

  1. SSR.hydrate's prepare(model) => Effect runs on the resumed Model before DOM adoption; keep this to state the view does not render (such as a tab session id).
  2. A Subscription reads the store after the first frame and updates the view through a normal Message.

For foldkit-remote/outbox, pass durable.session (never durable.restore) as prepare:

ts
import { outbox } from 'foldkit-remote/outbox'

const durable = outbox(Data, { key: model => `outbox:${model.userId}`, scope: model => model.userId })

SSR.hydrate(config, plan, { buildId, prepare: durable.session })

durable.session mints the tab's session id immediately so early writes queue safely, and the outbox Subscription delivers stored writes via OutboxRead after the first frame. See examples/reference.

Markup the browser adopts: SSR.serving

SSR.serving() returns true during SSR.render passes and false in the browser, allowing custom elements (such as foldkit-richtext-dom's editorAt({ serverRendered: SSR.serving })) to emit initial light DOM on the server.