Skip to content

Commit

Permalink
Defer route drawing to the first request, or when url_helpers are called
Browse files Browse the repository at this point in the history
Executes the first routes reload in middleware, or when a route set's
url_helpers receives a route call / asked if it responds to a route.
Previously, this was executed unconditionally on boot, which can
slow down boot time unnecessarily for larger apps with lots of routes.
  • Loading branch information
gmcgibbon committed Apr 20, 2024
1 parent 339035c commit b0b0a0e
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 4 deletions.
9 changes: 9 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,12 @@
* Defer route drawing to the first request, or when url_helpers are called

Executes the first routes reload in middleware, or when a route set's
url_helpers receives a route call / asked if it responds to a route.
Previously, this was executed unconditionally on boot, which can
slow down boot time unnecessarily for larger apps with lots of routes.

*Gannon McGibbon*

* Fix the error page that is displayed when a view template is missing to account for nested controller paths in the
suggested correct location for the missing template.

Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch.rb
Expand Up @@ -71,6 +71,7 @@ class MissingController < NameError
autoload :DebugView
autoload :ExceptionWrapper
autoload :Executor
autoload :Once
autoload :Flash
autoload :PublicExceptions
autoload :Reloader
Expand Down
19 changes: 19 additions & 0 deletions actionpack/lib/action_dispatch/middleware/once.rb
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# :markup: markdown

module ActionDispatch
class Once
def initialize(app, block)
@app, @block, @called = app, block, false
end

def call(env)
@called ||= begin
@block.call
true
end
@app.call(env)
end
end
end
3 changes: 3 additions & 0 deletions actionpack/test/abstract_unit.rb
Expand Up @@ -34,6 +34,9 @@ def env
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "test")
end

def application
end

def root; end
end
end
Expand Down
26 changes: 26 additions & 0 deletions actionpack/test/dispatch/once_test.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "abstract_unit"

module ActionDispatch
class OncesTest < ActionDispatch::IntegrationTest
setup do
@call_count = 0
@call_increment = proc { @call_count += 1 }
build_app
end

def test_runs_block_only_once
5.times { get "/test" }

assert_equal(1, @call_count)
end

private
def build_app
@app = self.class.build_app do |middleware|
middleware.use Once, @call_increment
end
end
end
end
9 changes: 9 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,12 @@
* Defer route drawing to the first request, or when url_helpers are called

Executes the first routes reload in middleware, or when a route set's
url_helpers receives a route call / asked if it responds to a route.
Previously, this was executed unconditionally on boot, which can
slow down boot time unnecessarily for larger apps with lots of routes.

*Gannon McGibbon*

* Allow Actionable Errors encountered when running tests to be retried.

```txt
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/application/default_middleware_stack.rb
Expand Up @@ -63,6 +63,7 @@ def build_stack
end

if config.reloading_enabled?
middleware.use ::ActionDispatch::Once, proc { app.routes_reloader.execute_unless_loaded } unless app.config.eager_load
middleware.use ::ActionDispatch::Reloader, app.reloader
end

Expand Down
8 changes: 6 additions & 2 deletions railties/lib/rails/application/finisher.rb
Expand Up @@ -159,7 +159,6 @@ def self.complete(_state)
initializer :set_routes_reloader_hook do |app|
reloader = routes_reloader
reloader.eager_load = app.config.eager_load
reloader.execute
reloaders << reloader

app.reloader.to_run do
Expand All @@ -177,7 +176,12 @@ def self.complete(_state)
ActiveSupport.run_load_hooks(:after_routes_loaded, self)
end

ActiveSupport.run_load_hooks(:after_routes_loaded, self)
if reloader.eager_load
reloader.execute
ActiveSupport.run_load_hooks(:after_routes_loaded, self)
elsif reloader.loaded
ActiveSupport.run_load_hooks(:after_routes_loaded, self)
end
end

# Set clearing dependencies after the finisher hook to ensure paths
Expand Down
11 changes: 10 additions & 1 deletion railties/lib/rails/application/routes_reloader.rb
Expand Up @@ -7,7 +7,7 @@ class Application
class RoutesReloader
include ActiveSupport::Callbacks

attr_reader :route_sets, :paths, :external_routes
attr_reader :route_sets, :paths, :external_routes, :loaded
attr_accessor :eager_load
attr_writer :run_after_load_paths # :nodoc:
delegate :execute_if_updated, :execute, :updated?, to: :updater
Expand All @@ -17,9 +17,11 @@ def initialize
@route_sets = []
@external_routes = []
@eager_load = false
@loaded = false
end

def reload!
@loaded = true
clear!
load_paths
finalize!
Expand All @@ -28,6 +30,13 @@ def reload!
revert
end

def execute_unless_loaded
unless @loaded
execute
true
end
end

private
def updater
@updater ||= begin
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/commands/routes/routes_command.rb
Expand Up @@ -23,6 +23,7 @@ def invoke_command(*)
desc "routes", "List all the defined routes"
def perform(*)
boot_application!
Rails.application.routes_reloader.execute_unless_loaded
require "action_dispatch/routing/inspector"

say inspector.format(formatter, routes_filter)
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/engine.rb
Expand Up @@ -349,6 +349,7 @@ module Rails
# config.railties_order = [Blog::Engine, :main_app, :all]
class Engine < Railtie
autoload :Configuration, "rails/engine/configuration"
autoload :RouteSet, "rails/engine/route_set"

class << self
attr_accessor :called_from, :isolated
Expand Down Expand Up @@ -544,7 +545,7 @@ def env_config
# Defines the routes for this engine. If a block is given to
# routes, it is appended to the engine.
def routes(&block)
@routes ||= ActionDispatch::Routing::RouteSet.new_with_config(config)
@routes ||= RouteSet.new_with_config(config)
@routes.append(&block) if block_given?
@routes
end
Expand Down
38 changes: 38 additions & 0 deletions railties/lib/rails/engine/route_set.rb
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# :markup: markdown

require "action_dispatch/routing/route_set"

module Rails
class Engine
class RouteSet < ActionDispatch::Routing::RouteSet # :nodoc:
def generate_url_helpers(supports_path)
application = Rails.application

method_missing = Module.new do
define_method(:method_missing) do |method_name, *args, &block|
if application && application.initialized? && application.routes_reloader.execute_unless_loaded
ActiveSupport.run_load_hooks(:after_routes_loaded, application)
public_send(method_name, *args, &block)
else
super(method_name, *args, &block)
end
end

define_method(:respond_to_missing?) do |method_name, include_private = false|
if application && application.initialized? && application.routes_reloader.execute_unless_loaded
ActiveSupport.run_load_hooks(:after_routes_loaded, application)
respond_to?(method_name, include_private)
else
super(method_name, include_private)
end
end
end
super.tap do |mod|
mod.singleton_class.prepend(method_missing)
end
end
end
end
end
102 changes: 102 additions & 0 deletions railties/test/engine/route_set_test.rb
@@ -0,0 +1,102 @@
# frozen_string_literal: true

require "isolation/abstract_unit"

module Rails
class Engine
class RouteSetTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation

setup :build_app

teardown :teardown_app

test "app lazily loads routes when invoking url helpers" do
require "#{app_path}/config/environment"

assert_not_operator(:root_path, :in?, app_url_helpers.methods)
assert_equal("/", app_url_helpers.root_path)
end

test "engine lazily loads routes when invoking url helpers" do
require "#{app_path}/config/environment"

assert_not_operator(:root_path, :in?, engine_url_helpers.methods)
assert_equal("/plugin/", engine_url_helpers.root_path)
end

test "app lazily loads routes when checking respond_to?" do
require "#{app_path}/config/environment"

assert_not_operator(:root_path, :in?, app_url_helpers.methods)
assert_operator(app_url_helpers, :respond_to?, :root_path)
end

test "engine lazily loads routes when checking respond_to?" do
require "#{app_path}/config/environment"

assert_not_operator(:root_path, :in?, engine_url_helpers.methods)
assert_operator(engine_url_helpers, :respond_to?, :root_path)
end

test "app lazily loads routes when making a request" do
require "#{app_path}/config/environment"

@app = Rails.application

assert_not_operator(:root_path, :in?, app_url_helpers.methods)
response = get("/")
assert_equal(200, response.first)
end

test "engine lazily loads routes when making a request" do
require "#{app_path}/config/environment"

@app = Rails.application

assert_not_operator(:root_path, :in?, engine_url_helpers.methods)
response = get("/plugin/")
assert_equal(200, response.first)
end

private
def build_app
super

app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
root to: proc { [200, {}, []] }
mount Plugin::Engine, at: "/plugin"
end
RUBY

build_engine
end

def build_engine
engine "plugin" do |plugin|
plugin.write "lib/plugin.rb", <<~RUBY
module Plugin
class Engine < ::Rails::Engine
end
end
RUBY
plugin.write "config/routes.rb", <<~RUBY
Plugin::Engine.routes.draw do
root to: proc { [200, {}, []] }
end
RUBY
end
end

def app_url_helpers
Rails.application.routes.url_helpers
end

def engine_url_helpers
Plugin::Engine.routes.url_helpers
end
end
end
end

0 comments on commit b0b0a0e

Please sign in to comment.