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
15 changed files
with
146 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# :markup: markdown | ||
|
||
module ActionDispatch | ||
class Once # :nodoc: | ||
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(@call_count, 1) | ||
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
# :markup: markdown | ||
|
||
require "action_dispatch/routing/route_set" | ||
|
||
module Rails | ||
class Engine | ||
class RouteSet < ActionDispatch::Routing::RouteSet # :nodoc: | ||
attr_accessor :application | ||
|
||
def generate_url_helpers(supports_path) | ||
application = self.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 | ||
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
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rails | ||
module Rack | ||
class LoadRoutes < ActionDispatch::Once # :nodoc: | ||
def initialize(app, routes_reloader) | ||
super(app, proc { routes_reloader.execute_unless_loaded }) | ||
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,32 @@ | ||
# 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 "/blah" | ||
|
||
assert_equal(true, Rails.application.routes_reloader.loaded) | ||
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