SSR / foldkit-ssr

ssr

Standard server rendering runs init on the server to fetch a post, read a session, or compute initial state, then embeds those input Flags in the HTML so the browser runs init all over again on hydration. Once init does real work, that second run repeats server queries, flashes loading states, and leaks server-only data into the page.

foldkit-ssr hands the browser the slice of the Model the server already reached, instead of re-running init. You declare a resume plan listing the fields the browser owns; SSR.render runs init once, writes that slice onto the root element, and verifies at render time that the browser's first frame matches the server's HTML. SSR.hydrate then resumes directly from the rendered DOM.

See it running in examples/foldkit/ssr and Site, Remote and SSR together.

Install

sh
pnpm add foldkit-ssr foldkit-surface foldkit effect

A first resumed page

This declares a resume plan (PostPlan) that sends title and draft to the browser while keeping serverSecret on the server, then renders the page:

ts
import { Effect, Schema } from 'effect'
import type { HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import { SSR } from 'foldkit-ssr'
import { Projection, Surface } from 'foldkit-surface'

const Model = Schema.Struct({ title: Schema.String, draft: Schema.String, serverSecret: Schema.String })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Typed: { draft: Schema.String } })
const initial: Model = { title: 'Hello', draft: '', serverSecret: '' }
const App = Surface.application({ Model, Message, initial, update: m => ({ model: m }) })

export const PostPlan = SSR.plan(App, {
  id: 'post',
  state: Projection.pick(App.model.title, App.model.draft),
})

const config = {
  Model,
  init: () => ({ model: { title: 'Hello', draft: 'notes', serverSecret: 'internal-token' } }),
  update: (model: Model) => ({ model }),
  view: (model: Model, h: HtmlBuilder<typeof Message.Type>) => ({
    title: model.title,
    body: h.main([], [h.h1([], [model.title]), h.p([], [model.draft])]),
  }),
  container: null,
}

const result = await Effect.runPromise(SSR.render(config, PostPlan, { buildId: 'b1' }))

result.application.html contains the rendered root element stamped with both Foldkit's app attribute and the serialized resume envelope (without serverSecret):

html
<main data-foldkit-app="..." data-foldkit-plus-resume='{"v":1,"plan":"post","buildId":"b1","state":{"title":"Hello","draft":"notes"}}'>
  <h1>Hello</h1><p>notes</p>
</main>

Inject that result into your HTML template with SSR.page(template, result) on the server, and boot in the browser from 'foldkit-ssr/client':

ts
import { SSR as ClientSSR } from 'foldkit-ssr/client'

ClientSSR.hydrate(config, PostPlan, { buildId: 'b1' })

Import 'foldkit-ssr/client' in shared plan and view modules as well as browser entry points so client bundles exclude foldkit/experimental/server (saving about 200 kB minified / 60 kB gzipped in examples/foldkit/ssr).

  • SSR.plan declares which Model slice crosses to the browser.
  • SSR.render runs init once, renders the view against both the server Model and the browser's resumed Model, and fails with ResumeUnsafe if they differ (When a page is refused).
  • SSR.page injects the rendered application into your HTML template via Foldkit's injectIntoTemplate, with an optional head callback.
  • SSR.hydrate verifies the buildId and envelope, resumes the Model without calling init, adopts the DOM via Runtime.hydrate, and runs the plan's boot Commands.