New Discussion

Notifications

You’re not receiving notifications from this thread.

How to add Lazy Load Hooks to Rails Plugins Discussion

1
General

Have you used lazy load hooks to extend a gem with some interesting features?

Thanks for sharing this — it’s a really useful topic, especially as Rails apps get more complex and performance becomes more critical.

Adding lazy load hooks to Rails plugins is definitely a smart move if you're trying to reduce boot time and avoid unnecessary memory usage. If you're using ActiveSupport.on_load, it's a great way to defer loading code until the specific framework component is initialized. Something like:

ActiveSupport.on_load(:action_controller) do
  include MyPlugin::ControllerMethods
end

This way, your plugin doesn't pull in Rails components prematurely, and it integrates cleanly when the relevant part of the framework is ready.

Out of curiosity — are you using Zeitwerk for autoloading in your plugin? It plays really nicely with this approach if your plugin is structured properly. I’d love to hear how you're organizing things or if you're hitting any snags with it.

Again, awesome share. This kind of insight helps keep the community sharp.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 90,206+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.