What Is Hotwire? How Turbo Updates Pages by Sending HTML
When to use Drive, Frames or Streams, and when React is the better call
The whole choice comes down to how much of the screen changes. Page navigation is Turbo Drive, one region is Turbo Frames, several places at once is Turbo Streams. Behaviour that stays inside the browser belongs to Stimulus.
Turbo Drive: on by default
It intercepts links and forms, fetches the response and swaps the whole <body>. It's still a redraw, but JS and CSS aren't reloaded, so navigation stays light.
Because <body> is new each time, code hung on DOMContentLoaded only runs on the first screen. If an old jQuery plugin dies from the second page on, that's usually why. Use turbo:load or move it to Stimulus.
Turbo Frames: the id has to match
A link inside turbo_frame_tag "post_123" takes only the frame with the same id from the response and puts it in place. If the response has no such frame, the page shows Content missing and the console logs did not contain the expected <turbo-frame id="...">. An error page or a login redirect will do this. For links meant to leave the frame, add data-turbo-frame="_top".
Turbo Streams: eight actions
The old version of this post listed morph among the eight. That was wrong. In the turbo-rails 2.0.20 source they are append, prepend, replace, update, remove, before, after and refresh. Morph isn't an action; it's a method: :morph option on replace and update.
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.append("comments", @comment),
turbo_stream.update("comments_count", @post.comments.count)
]
end
format.html { redirect_to @post }
end
By default the Stream format is only added to the Accept header on unsafe requests like POST (requestAcceptsTurboStreamResponse). To get one from a GET form or link, add data-turbo-stream. Push it over Action Cable and other users' screens change too.
The error everyone hits once
render :new on a failed validation gives Form responses must redirect to another location. Turbo treats a 200 from an unsafe form as an error. Pass the status: render :new, status: :unprocessable_entity.
Hotwire or a SPA like React?
Admin screens, lists, forms and comments, anything that shows and edits server data, are fine on Hotwire. The code stays inside Rails and you don't need a separate API.
A drag-to-edit canvas, offline support, or complex state that updates on every keystroke without asking the server is better in React. Same if a mobile app needs the JSON API anyway. You can mix them, too: keep just the one hard screen in React.
If you're weighing how far to take an existing Rails app with Hotwire, you can ask negabaro.
Architecture Diagram
Key Points
Leave navigation to Drive and move `DOMContentLoaded` code to `turbo:load` or Stimulus
To change one region, wrap it in `turbo_frame_tag` and return the same id
To change several places, respond with `format.turbo_stream`
Give failed form responses `status: :unprocessable_entity`