Clarify documentation for conditional Maglev loading using `require: false`
Currently, the documentation suggests installing the gem as follows:
gem 'maglevcms'
However, this causes Maglev to be loaded in all environments and contexts, including those where it’s not needed -such as Sidekiq, the Rails console, or background tasks. This can add unnecessary load time and memory usage.
Proposed Improvement: Update the documentation to include an example of using require: false in the Gemfile, along with guidance on how to conditionally load Maglev only when required (e.g., during web requests or when editing content):
gem 'maglevcms', require: false
This improves performance and prevents Maglev from loading in non-web contexts, while keeping setup simple and explicit for developers.
Note: I'm investigating how exactly to do this and will attach any solutions I find.
Solution
gemfile
gem 'maglevcms', require: false
application.rb - conditionally require maglevcms, in this case only if it's not sidekiq.
require 'maglevcms' unless (defined?(Sidekiq) && Sidekiq.server?)
anywhere else that requires use of the Maglev gem needs to check if it is defined
# routes
if defined?(Maglev) && Maglev.const_defined?(:Engine)
mount Maglev::Engine => '/maglev'
....
end
# initializer/maglev.rb
if defined?(Maglev) && Maglev.const_defined?(:Engine)
Maglev.configure do |config|
....
end
👋 @ishields! It really depends on the context of the main app. For instance, I've got one with async jobs which deal with Maglev pages. But yes, it's a good trick, I'll find it a place in the documentation. Thanks!