Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double registration of :turbo_stream Renderer #562

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

skipkayhil
Copy link
Contributor

When an application loads both ActionController::Base and
ActionController::API, the :action_controller load hook gets called
twice (once for each of them). Since the :turbo_stream Renderer is
registered in this load hook, it ends up getting registered twice and
prints a warning for method redefinition.

To demonstrate the warning in the turbo-rails tests, an
Api::ApplicationController was added to the dummy app so that both
base controller classes get loaded:

$ CI=true bundle exec rake test
/home/hartley/.cache/asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/actionpack-7.1.3/lib/action_controller/metal/renderers.rb:75: warning: method redefined; discarding old _render_with_renderer_turbo_stream
/home/hartley/src/github.com/skipkayhil/turbo-rails/lib/turbo/engine.rb:67: warning: previous definition of _render_with_renderer_turbo_stream was here

This commit fixes the issue by not using the :action_controller load
hook to register the :turbo_stream Renderer. Adding a Renderer should
not require a lazy load hook because ActionController::Renderers does
not load any other classes.

This is more reflective of real world applications and allows us to more
easily simulate how the presence of certains Gems/Frameworks affect
behavior.
When an application loads both `ActionController::Base` and
`ActionController::API`, the `:action_controller` load hook gets called
twice (once for each of them). Since the `:turbo_stream` Renderer is
registered in this load hook, it ends up getting registered twice and
prints a warning for method redefinition.

To demonstrate the warning in the `turbo-rails` tests, an
`Api::ApplicationController` was added to the dummy app so that both
base controller classes get loaded:

```
$ CI=true bundle exec rake test
/home/hartley/.cache/asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/actionpack-7.1.3/lib/action_controller/metal/renderers.rb:75: warning: method redefined; discarding old _render_with_renderer_turbo_stream
/home/hartley/src/github.com/skipkayhil/turbo-rails/lib/turbo/engine.rb:67: warning: previous definition of _render_with_renderer_turbo_stream was here
```

This commit fixes the issue by not using the `:action_controller` load
hook to register the `:turbo_stream` Renderer. Adding a Renderer should
not require a lazy load hook because `ActionController::Renderers` does
not load any other classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the removal of the Mailboxes related to engine changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was to enable eager_load since neither the Action Mailbox nor the Action Mailer railties are required in config/application.rb. I think eager_load has merits on its own, but it also makes it easier to demonstrate the issue in this PR because we don't have to use the Api::ApplicationController in a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants