patrickarnett/view_component-fragment_caching

Changes to a view component *view file* do not trigger a re-render in rails 7.1

Opened this issue · 0 comments

n-b commented

This is linked, as far as I can tell, to this change in rails in ActionView::PathRegistry#cast_file_system_resolvers. As far as I can tell, this only matters in development environment, because the file watcher that triggers re-renders does not really matter in production.

Bug description:

In development, changes to a view component view file (e.g. app/components/some_component.html.erb) do not trigger a re-render. changes to the compoent source code (app/components/some_component.rb) work correctly.

I noticed it with view components that were used inside other templates (e.g. view/some_model/some_mode.html.erb) that were themselves cached, but I think this is not related.

Analysis:

At load time, ViewComponent::FragmentCaching add its resolver to ActionView. This is done in prepend_view_component_paths:

          resolver = Resolvers::ViewComponentResolver.new dir
          context.prepend_view_path resolver

This is needed to add the resolver to ActionView::PathRegistry. Before rails 7.1, adding this resolver triggered the rebuild_watcher callback; after rails 7.1, as ActionView::PathRegistry only calls rebuild_watcher if a new FileSystemResolver had to be built for the added path.

rebuild_watcher is what sets up the filesystem watchers, in ActionView::CacheExpiry::ViewReloader.

Workaround:

Adding the app/view_components path as a regular watch path seems to fix the issue:

# config/initializers/view_component_fragment_caching.rb

ActiveSupport.on_load :action_controller do
  next unless self == ActionController::Base
  
  ActionController::Base.prepend_view_path ::ViewComponent::Base.view_component_path
end