aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2017-09-26 13:27:53 -0400
committereileencodes <eileencodes@gmail.com>2017-10-04 09:17:21 -0400
commit59a02fb7bcbe68f26e1e7fdcec45c00c66e4a065 (patch)
treef455ef1b6fa18c6cdc11540d0af365937e8a7b35 /railties
parentf1e89628fe3d18427e3b6644b1e7f3ef965ecd5b (diff)
downloadrails-59a02fb7bcbe68f26e1e7fdcec45c00c66e4a065.tar.gz
rails-59a02fb7bcbe68f26e1e7fdcec45c00c66e4a065.tar.bz2
rails-59a02fb7bcbe68f26e1e7fdcec45c00c66e4a065.zip
Implement H2 Early Hints for Rails
When puma/puma#1403 is merged Puma will support the Early Hints status code for sending assets before a request has finished. While the Early Hints spec is still in draft, this PR prepares Rails to allowing this status code. If the proxy server supports Early Hints, it will send H2 pushes to the client. This PR adds a method for setting Early Hints Link headers via Rails, and also automatically sends Early Hints if supported from the `stylesheet_link_tag` and the `javascript_include_tag`. Once puma supports Early Hints the `--early-hints` argument can be passed to the server to enable this or set in the puma config with `early_hints(true)`. Note that for Early Hints to work in the browser the requirements are 1) a proxy that can handle H2, and 2) HTTPS. To start the server with Early Hints enabled pass `--early-hints` to `rails s`. This has been verified to work with h2o, Puma, and Rails with Chrome. The commit adds a new option to the rails server to enable early hints for Puma. Early Hints spec: https://tools.ietf.org/html/draft-ietf-httpbis-early-hints-04 [Eileen M. Uchitelle, Aaron Patterson]
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/commands/server/server_command.rb8
-rw-r--r--railties/test/commands/server_test.rb12
2 files changed, 19 insertions, 1 deletions
diff --git a/railties/lib/rails/commands/server/server_command.rb b/railties/lib/rails/commands/server/server_command.rb
index 785265d766..5b5037d3de 100644
--- a/railties/lib/rails/commands/server/server_command.rb
+++ b/railties/lib/rails/commands/server/server_command.rb
@@ -127,6 +127,7 @@ module Rails
class_option "dev-caching", aliases: "-C", type: :boolean, default: nil,
desc: "Specifies whether to perform caching in development."
class_option "restart", type: :boolean, default: nil, hide: true
+ class_option "early_hints", type: :boolean, default: nil, desc: "Enables HTTP/2 early hints."
def initialize(args = [], local_options = {}, config = {})
@original_options = local_options
@@ -161,7 +162,8 @@ module Rails
daemonize: options[:daemon],
pid: pid,
caching: options["dev-caching"],
- restart_cmd: restart_command
+ restart_cmd: restart_command,
+ early_hints: early_hints
}
end
end
@@ -227,6 +229,10 @@ module Rails
"bin/rails server #{@server} #{@original_options.join(" ")} --restart"
end
+ def early_hints
+ options[:early_hints]
+ end
+
def pid
File.expand_path(options[:pid])
end
diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb
index 556c2289e7..a6201e4f04 100644
--- a/railties/test/commands/server_test.rb
+++ b/railties/test/commands/server_test.rb
@@ -81,6 +81,18 @@ class Rails::ServerTest < ActiveSupport::TestCase
assert_equal false, options[:caching]
end
+ def test_early_hints_with_option
+ args = ["--early-hints"]
+ options = parse_arguments(args)
+ assert_equal true, options[:early_hints]
+ end
+
+ def test_early_hints_is_nil_by_default
+ args = []
+ options = parse_arguments(args)
+ assert_nil options[:early_hints]
+ end
+
def test_log_stdout
with_rack_env nil do
with_rails_env nil do