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 called.
Executes the first routes reload in middleware, or when the route set url_helpers is called. 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
19 changed files
with
249 additions
and
9 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
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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
# :markup: markdown | ||
|
||
require "action_dispatch/routing/route_set" | ||
|
||
module Rails | ||
class Engine | ||
class RouteSet < ActionDispatch::Routing::RouteSet # :nodoc: | ||
def initialize(config = DEFAULT_CONFIG) | ||
super | ||
named_routes.url_helpers_module.prepend(method_missing_module) | ||
named_routes.path_helpers_module.prepend(method_missing_module) | ||
end | ||
|
||
private | ||
def method_missing_module | ||
@method_missing_module ||= Module.new do | ||
private | ||
def method_missing(method_name, *args, &block) | ||
application = Rails.application | ||
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 | ||
|
||
def respond_to_missing?(method_name, *args) | ||
application = Rails.application | ||
if application && application.initialized? && application.routes_reloader.execute_unless_loaded | ||
ActiveSupport.run_load_hooks(:after_routes_loaded, application) | ||
respond_to?(method_name, *args) | ||
else | ||
super(method_name, *args) | ||
end | ||
end | ||
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
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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rails | ||
module Rack | ||
class LoadRoutes | ||
def initialize(app, routes_reloader) | ||
@app = app | ||
@called = false | ||
@routes_reloader = routes_reloader | ||
end | ||
|
||
def call(env) | ||
@called ||= begin | ||
@routes_reloader.execute_unless_loaded | ||
true | ||
end | ||
@app.call(env) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "isolation/abstract_unit" | ||
require "active_support/log_subscriber/test_helper" | ||
require "rack/test" | ||
|
||
module ApplicationTests | ||
module RackTests | ||
class LoggerTest < ActiveSupport::TestCase | ||
include ActiveSupport::Testing::Isolation | ||
include ActiveSupport::LogSubscriber::TestHelper | ||
include Rack::Test::Methods | ||
|
||
setup do | ||
build_app | ||
require "#{app_path}/config/environment" | ||
end | ||
|
||
teardown do | ||
teardown_app | ||
end | ||
|
||
test "loads routes on request" do | ||
assert_equal(false, Rails.application.routes_reloader.loaded) | ||
|
||
get "/test" | ||
|
||
assert_equal(true, Rails.application.routes_reloader.loaded) | ||
end | ||
|
||
test "loads routes only once" do | ||
assert_called(Rails.application.routes_reloader, :execute_unless_loaded, 1) do | ||
5.times { get "/test" } | ||
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 |
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
Oops, something went wrong.