-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
21 changed files
with
283 additions
and
11 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
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 |
Oops, something went wrong.