SSR / foldkit-ssr

Forms that work without scripts

Forms built with rh.form and rh.OnSubmit(Message) can execute update on the server when JavaScript is blocked or still loading by setting fallback: 'server':

ts
const Todos = SSR.plan(App, {
  id: 'todos',
  state: Projection.pick(App.model.draft, App.model.todos),
  surfaces: [Surface.at(TodoList, undefined)],
  fallback: 'server',
})
ts
rh.form(
  [rh.OnSubmit(Message.Added({ title: model.draft }))],
  [rh.input([rh.Name('title'), rh.Value(model.draft), rh.OnInput(Message.Typed)])],
)

With scripts running, Foldkit handles OnSubmit in the browser. Without scripts, the server emits method="post" and a hidden Message field; on POST, SSR.handle decodes the Message, merges posted form fields (like title), verifies it against active Surfaces (400 FallbackRefused on invalid input), runs init, prepare, boot, update, and up to 100 follow-up Commands (providing request-scoped resources over config.resources), and returns the updated page (500 if a Command fails).

Give plain search forms rh.Method('get') so they submit query parameters instead of posting a Message.

Every SSR.entry using fallback: 'server' requires origins (such as origins: ['https://example.com'] behind reverse proxies, or 'same-origin' in local dev) and rejects unmatched Origin / Sec-Fetch-Site headers with 403 before reading the body.

Answering a post with a redirect

Set answerPost: 'redirect' (Post/Redirect/Get) so non-idempotent posts answer 303 to the same path and browser reloads send GET instead of re-posting:

ts
SSR.entry(config, Todos, {
  buildId,
  origins: ['https://example.com'],
  answerPost: 'redirect',
  headers: (_request, model) =>
    Option.match(model, {
      onNone: () => ({}),
      onSome: ({ todos }) => ({
        'set-cookie': `todos=${encodeURIComponent(JSON.stringify(todos))}; Path=/; HttpOnly; SameSite=Lax`,
      }),
    }),
})
  1. POST /todos runs update and Commands, writes cookies via headers, and responds 303 with relative Location: /todos.
  2. GET /todos runs init and prepare (reading the cookie or store) and renders the new page.

A whole page without scripts

See Pages that work without JavaScript and examples/foldkit/domains-plus for combining prepare, GET forms, (scripting: none) styles, cookies, and E2E tests.