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

Only execute route reloads once on boot for development environment #40742

Conversation

dotLou
Copy link
Contributor

@dotLou dotLou commented Dec 3, 2020

Signed-off-by: Louis Cloutier louis.cloutier@shopify.com

Summary

In #39980 a new supported use case was added to allow overriding of the welcome route in an app via get '/'. Unfortunately, this introduced a double routes_reloader.execute in the development environment. For apps with lots of routes and/or lots of routes files, this creates a significant expense when booting in development environments.

This PR aims to allow for that use case while only executing the routes reloader a single time. To do this, I've introduced a new callback that can be set via Proc on the routes reloader, and will be called after load_paths.

You can see that the number of items is decreased in the LoadingTest, back to its original number, and the existing tests should continue to pass (and support overriding of the welcome route via both root and get '/' route definitions).

Other Information

I've gone ahead and updated the changelog already, but please let me know if I should remove that or alter the text in any way.

@rails-bot rails-bot bot added the railties label Dec 3, 2020
@dotLou dotLou force-pushed the development_only_execute_route_reloader_once branch from 252341b to 1680429 Compare December 3, 2020 16:57
Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Great stuff and nice finding. Can we add a test to make sure only 1 route reload happens in boot? This will make sure we don't regress again.

@dotLou dotLou force-pushed the development_only_execute_route_reloader_once branch from 1680429 to dcf5f5e Compare December 3, 2020 19:29
Comment on lines -47 to +54
paths.each { |path| load(path) }
run_callbacks :load_paths do
paths.each { |path| load(path) }
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the callback setup feels like we could just do this?

def load_paths
  paths.each { |path| load(path) }
  after_load_path_callback&.call
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca had suggested this but it might very well be faster not to use the activesupport callbacks? I have no strong feelings either way and am happy to make changes

Copy link
Member

Choose a reason for hiding this comment

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

I'll remove it after the merge

railties/lib/rails/application/routes_reloader.rb Outdated Show resolved Hide resolved
railties/lib/rails/application/finisher.rb Outdated Show resolved Hide resolved
railties/lib/rails/application/finisher.rb Outdated Show resolved Hide resolved
@@ -173,6 +173,22 @@ def self.complete(_state)
end
end

initializer :add_builtin_route do |app|
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
initializer :add_builtin_route do |app|
initializer :add_internal_routes do |app|

Can we rename this without anyone relying on the initializer name? Feels like we aren't adding just one more anymore and internal would be closer than builtin.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've gone ahead and made this change but will leave the comment unresolved if only so others can notice this change.

Signed-off-by: Louis Cloutier <louis.cloutier@shopify.com>
@dotLou dotLou force-pushed the development_only_execute_route_reloader_once branch from dcf5f5e to 796e712 Compare December 3, 2020 20:56
@rafaelfranca rafaelfranca merged commit bcb3383 into rails:master Dec 3, 2020
@kaspth
Copy link
Contributor

kaspth commented Dec 3, 2020

@dotLou thank you, this was great 🙌

@dotLou dotLou deleted the development_only_execute_route_reloader_once branch December 4, 2020 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants