With Turbo, what does a <turbo-stream> element do, and which actions can it apply to its target?
answer
- a custom element that runs, then removes itself
- action attribute plus target or targets
- new content travels inside a <template>
- eight actions; refresh arrived in Turbo 8
- append and prepend drop same-id children first
basics
~20 sA <turbo-stream> element is a page instruction: Turbo applies its action (append, prepend, before, after, replace, update, remove or refresh) to the element whose id is in target, or every match of the targets selector, using its <template> content.
solid answer
~40 sA `<turbo-stream>` is a custom element that Turbo executes as soon as it is connected to the document and then removes. Its `action` attribute names one of eight actions: `append` and `prepend` insert the template's content inside the target, `before` and `after` insert it as a sibling, `replace` swaps the whole target element, `update` swaps only its children, `remove` deletes the target, and `refresh` asks for a page refresh. `target` holds one element id; `targets` holds a CSS selector, so one stream can change every match. The new HTML travels inside a `<template>`. Streams arrive in a form response, over a WebSocket broadcast, or embedded in rendered HTML, and the same server partials render all three. An action Turbo does not know raises an error unless you register it as a custom action.
code
html · 11 lines<turbo-stream action="prepend" target="tickets">
<template>
<article id="ticket_42">Table 7: 2x risotto</article>
</template>
</turbo-stream>
<turbo-stream action="remove" target="ticket_17"></turbo-stream>
<turbo-stream action="update" targets=".grill-count">
<template>4</template>
</turbo-stream>go deeper
Recall the eight action names and which ones work inside the target (append, prepend, update) versus around or on it (before, after, replace, remove). Know that target is an id and targets is a CSS selector.
Explain the element's lifecycle: connectedCallback, turbo:before-stream-render, the action, then self-removal. Be ready to say why a missing target fails silently and how append de-duplicates by id.
Show you can debug a stream that did nothing: check the response media type, the target id against the partial's root, and whether a duplicate id moved an element. Know when a custom action beats a Stimulus controller.
Weigh the small action vocabulary as a design constraint: it keeps server templates the single source of markup and pushes behaviour into Stimulus, at the cost of occasionally registering custom actions a team must document.
## What a stream element is Turbo Streams are the part of Turbo that changes **pieces** of the current page instead of replacing the whole body (Turbo Drive) or one framed region (Turbo Frames). The unit is the `<turbo-stream>` **custom element**. When the browser connects one to the document, its `connectedCallback` runs: Turbo dispatches the cancelable `turbo:before-stream-render` event, performs the action, and then removes the `<turbo-stream>` element itself. Nothing is left behind except the change it made. Every stream element carries three things: - an **`action`** attribute naming what to do; - a **`target`** (one element id) or **`targets`** (a CSS selector) naming where to do it; - a **`<template>`** child holding the HTML to insert (empty or absent for `remove` and `refresh`). ## The eight actions Turbo 8 ships exactly eight actions, defined in its `StreamActions` object: | action | effect on each target | |---|---| | `append` | adds the template content as the target's last children | | `prepend` | adds it as the target's first children | | `before` | inserts it as a sibling just before the target | | `after` | inserts it as a sibling just after the target | | `replace` | swaps the whole target element, its id included | | `update` | empties the target and fills it, keeping the element | | `remove` | deletes the target element | | `refresh` | asks Turbo to refresh the current page | `replace` and `update` also read a `method` attribute: `method="morph"` morphs instead of swapping. That belongs to the morphing feature, and there is **no** separate `morph` action. Some prose in the Turbo handbook still speaks of nine actions with morph among them; the source defines eight. ## target versus targets - `target="tickets"` looks up `document.getElementById("tickets")`, one element. - `targets=".ticket.served"` runs `querySelectorAll`, so one stream can remove every served ticket on a kitchen order board at once. - If neither attribute is present, Turbo throws ("target or targets attribute is missing") and logs the error. - If the attribute is present but **nothing matches**, the action silently does nothing. That is the usual cause of a stream that "did not work": the partial's root element lacks the id the stream targets. ## Behaviours worth knowing 1. **De-duplication by id.** Before `append` or `prepend`, Turbo removes any existing child of the target whose id matches a top-level element in the template; `before` and `after` do the same for siblings. Appending `<article id="ticket_42">` to a board that already shows `ticket_42` therefore moves it to the end with fresh content rather than duplicating it. That matters when the cook who created a ticket receives it twice: once in the form response and once in the broadcast. 2. **Unknown actions throw.** An `action` value that is not in `StreamActions` raises "unknown action". 3. **Streams inside HTML run too.** Because execution happens in `connectedCallback`, a `<turbo-stream>` rendered into a page or a frame response executes when it lands. 4. **Order of work.** The cancelable `turbo:before-stream-render` event fires first, so a listener can veto a stream or wrap its `render` function; Turbo then waits for the next repaint before performing the action. ## Where the elements come from The element does not care how it arrived: - an HTTP response with the media type `text/vnd.turbo-stream.html` (a Rails controller's `format.turbo_stream`); - a WebSocket message from Action Cable (turbo-rails' `turbo_stream_from` subscriptions and model broadcasts); - markup already in a rendered page. On the Rails side the `turbo_stream` tag builder writes the elements for you: `turbo_stream.append "tickets", @ticket` renders the ticket's ordinary `_ticket.html.erb` partial into the template, and `turbo_stream.remove @ticket` targets `dom_id(@ticket)`, such as `ticket_42`. ## Example on a kitchen order board ```html <turbo-stream action="prepend" target="tickets"> <template> <article id="ticket_42">Table 7: 2x risotto</article> </template> </turbo-stream> <turbo-stream action="remove" target="ticket_17"></turbo-stream> <turbo-stream action="update" targets=".grill-count"> <template>4</template> </turbo-stream> ``` The first adds a new ticket at the top of `#tickets`, the second clears a served ticket, the third rewrites the counter text in every element with class `grill-count` while keeping those elements themselves. ## Why the vocabulary is small The handbook deliberately limits streams to DOM changes and points behaviour at Stimulus controllers. When a team really needs another verb (play a chime, highlight a row), Turbo lets it register a **custom action** rather than smuggling scripts into templates.
- How do you make a Turbo Stream do something the eight actions cannot, such as chime when a ticket arrives?Register a custom action: import `StreamActions` from `@hotwired/turbo` and add a function, `StreamActions.chime = function () { ... }`, where `this` is the stream element (`this.targetElements`, `this.templateContent`, `this.getAttribute`). On the Rails side `turbo_stream.action(:chime, "tickets")` writes the tag, or you add a `chime` method to the tag builder in an `ActiveSupport.on_load(:turbo_streams_tag_builder)` block. A `turbo:before-stream-render` listener that wraps `event.detail.render` is the older alternative.
- With Turbo, what happens when a stream's target id is not on the page?Nothing visible: the id lookup returns no element, so the action applies to an empty list and no error is raised. That is why a missing `id="<%= dom_id ticket %>"` on a partial's root fails silently. Only a stream with neither `target` nor `targets`, an unknown action, or a first child that is not a `<template>` throws, and Turbo logs those to the console.
- Turbo has a morph stream action for morphing a target in place.
- update replaces the target element itself, id and all, just like replace.
- targets takes a space-separated list of element ids.
- The <turbo-stream> element stays in the DOM after it runs and needs cleanup.
- Appending an element whose id is already in the list always creates a duplicate.
- A stream whose target is missing raises an error the server can rescue.
Assumes Turbo 8 (8.0.23), which defines eight stream actions including refresh, the action Turbo 8 added for page refreshes; morphing streams use method="morph" on replace or update, not a morph action.