Lesson 8 of 9 · 24m
Hotwire Interfaces Without SPA Sprawl
Use Turbo Frames, Turbo Streams, Stimulus, and server-rendered state to build fast interfaces while keeping authority on the server.
Hotwire succeeds when you let the server remain the source of truth. It fails when you reconstruct a fragile client-side state machine inside a dozen disconnected frames. The promise is not "no JavaScript." The promise is less duplicated state, fewer API endpoints that exist only to feed widgets, and interfaces that still respect the browser.
Frames are navigation boundaries
A Turbo Frame should correspond to a meaningful UI region:
<%= turbo_frame_tag dom_id(invoice, :line_items) do %>
<%= render invoice.line_items %>
<% end %>The frame owns its navigation and replacement. Do not make tiny frames for every button because you can; each boundary is cognitive overhead. Design around the interaction, not the DOM node count.
Streams are for targeted change after server truth
Turbo Streams shine when one action changes several regions:
<%= turbo_stream.replace dom_id(invoice, :total), partial: "invoices/total", locals: { invoice: } %>
<%= turbo_stream.prepend "activity", partial: "activity/event", locals: { event: } %>
<%= turbo_stream.replace "flash", partial: "layouts/flash" %>The server calculates authorization, totals, and validation once. The stream merely distributes the rendered consequences. If the client has to recompute totals, you have drifted back toward SPA complexity without the tooling.
Stimulus is behavior glue, not application state
Use Stimulus for focused behavior: copy buttons, menus, debounced search, forms that reveal fields, keyboard shortcuts. Keep server-authoritative state in rendered HTML.
// app/javascript/controllers/debounce_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["form"]
search() {
clearTimeout(this.timeout); this.timeout = setTimeout(() => this.formTarget.requestSubmit(), 250)
}
}If a controller starts accumulating a mirror of server objects, ask why. Sometimes a real client component is justified. Often the workflow simply needs a better server-rendered representation.
Broadcasts need authorization discipline
Turbo::Streams::Broadcasts can update many clients in near real time. Never broadcast private object renders into a channel wider than the audience. Stream names are authorization boundaries:
Turbo::StreamsChannel.broadcast_replace_to(invoice.account,
target: dom_id(invoice, :total), partial: "invoices/total",
locals: { invoice: })broadcast_replace_to invoice may be wrong if the model name channel crosses tenant boundaries in your design. Name the audience explicitly.
Field notes
- A frame response that renders the entire layout usually means the frame target or navigation behavior is wrong.
- Turbo Drive caching can make stale flashes and flashes-of-old-content appear; use metadata and cache-control intentionally.
- Hotwire does not remove the need for API design when mobile apps or partners consume the same workflow.
Was this lesson helpful?