Only execute route reloads once on boot for development environment

Signed-off-by: Louis Cloutier <louis.cloutier@shopify.com>
This commit is contained in:
Louis Cloutier 2020-12-03 11:50:18 -05:00
parent cafdd18721
commit 796e7129e6
No known key found for this signature in database
GPG Key ID: 39146C2818B26AFE
4 changed files with 59 additions and 20 deletions

View File

@ -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.

View File

@ -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|

View File

@ -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!

View File

@ -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