precompile_and_cache
⚡ Precompiling & Caching ViewComponents for Production Performance
To minimize runtime compilation costs, eager-load and cache your components at boot. Use Rails initializers and Rails.cache.fetch
to store rendered fragments, especially for layout-heavy or static content components.
# config/initializers/view_component_cache.rb
Rails.application.config.to_prepare do
Dir[Rails.root.join("app/components/**/*.rb")].each { |file| require_dependency file }
end
# Example usage in layout:
<% cache "sidebar_menu" do %>
<%= render(SidebarComponent.new) %>
<% end %>