DEV Community


Posted on

How to set-up Rails Hotwire live reload

  1. Install Gem
  2. Run install script
  3. Listen to file changes

This tutorial will explore a modern way to live-reload a rails app set-up with css-bundling (tailwind) and js-bundling (esbuild).

1. Install Gem

Add to Gemfile

group :development do
  gem "hotwire-livereload"
Enter fullscreen mode Exit fullscreen mode
bundle install
Enter fullscreen mode Exit fullscreen mode

2. Run install script

rails livereload:install
Enter fullscreen mode Exit fullscreen mode

3. Listen to file changes

View, helpers, and assets files are listened for by default.

If you use rails's css-bundling and js-bundling gems, find the bundled JS and CSS file app/builds.

The app/builds folder includes:

  • app/builds/application.css
  • app/builds/application.js

An additional watch process is needed to re-build the JS and CSS file after changes (see docs). Tailwind, for example, uses a Just-in-time compiler to only add classes to the CSS that are being used in your application. This requires specifying an additional path to listen for any (Just-in-time) changes.

# config/environments/development.rb

config.hotwire_livereload.listen_paths << Rails.root.join("app/assets/builds")
Enter fullscreen mode Exit fullscreen mode

Top comments (9)

ccastillop profile image

Makes a lot of sense working with hotwire reload, specially when working with tailwind v3.0 wich uses Just_In_Time compilation :)

Thanks for sharing

wdiechmann profile image
Walther Diechmann

thank you so much for sharing! This really hit a "soar spot" :D

chidoyo profile image
Erick Adrian Perez

I love this new LiveReload and I have been using it on a Rails 7 app I've been using for practice on tutorials, etc.
I love it so much that I decided to try to install it to a small Rails app at work that we use to show design prototypes.
So I decided to install it to this existing app that was running on rails '5.2.4'.
I just updated it to rails '7.0.2'
Update was simple and it was successful.
I followed the steps on this artible to install hotwire-livereload.
I got my first error when I tried to make a small change to a html.erb template. The error is:

Uncaught ReferenceError: Turbo is not defined

Does this mean that this livereload requires gem "turbo-rails" as a dependency?

I've tried installing it to my app but I'm running into the problem of this app not allowing me to add this line to my application.js file:

import '@hotwired/turbo-rails'

This app is a couple of years old so I don't use import on my JS files.
I also tried to add it like this but the file could not be found.

//= require @hotwired/turbo-rails

I think it's obvious that it requires turbo-rails since it's finding that Turbo is not defined.
What should I do to make this LiveReload work on a rails app that I've updated to rails 7?

igorkasyanchuk profile image
Igor Kasyanchuk

I've an own alternative, with no dependency on hotwire

chmich profile image
Christian Sedlmair

Thanks Igor, i installed it, it works fine, is slimmer and configurable. i will continue with yours

ahmednadar profile image
Ahmed Nadar • Edited

Does it work with Rails 7 and Hotwire out of the box?
I installed but no live update!!

Thread Thread
chmich profile image
Christian Sedlmair

Sorry, Ahmed didn't see the comments for long time. Yes. How may i help you? ... A Quick and dirty Sample project would be here

roqkabel profile image

Thanks so much for sharing,

alfmachine profile image

Image description