Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 6

## main

* Add `compile.view_component` ActiveSupport::Notifications event for eager compilation at boot time.

*Joel Hawksley*, *GitHub Copilot*

## 4.7.0

* Fix stale content cache when slots are accessed before `render_in`.
Expand Down
14 changes: 14 additions & 0 deletions docs/guide/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ end

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

## Compile instrumentation

Since 4.8.0
{: .label }

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`:

```ruby
ActiveSupport::Notifications.subscribe("compile.view_component") do |event|
event.name # => "compile.view_component"
event.duration # => 123.45 (milliseconds)
end
Comment thread
joelhawksley marked this conversation as resolved.
```

## Viewing instrumentation sums in the browser developer tools

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`.
Expand Down
6 changes: 5 additions & 1 deletion lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ class Engine < Rails::Engine # :nodoc:

initializer "view_component.eager_load_actions" do
ActiveSupport.on_load(:after_initialize) do
ViewComponent::Base.descendants.each(&:__vc_compile) if Rails.application.config.eager_load
if Rails.application.config.eager_load
ActiveSupport::Notifications.instrument("compile.view_component") do
ViewComponent::Base.descendants.each(&:__vc_compile)
end
end
end
end

Expand Down
22 changes: 22 additions & 0 deletions test/sandbox/test/instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,26 @@ def test_instrumentation_disabled
assert_equal(events.size, 0)
end
end

def test_compile_instrumentation
events = []
subscriber = ActiveSupport::Notifications.subscribe("compile.view_component") do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end

old_eager_load = Rails.application.config.eager_load
Rails.application.config.eager_load = true

ViewComponent::CompileCache.invalidate!

# Execute the same block that the view_component.eager_load_actions initializer registers
initializer = ViewComponent::Engine.initializers.find { |i| i.name == "view_component.eager_load_actions" }
initializer.run(Rails.application)

assert_equal(1, events.size)
assert_equal("compile.view_component", events[0].name)
ensure
Rails.application.config.eager_load = old_eager_load
ActiveSupport::Notifications.unsubscribe(subscriber)
end
end
Loading