railsjazz / rails_live_reload

Live Reload for your Rails app. The easiest way to increase your productivity.
https://www.railsjazz.com/
MIT License
317 stars 8 forks source link

Doesn't work with Phlex #33

Open joshuap opened 6 months ago

joshuap commented 6 months ago

I was playing with phlex-rails today and wanted to see if I could get it working with a live reload plugin and landed on this one.

I edited rails_live_reload's default watched file patterns to watch Phlex views:

RailsLiveReload.configure do |config|
  # Add .rb for Phlex views
  config.watch %r{app/views/.+\.(erb|rb)$}, reload: :on_change
  config.watch %r{(app|vendor)/(assets|javascript)/\w+/(.+\.(css|js|html|png|jpg|ts|jsx)).*}, reload: :always
end

That made it watch the Phlex view paths, but it wasn't live reloading. The problem is that rails_live_reload uses the payload[:identifier] from the ActiveSupport::Notifications::Event to track which views were rendered for the current page, and compares that to the watched file paths to trigger a reload for just the pages that need it. For other template systems, payload[:identifier] is the template file path, but in Phlex it's the name of the Ruby class:

#<ActiveSupport::Notifications::Event:0x0000000121293020
 @allocation_count_finish=0,
 @allocation_count_start=0,
 @cpu_time_finish=0.0,
 @cpu_time_start=0.0,
 @end=1708821265437.4321,
 @name="render_template.action_view",
 @payload=
  {:identifier=>"Posts::IndexView",
   :layout=>"application_layout",
   :locals=>{}},
 @time=1708821265414.1838,
 @transaction_id="2b982eb42c32ed910b90">

It's trying to compare the file that changed — "app/views/posts/index_view.rb" — with the file that was rendered, which for Phlex is "Posts::IndexView".

I know Phlex is a relatively new project, so I understand if you don't want to fix this, but I wasn't sure what else to do with this knowledge, so dropping it here. :)

cc @joeldrapper @igorkasyanchuk

wdiechmann commented 4 months ago

I'll second that observation - but I'm confident that when @joeldrapper get just a few minutes on his hands the magic will be eminent 😍