Skip to content

Commit 9a438f5

Browse files
committed
Only register the compile cache executor hook without template caching
The executor hook that clears the compile cache before each request did nothing when Action View caches templates, as in production, but it still read `ActionView::Base.cache_template_loading` on every request. That reads `ActionView::Resolver`'s `@@caching` class variable, which non-main Ractors can't read on Ruby 4.0, so under Rails' experimental `Rails.application.ractorize!` every request failed once view_component was loaded. Decide once, when Action View loads, whether to register the hook. Rails' Action View railtie likewise registers its view reloader only when template caching is off. The hook still checks the flag on each run, so toggling it at runtime, with caching off at boot, works as before.
1 parent b5b98c4 commit 9a438f5

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Only register the executor hook that clears the compile cache when Action View doesn't cache templates. With template caching on, as in production, the hook did nothing but still read `ActionView::Base.cache_template_loading` on every request. That value lives in a class variable, which non-main Ractors can't read on Ruby 4.0, so under Rails' experimental `Rails.application.ractorize!` every request failed.
14+
15+
*Mikael Henriksson*
16+
1317
## 4.16.0
1418

1519
* Fix subclasses defined after their parent renders a collection using the parent's default collection parameter name instead of their own. Fix recompiling a component not refreshing its collection parameter, counter, and iteration detection. Compute that detection when a component compiles, and store the results frozen so they're shareable across Ractors.

‎lib/view_component/engine.rb‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ class Engine < Rails::Engine # :nodoc:
132132
end
133133
end
134134

135-
app.executor.to_run :before do
136-
CompileCache.invalidate! unless ActionView::Base.cache_template_loading
135+
# Only when templates can change: with template caching on, as in
136+
# production, the hook would do nothing on every request.
137+
ActiveSupport.on_load(:action_view) do
138+
unless ActionView::Base.cache_template_loading
139+
app.executor.to_run :before do
140+
CompileCache.invalidate! unless ActionView::Base.cache_template_loading
141+
end
142+
end
137143
end
138144
end
139145
end

‎test/sandbox/test/initializers/engine_test.rb‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,38 @@
22

33
require "test_helper"
44

5+
require "open3"
6+
57
class EngineTest < Minitest::Test
68
def test_set_no_duplicate_autoload_paths
79
test_path = "#{Rails.root}/my/components/previews" # set in sandbox/config/application.rb
810
assert_equal 1, ActiveSupport::Dependencies.autoload_paths.count(test_path)
911
end
12+
13+
# Non-main Ractors can't read ActionView::Resolver's class variable on Ruby 4.0,
14+
# so the executor must not read it on every request when templates are cached.
15+
def test_executor_does_not_read_cache_template_loading_when_templates_are_cached
16+
reads, errors, status = Open3.capture3({"RAILS_ENV" => "production"}, RbConfig.ruby, "-rbundler/setup", "-e", <<~RUBY)
17+
require "rails"
18+
require "action_controller/railtie"
19+
require "view_component"
20+
21+
class CachedTemplatesApp < Rails::Application
22+
config.eager_load = true
23+
config.enable_reloading = false
24+
config.secret_key_base = "test"
25+
config.logger = Logger.new(nil)
26+
end
27+
28+
CachedTemplatesApp.initialize!
29+
30+
reads = 0
31+
ActionView::Resolver.singleton_class.prepend(Module.new { define_method(:caching?) { reads += 1; super() } })
32+
CachedTemplatesApp.executor.wrap {}
33+
print reads
34+
RUBY
35+
36+
assert status.success?, errors
37+
assert_equal "0", reads
38+
end
1039
end

0 commit comments

Comments
 (0)