-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
13 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |