From 796e7129e64d36cbca1c8b11dc48ba5587913217 Mon Sep 17 00:00:00 2001 From: Louis Cloutier Date: Thu, 3 Dec 2020 11:50:18 -0500 Subject: [PATCH] Only execute route reloads once on boot for development environment Signed-off-by: Louis Cloutier --- railties/CHANGELOG.md | 4 +++ railties/lib/rails/application/finisher.rb | 32 +++++++++---------- .../lib/rails/application/routes_reloader.rb | 15 +++++++-- railties/test/application/loading_test.rb | 28 ++++++++++++++-- 4 files changed, 59 insertions(+), 20 deletions(-) diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 06d69fd023..5cfc67c653 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Only execute route reloads once on boot for development environment + + *Louis Cloutier* + * Removed manifest.js and application.css in app/assets folder when --skip-sprockets option passed as flag to rails. diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index f996db21a3..ae75ac6c0d 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -173,6 +173,22 @@ module Rails end end + initializer :add_internal_routes do |app| + if Rails.env.development? + app.routes.prepend do + get "/rails/info/properties" => "rails/info#properties", internal: true + get "/rails/info/routes" => "rails/info#routes", internal: true + get "/rails/info" => "rails/info#index", internal: true + end + + routes_reloader.after_load_paths = -> do + app.routes.append do + get "/" => "rails/welcome#index", internal: true + end + end + end + end + # Set routes reload after the finisher hook to ensure routes added in # the hook are taken into account. initializer :set_routes_reloader_hook do |app| @@ -195,22 +211,6 @@ module Rails end end - initializer :add_builtin_route do |app| - if Rails.env.development? - app.routes.prepend do - get "/rails/info/properties" => "rails/info#properties", internal: true - get "/rails/info/routes" => "rails/info#routes", internal: true - get "/rails/info" => "rails/info#index", internal: true - end - - app.routes.append do - get "/" => "rails/welcome#index", internal: true - end - - routes_reloader.execute - end - end - # Set clearing dependencies after the finisher hook to ensure paths # added in the hook are taken into account. initializer :set_clear_dependencies_hook, group: :all do |app| diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index 3ba31f16c2..8db429f785 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -5,10 +5,15 @@ require "active_support/core_ext/module/delegation" module Rails class Application class RoutesReloader + include ActiveSupport::Callbacks + attr_reader :route_sets, :paths, :external_routes - attr_accessor :eager_load + attr_accessor :eager_load, :after_load_paths delegate :execute_if_updated, :execute, :updated?, to: :updater + define_callbacks :load_paths + set_callback :load_paths, :after, :run_after_load_paths + def initialize @paths = [] @route_sets = [] @@ -44,7 +49,13 @@ module Rails end def load_paths - paths.each { |path| load(path) } + run_callbacks :load_paths do + paths.each { |path| load(path) } + end + end + + def run_after_load_paths + after_load_paths.call if after_load_paths.respond_to?(:call) end def finalize! diff --git a/railties/test/application/loading_test.rb b/railties/test/application/loading_test.rb index 2d8961eb7f..bff17eb0db 100644 --- a/railties/test/application/loading_test.rb +++ b/railties/test/application/loading_test.rb @@ -283,12 +283,36 @@ class LoadingTest < ActiveSupport::TestCase require "#{rails_root}/config/environment" get "/c" - assert_equal "5", last_response.body + assert_equal "3", last_response.body app_file "db/schema.rb", "" get "/c" - assert_equal "11", last_response.body + assert_equal "7", last_response.body + end + + test "routes are only loaded once on boot" do + add_to_config <<-RUBY + config.cache_classes = false + RUBY + + app_file "config/routes.rb", <<-RUBY + $counter ||= 0 + $counter += 1 + Rails.application.routes.draw do + get '/c', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [$counter.to_s]] } + end + RUBY + + boot_app "development" + + require "rack/test" + extend Rack::Test::Methods + + require "#{rails_root}/config/environment" + + get "/c" + assert_equal "1", last_response.body end test "columns migrations also trigger reloading" do