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
POSTto the page.SSR.entrychecksOriginagainstorigins(returning403on mismatch before reading the body), decodes and validates theMessageagainstMessageSchema and activeSurfaces (400if invalid), rebuilds the initialModel, runsupdate, and renders the updated page. - With scripts on:
SSR.hydratestripsmethod="post"and the hidden field during boot so the first client render matches the served DOM, then handlesOnSubmitin the browser without a networkPOST.
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.