SSR / Pages that work without JavaScript

A first form that works both ways

This creates a cart form with Resume.builder(h), lists Message.Added on an active Surface, and configures SSR.plan with fallback: 'server' so the server handles POST requests when JavaScript has not booted:

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

const Model = Schema.Struct({ cart: Schema.Array(Schema.String) })
const Message = defineMessageUnion({ Added: { name: Schema.String.check(Schema.isMaxLength(128)) } })
const App = Surface.application({ Model, Message, initial: { cart: [] }, update: m => ({ model: m }) })
const CartControls = App.surface('CartControls', {
  model: ({ model }) => ({ cart: model.cart }),
  messages: [Message.Added],
})
const plan = SSR.plan(App, {
  id: 'cart',
  state: Projection.pick(App.model.cart),
  surfaces: [Surface.at(CartControls, undefined)],
  fallback: 'server',
})
const view = (model: typeof Model.Type, h: HtmlBuilder<typeof Message.Type>) => {
  const rh = Resume.builder(h)
  return {
    title: 'Cart',
    body: rh.form([rh.OnSubmit(Message.Added({ name: 'acme.com' }))], [
      rh.p([], [`${model.cart.length} in the cart`]),
      rh.button([rh.Type('submit')], ['Add acme.com']),
    ]),
  }
}
export const { renderPage } = SSR.entry(
  { Model, init: () => ({ model: { cart: [] } }), update: m => ({ model: m }), view, container: null },
  plan,
  { buildId: 'build-1', origins: ['https://shop.example'] },
)

renderPage emits <form method="post"> containing a hidden foldkit-plus-message field encoding Added({ name: 'acme.com' }).

  • With scripts off: Clicking Add acme.com submits a standard POST to the page. SSR.entry checks Origin against origins (returning 403 on mismatch before reading the body), decodes and validates the Message against Message Schema and active Surfaces (400 if invalid), rebuilds the initial Model, runs update, and renders the updated page.
  • With scripts on: SSR.hydrate strips method="post" and the hidden field during boot so the first client render matches the served DOM, then handles OnSubmit in the browser without a network POST.

Because the server starts each POST from init, validate the Message payload (such as keeping name under 128 characters and verifying item prices in update) and persist multi-request state in a cookie or database.