Skip to content

Commit d7947fc

Browse files
committed
Add compile.view_component ActiveSupport::Notifications event for eager compilation at boot time.
1 parent 5d84af8 commit d7947fc

4 files changed

Lines changed: 40 additions & 1 deletion

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+
* Add `compile.view_component` ActiveSupport::Notifications event for eager compilation at boot time.
14+
15+
*Joel Hawksley*, *GitHub Copilot*
16+
1317
## 4.7.0
1418

1519
* Fix stale content cache when slots are accessed before `render_in`.

‎docs/guide/instrumentation.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ end
2828

2929
_Note: Enabling instrumentation negatively impacts the performance of ViewComponent._
3030

31+
## Compile instrumentation
32+
33+
Since 4.8.0
34+
{: .label }
35+
36+
ViewComponent also instruments eager compilation at boot time via the `compile.view_component` event. This event is always emitted (no configuration needed) when `config.eager_load` is `true`:
37+
38+
```ruby
39+
ActiveSupport::Notifications.subscribe("compile.view_component") do |event|
40+
event.name # => "compile.view_component"
41+
event.duration # => 123.45 (milliseconds)
42+
end
43+
```
44+
3145
## Viewing instrumentation sums in the browser developer tools
3246

3347
When using `render.view_component` with `config.server_timing = true` (default in development) in Rails 7, the browser developer tools display the sum total timing information in Network > Timing under the key `render.view_component`.

‎lib/view_component/engine.rb‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ class Engine < Rails::Engine # :nodoc:
7979

8080
initializer "view_component.eager_load_actions" do
8181
ActiveSupport.on_load(:after_initialize) do
82-
ViewComponent::Base.descendants.each(&:__vc_compile) if Rails.application.config.eager_load
82+
if Rails.application.config.eager_load
83+
ActiveSupport::Notifications.instrument("compile.view_component") do
84+
ViewComponent::Base.descendants.each(&:__vc_compile)
85+
end
86+
end
8387
end
8488
end
8589

‎test/sandbox/test/instrumentation_test.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,21 @@ def test_instrumentation_disabled
3232
assert_equal(events.size, 0)
3333
end
3434
end
35+
36+
def test_compile_instrumentation
37+
events = []
38+
ActiveSupport::Notifications.subscribe("compile.view_component") do |*args|
39+
events << ActiveSupport::Notifications::Event.new(*args)
40+
end
41+
42+
ViewComponent::CompileCache.invalidate!
43+
ActiveSupport::Notifications.instrument("compile.view_component") do
44+
ViewComponent::Base.descendants.each(&:__vc_compile)
45+
end
46+
47+
assert_equal(1, events.size)
48+
assert_equal("compile.view_component", events[0].name)
49+
ensure
50+
ActiveSupport::Notifications.unsubscribe("compile.view_component")
51+
end
3552
end

0 commit comments

Comments
 (0)