May 3rd 2024

Embracing Hotwire: A Modern Front-end Approach

We discuss the benefits and drawbacks of Hotwire in modern Ruby on Rails.

Evermind Digital

Rails has traditionally been known for its back-end prowess, with developers often relying on front-end frameworks like React or Vue.js to build rich and interactive user interfaces. However, the landscape is shifting, and Rails has stepped up its front-end game with the introduction of Hotwire, a modern approach that promises to streamline web development while leveraging the existing strengths of Rails.

What is Hotwire?

Hotwire is a collection of three complementary techniques: Turbo Drives, Turbo Frames, and Turbo Streams, designed to provide a seamless and efficient front-end experience for Rails applications. It leverages HTML over the wire, taking advantage of modern browser capabilities to deliver snappy and responsive user interfaces without the need for complex front-end frameworks or extensive JavaScript code. You can read more about Hotwire on the official website: https://hotwired.dev/

Turbo Drives

Turbo Drives is a technique that replaces the traditional page-to-page navigation with a smooth, in-place transition. When a user clicks a link or submits a form, instead of loading an entirely new page, Turbo Drives intelligently updates only the relevant portions of the page, resulting in a faster and more efficient user experience.

Turbo Frames

Turbo Frames allow you to define specific areas of your page as isolated components that can be independently updated without affecting the rest of the page. This modular approach promotes code reusability and simplifies the development process, as developers can focus on individual components rather than managing the entire page.

Turbo Streams

Turbo Streams provide a mechanism for broadcasting partial updates from the server to the client in real-time. This technique enables you to create reactive and dynamic user interfaces that update automatically in response to server-side events, such as new data being available or changes made by other users.

Benefits of Hotwire

  • Simplified Development: Hotwire allows you to build modern, reactive web applications using familiar Rails conventions and without the need to learn complex front-end frameworks or manage a separate build process.
  • Improved Performance: By leveraging HTML over the wire and partial page updates, Hotwire reduces the amount of data transferred between the server and the client, resulting in faster load times and a more responsive user experience.
  • Streamlined Workflow: With Hotwire, you can develop both the back-end and front-end logic within the Rails ecosystem, eliminating the need for context switching between different tools and technologies.
  • Reduced Complexity: Hotwire relies on standard HTML, CSS, and minimal JavaScript, reducing the overall complexity of your codebase and making it more maintainable in the long run.
  • Incremental Adoption: Hotwire can be gradually introduced into existing Rails applications, allowing developers to migrate to a modern front-end approach at their own pace.

Drawbacks of Hotwire

  • Limited Ecosystem: While Hotwire is gaining momentum, it is still relatively new, and the ecosystem of libraries, tools, and resources may not be as mature as those for established front-end frameworks.
  • Potential Performance Bottlenecks: In applications with heavily interactive and complex user interfaces, the reliance on server-side rendering could potentially introduce performance bottlenecks if not implemented carefully.
  • Learning Curve: While Hotwire aims to simplify front-end development, there is still a learning curve involved in understanding its concepts and best practices.
  • Limited Customization: Hotwire is designed to work within the Rails ecosystem, which may limit the flexibility and customization options available compared to dedicated front-end frameworks.
  • Compatibility Concerns: As with any new technology, there may be compatibility issues with existing libraries, plugins, or third-party tools that were not designed with Hotwire in mind.

Code Samples

Here are some code samples to illustrate how Hotwire can be implemented in a Rails application:

Turbo Drives Example

# config/routes.rb Rails.application.routes.draw do resources :posts end
<!-- app/views/posts/index.html.erb --> <h1>Posts</h1> <ul> <% @posts.each do |post| %> <%= render 'post', post: post %> <% end %> </ul> <!-- app/views/posts/_post.html.erb --> <li> <%= link_to post.title, post, data: { turbo: true } %> </li>

In this example, Turbo Drives is enabled by adding data: { turbo: true } to the link. When a user clicks on a post title, Turbo Drives will seamlessly update the page with the show view without a full page reload.

Turbo Frames Example

<!-- app/views/posts/index.html.erb --> <h1>Posts</h1> <%= turbo_frame_tag :posts_list do %> <ul> <% @posts.each do |post| %> <%= render 'post', post: post %> <% end %> </ul> <% end %> <!-- app/views/posts/_post.html.erb --> <li> <%= link_to post.title, post, data: { turbo_frame: :post_details } %> </li> <%= turbo_frame_tag :post_details %>
<!-- app/views/posts/show.html.erb --> <%= turbo_stream.update :post_details do %> <h2><%= @post.title %></h2> <p><%= @post.body %></p> <% end %>

In this example, we define a turbo_frame_tag for the posts list and another for the post details. When a user clicks on a post title, Turbo Frames updates only the :post_details frame with the show view, without reloading the entire page.

Turbo Streams Example

# app/controllers/comments_controller.rb class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(comment_params) respond_to do |format| format.turbo_stream end end private def comment_params params.require(:comment).permit(:body) end end
<!-- app/views/comments/create.turbo_stream.erb --> <%= turbo_stream.append :comments do %> <%= render @comment %> <% end %> <!-- app/views/posts/show.html.erb --> <h2><%= @post.title %></h2> <p><%= @post.body %></p> <div id="comments"> <h3>Comments</h3> <%= turbo_stream_from @post, :comments %> <%= render @post.comments %> </div> <%= form_with model: [ @post, @post.comments.build ], data: { turbo_frame: :new_comment } do |form| %> <%= form.text_area :body %> <%= form.submit %> <% end %>

In this example, when a new comment is created, Turbo Streams appends the rendered comment to the :comments div on the show view without reloading the entire page. The turbo_stream_from helper sets up a stream connection to receive real-time updates. These are just a few examples of how Hotwire can be used in a Rails application. The true power of Hotwire lies in its ability to combine these techniques to build dynamic and responsive user interfaces while leveraging the conventions and familiarity of the Rails ecosystem.