Skip to content

Commit ac0a307

Browse files
joelhawksleyCopilot
andcommitted
Merge main and check template caching once
Resolve conflicts with #2730's template watcher. Move hook registration into CompileCache.watch_templates so the cached-templates case is tested in-process instead of in a subprocess, and drop the runtime caching check, which only existed to support a test that toggled caching after boot. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f2e356ff-16cd-4517-8e63-a8600747d3fa
2 parents 9a438f5 + 8d7627c commit ac0a307

5 files changed

Lines changed: 73 additions & 47 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ 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.
13+
* Only register the executor hook that watches component templates when Action View doesn't cache templates at boot. 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.
1414

1515
*Mikael Henriksson*
1616

17+
* Only recompile components when a template or a translation file changed, instead of before every request, when templates aren't cached. Pages render about twice as fast in development.
18+
19+
*Clément Joubert*
20+
1721
## 4.16.0
1822

1923
* 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/compile_cache.rb‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,29 @@ def invalidate_class!(klass)
2525
def invalidate!
2626
cache.clear
2727
end
28+
29+
# Invalidates the cache when a template or a translation file is added,
30+
# changed, or removed. Changes to Ruby files reload the application instead.
31+
def template_watcher(file_watcher)
32+
extensions = ActionView::Template::Handlers.extensions.map(&:to_s) + %w[yml yaml]
33+
directories = ActiveSupport::Dependencies.autoload_paths.index_with(extensions)
34+
35+
file_watcher.new([], directories) { invalidate! }
36+
end
37+
38+
# Like Rails' own template cache expiry, only watches templates when they
39+
# aren't cached at boot. With template caching on, as in production, no
40+
# hook runs per request, so requests never read ActionView's caching
41+
# class variable, which non-main Ractors can't access.
42+
def watch_templates(app)
43+
return if ActionView::Base.cache_template_loading
44+
45+
watcher = nil
46+
47+
app.executor.to_run :before do
48+
watcher ||= CompileCache.template_watcher(app.config.file_watcher)
49+
watcher.execute_if_updated
50+
end
51+
end
2852
end
2953
end

‎lib/view_component/engine.rb‎

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

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
143-
end
135+
ActiveSupport.on_load(:action_view) { CompileCache.watch_templates(app) }
136+
137+
app.reloader.before_class_unload { CompileCache.invalidate! }
144138
end
145139
end
146140
end

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,33 @@
22

33
require "test_helper"
44

5-
require "open3"
6-
75
class EngineTest < Minitest::Test
86
def test_set_no_duplicate_autoload_paths
97
test_path = "#{Rails.root}/my/components/previews" # set in sandbox/config/application.rb
108
assert_equal 1, ActiveSupport::Dependencies.autoload_paths.count(test_path)
119
end
1210

1311
# 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
12+
# so no per-request hook may read it when templates are cached.
13+
def test_watch_templates_registers_no_executor_hook_when_templates_are_cached
14+
app = app_with_new_executor
15+
16+
with_template_caching { ViewComponent::CompileCache.watch_templates(app) }
17+
18+
assert_empty app.executor._run_callbacks
19+
end
20+
21+
def test_watch_templates_registers_an_executor_hook_when_templates_are_not_cached
22+
app = app_with_new_executor
23+
24+
with_new_cache { ViewComponent::CompileCache.watch_templates(app) }
25+
26+
assert_equal 1, app.executor._run_callbacks.count
27+
end
28+
29+
private
30+
31+
def app_with_new_executor
32+
Struct.new(:executor, :config).new(Class.new(ActiveSupport::Executor), Rails.application.config)
3833
end
3934
end

‎test/sandbox/test/integration_test.rb‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ def test_rendering_component_in_a_controller_with_block
5050
assert_response :success
5151
end
5252

53-
def test_template_changes_are_not_reflected_on_new_request_when_cache_template_loading_is_true
54-
with_template_caching do
53+
def test_template_changes_are_reflected_on_new_request_when_cache_template_loading_is_false
54+
with_new_cache do
5555
get "/controller_inline"
5656
assert_select("div", "bar")
5757
assert_response :success
5858

5959
modify_file "app/components/controller_inline_component.html.erb", "<div>Goodbye world!</div>" do
6060
get "/controller_inline"
61-
assert_select("div", "bar")
61+
assert_select("div", "Goodbye world!")
6262
assert_response :success
6363
end
6464

@@ -68,22 +68,31 @@ def test_template_changes_are_not_reflected_on_new_request_when_cache_template_l
6868
end
6969
end
7070

71-
def test_template_changes_are_reflected_on_new_request_when_cache_template_loading_is_false
71+
def test_components_are_not_recompiled_on_new_request_when_no_template_changed
7272
with_new_cache do
7373
get "/controller_inline"
74-
assert_select("div", "bar")
74+
MyComponent.__vc_compile
75+
76+
get "/controller_inline"
7577
assert_response :success
78+
assert MyComponent.__vc_compiled?
79+
end
80+
end
7681

77-
modify_file "app/components/controller_inline_component.html.erb", "<div>Goodbye world!</div>" do
78-
get "/controller_inline"
79-
assert_select("div", "Goodbye world!")
80-
assert_response :success
81-
end
82+
def test_components_are_recompiled_on_new_request_when_a_template_is_added
83+
new_template = Rails.root.join("app/components/added_template_component.html.erb")
8284

85+
with_new_cache do
86+
get "/controller_inline"
87+
MyComponent.__vc_compile
88+
89+
File.write(new_template, "<div>Added</div>")
8390
get "/controller_inline"
84-
assert_select("div", "bar")
8591
assert_response :success
92+
refute MyComponent.__vc_compiled?
8693
end
94+
ensure
95+
FileUtils.rm_f(new_template)
8796
end
8897

8998
def test_inherited_template_changes_are_reflected_on_new_request_when_cache_template_loading_is_false

0 commit comments

Comments
 (0)