Skip to content

Commit 51f6e48

Browse files
authored
Merge branch 'main' into class-translate-missing-keys
2 parents 2297b26 + 8d7627c commit 51f6e48

4 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎docs/CHANGELOG.md‎

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

1515
*Andrii Furmanets*
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,14 @@ 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
2837
end
2938
end

‎lib/view_component/engine.rb‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,16 @@ class Engine < Rails::Engine # :nodoc:
132132
end
133133
end
134134

135+
template_watcher = nil
136+
135137
app.executor.to_run :before do
136-
CompileCache.invalidate! unless ActionView::Base.cache_template_loading
138+
next if ActionView::Base.cache_template_loading
139+
140+
template_watcher ||= CompileCache.template_watcher(app.config.file_watcher)
141+
template_watcher.execute_if_updated
137142
end
143+
144+
app.reloader.before_class_unload { CompileCache.invalidate! }
138145
end
139146
end
140147
end

‎test/sandbox/test/integration_test.rb‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ def test_template_changes_are_reflected_on_new_request_when_cache_template_loadi
8686
end
8787
end
8888

89+
def test_components_are_not_recompiled_on_new_request_when_no_template_changed
90+
with_new_cache do
91+
get "/controller_inline"
92+
MyComponent.__vc_compile
93+
94+
get "/controller_inline"
95+
assert_response :success
96+
assert MyComponent.__vc_compiled?
97+
end
98+
end
99+
100+
def test_components_are_recompiled_on_new_request_when_a_template_is_added
101+
new_template = Rails.root.join("app/components/added_template_component.html.erb")
102+
103+
with_new_cache do
104+
get "/controller_inline"
105+
MyComponent.__vc_compile
106+
107+
File.write(new_template, "<div>Added</div>")
108+
get "/controller_inline"
109+
assert_response :success
110+
refute MyComponent.__vc_compiled?
111+
end
112+
ensure
113+
FileUtils.rm_f(new_template)
114+
end
115+
89116
def test_inherited_template_changes_are_reflected_on_new_request_when_cache_template_loading_is_false
90117
with_new_cache do
91118
get "/inherited_sidecar"

0 commit comments

Comments
 (0)