All of the guide

Forms

A form is an ordinary <form method="post">. Turbo submits it with fetch and expects one of three answers:

The form Answer with
Succeeded A 303 redirect to the next page: c.redirect("/", 303). Turbo follows it
Succeeded, and you want to change just part of the page Turbo Streams (below)
Has errors Status 422 with the form rendered again, errors included. Turbo shows it

A 200 page in answer to a POST is not shown. Turbo stays on the current page and the form looks dead. That is the most common mistake.

The example's form

routes/messages/index.tsx answers POST /messages:

export const POST = defineHandler(async (c) => {
  const form = await c.req.formData();
  const body = String(form.get("body") ?? "").trim();
  const standalone = form.has("standalone");
  const inline = acceptsStream(c) && !standalone;

  if (!body) {
    // inline: a stream that replaces the form with its error, status 422
    // standalone: the whole page again, status 422
  }

  const message = await createMessage(body, getUser());
  // … broadcast it (see Live updates), then:
  if (inline) return streamResponse(c, <>{prepend}{newForm}</>);
  return refreshOrRedirect(c, "/");
});

The same form appears in two places. On the messages page it is inline, so the answer changes the page in place. On /messages/new it is standalone, its own page and a modal in the phone apps, so the answer is a redirect. A hidden standalone field tells them apart.

Turbo Streams

When Turbo submits a form, its Accept header includes text/vnd.turbo-stream.html. If you answer with that content type, Turbo applies each <turbo-stream> element in the answer instead of navigating:

<turbo-stream action="prepend" target="messages">
  <template><li id="message_42">…</li></template>
</turbo-stream>
<turbo-stream action="replace" target="message_form">
  <template><form id="message_form">…</form></template>
</turbo-stream>

src/turbo.tsx has the helpers: acceptsStream(c), the <Stream> component, and streamResponse(c, streams, status). The actions are append, prepend, replace, update, remove, before, after and refresh. target is an element's id, and targets is a CSS selector.

Give every element a stream changes a stable id (message_${id}), and render it with the same component everywhere (the page, the stream, the broadcast). The example uses MessageItem for all three.

Reference: https://turbo.hotwired.dev/handbook/streams

After the form

reset_form_controller.ts empties the form when Turbo reports success:

<form data-controller="reset-form" data-action="turbo:submit-end->reset-form#reset">

Add data-turbo-submits-with="Sending…" to a submit button to relabel it while the request runs, and data-turbo-confirm="Sure?" to the form to ask first.

A notice

After a message is sent, the page says so: "Message sent.", in a daisyUI toast (Notice, in #notices at the end of every page), which fades after a few seconds. The inline form's stream answer brings it (<Stream action="update" target="notices">). The form on its own page redirects, so it leaves the notice in a short-lived cookie for the page it goes to (leaveNotice in src/notice.ts), which takes it (takeNotice): shown once, and not to a Turbo prefetch of that page. It is data-turbo-temporary, so a page Turbo restores from its cache doesn't show it again. In the phone apps it is a native toast instead (Hotwire Native).

Seeing your own message

The messages are rows in D1, which answers reads from a replica near each request (database: { readReplication: true } in void.config.ts). A replica can be a moment behind the primary, so the page a form redirects to could miss the message it just saved. D1's answer is a bookmark: the point in the data a request has seen. Given one, D1 answers from a replica only once it has caught up to it.

middleware/01.bookmark.ts carries it in a cookie, as Void's D1 guide does: it starts each request's D1 session from the cookie's bookmark (setSession), and after the request sets the cookie to the new one (getBookmark). Turbo sends cookies with every visit, frame and form, as the browser does, and the phone apps' web views do too: nothing in the browser handles the bookmark.

The footer of every page, Edge, shows the Cloudflare data center that answered (from request.cf) and the bookmark the page was made at. The inline form's stream answer replaces it too (<Stream action="replace" target="edge">), as the page is not rendered again.

The cookie is one value for all of a browser's tabs: two answers at once can leave the older bookmark in it. Void's guide has a header recipe for API clients that need the newest.

Check

curl -i -X POST -H 'Accept: text/vnd.turbo-stream.html' --data-urlencode body=hi https://hotwire.localhost:5173/messages
curl -i -X POST -H 'Accept: text/vnd.turbo-stream.html' --data-urlencode body= https://hotwire.localhost:5173/messages

The first returns 200 with Content-Type: text/vnd.turbo-stream.html, and the second returns 422. vp test checks both (Testing).

When it does not work

Symptom Cause
Nothing happens on submit The server answered 200 HTML. Answer 303, 422 or a stream
Errors are not shown The status is 200; use 422
A stream answer changes nothing Wrong Content-Type, content not inside <template>, or no element with the target id
A standalone form shows nothing after success It got a stream for elements that page does not have; it wants a redirect
A saved message is missing on the next page, deployed The bookmark cookie was not sent back: the middleware is missing, or another response's Set-Cookie replaced it
A notice shows on every visit, or never The cookie is taken by another request first (a frame, a prefetch), or the page doesn't call takeNotice
no such table: messages The dev server started before the migration existed: start it again, which applies it