Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bd2542b7
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
bd2542b7
编写于
10月 04, 2017
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
10月 04, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #30744 from eileencodes/early-hints
Implement H2 Early Hints for Rails
上级
df281467
59a02fb7
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
93 addition
and
6 deletion
+93
-6
actionpack/CHANGELOG.md
actionpack/CHANGELOG.md
+8
-0
actionpack/lib/action_dispatch/http/request.rb
actionpack/lib/action_dispatch/http/request.rb
+17
-0
actionpack/test/dispatch/request_test.rb
actionpack/test/dispatch/request_test.rb
+15
-0
actionview/lib/action_view/helpers/asset_tag_helper.rb
actionview/lib/action_view/helpers/asset_tag_helper.rb
+26
-4
actionview/test/template/asset_tag_helper_test.rb
actionview/test/template/asset_tag_helper_test.rb
+4
-1
actionview/test/template/javascript_helper_test.rb
actionview/test/template/javascript_helper_test.rb
+4
-0
railties/lib/rails/commands/server/server_command.rb
railties/lib/rails/commands/server/server_command.rb
+7
-1
railties/test/commands/server_test.rb
railties/test/commands/server_test.rb
+12
-0
未找到文件。
actionpack/CHANGELOG.md
浏览文件 @
bd2542b7
*
Add ability to enable Early Hints for HTTP/2
If supported by the server, and enabled in Puma this allows H2 Early Hints to be used.
The `javascript_include_tag` and the `stylesheet_link_tag` automatically add Early Hints if requested.
*Eileen M. Uchitelle*, *Aaron Patterson*
*
Simplify cookies middleware with key rotation support
Use the `rotate` method for both `MessageEncryptor` and
...
...
actionpack/lib/action_dispatch/http/request.rb
浏览文件 @
bd2542b7
...
...
@@ -199,6 +199,23 @@ def headers
@headers
||=
Http
::
Headers
.
new
(
self
)
end
# Early Hints is an HTTP/2 status code that indicates hints to help a client start
# making preparations for processing the final response.
#
# If the env contains +rack.early_hints+ then the server accepts HTTP2 push for Link headers.
#
# The +send_early_hints+ method accepts an hash of links as follows:
#
# send_early_hints("Link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload")
#
# If you are using +javascript_include_tag+ or +stylesheet_link_tag+ the
# Early Hints headers are included by default if supported.
def
send_early_hints
(
links
)
return
unless
env
[
"rack.early_hints"
]
env
[
"rack.early_hints"
].
call
(
links
)
end
# Returns a +String+ with the last requested path including their params.
#
# # get '/foo'
...
...
actionpack/test/dispatch/request_test.rb
浏览文件 @
bd2542b7
...
...
@@ -1304,3 +1304,18 @@ class RequestFormData < BaseRequestTest
assert
!
request
.
form_data?
end
end
class
EarlyHintsRequestTest
<
BaseRequestTest
def
setup
super
@env
[
"rack.early_hints"
]
=
lambda
{
|
links
|
links
}
@request
=
stub_request
end
test
"when early hints is set in the env link headers are sent"
do
early_hints
=
@request
.
send_early_hints
(
"Link"
=>
"</style.css>; rel=preload; as=style
\n
</script.js>; rel=preload"
)
expected_hints
=
{
"Link"
=>
"</style.css>; rel=preload; as=style
\n
</script.js>; rel=preload"
}
assert_equal
expected_hints
,
early_hints
end
end
actionview/lib/action_view/helpers/asset_tag_helper.rb
浏览文件 @
bd2542b7
...
...
@@ -37,6 +37,9 @@ module AssetTagHelper
# When the Asset Pipeline is enabled, you can pass the name of your manifest as
# source, and include other JavaScript or CoffeeScript files inside the manifest.
#
# If the server supports Early Hints header links for these assets will be
# automatically pushed.
#
# ==== Options
#
# When the last parameter is a hash you can add HTML attributes using that
...
...
@@ -77,12 +80,20 @@ module AssetTagHelper
def
javascript_include_tag
(
*
sources
)
options
=
sources
.
extract_options!
.
stringify_keys
path_options
=
options
.
extract!
(
"protocol"
,
"extname"
,
"host"
,
"skip_pipeline"
).
symbolize_keys
sources
.
uniq
.
map
{
|
source
|
early_hints_links
=
[]
sources_tags
=
sources
.
uniq
.
map
{
|
source
|
href
=
path_to_javascript
(
source
,
path_options
)
early_hints_links
<<
"<
#{
href
}
>; rel=preload; as=script"
tag_options
=
{
"src"
=>
path_to_javascript
(
source
,
path_options
)
"src"
=>
href
}.
merge!
(
options
)
content_tag
(
"script"
.
freeze
,
""
,
tag_options
)
}.
join
(
"
\n
"
).
html_safe
request
.
send_early_hints
(
"Link"
=>
early_hints_links
.
join
(
"
\n
"
))
sources_tags
end
# Returns a stylesheet link tag for the sources specified as arguments. If
...
...
@@ -92,6 +103,9 @@ def javascript_include_tag(*sources)
# to "screen", so you must explicitly set it to "all" for the stylesheet(s) to
# apply to all media types.
#
# If the server supports Early Hints header links for these assets will be
# automatically pushed.
#
# stylesheet_link_tag "style"
# # => <link href="/assets/style.css" media="screen" rel="stylesheet" />
#
...
...
@@ -113,14 +127,22 @@ def javascript_include_tag(*sources)
def
stylesheet_link_tag
(
*
sources
)
options
=
sources
.
extract_options!
.
stringify_keys
path_options
=
options
.
extract!
(
"protocol"
,
"host"
,
"skip_pipeline"
).
symbolize_keys
sources
.
uniq
.
map
{
|
source
|
early_hints_links
=
[]
sources_tags
=
sources
.
uniq
.
map
{
|
source
|
href
=
path_to_stylesheet
(
source
,
path_options
)
early_hints_links
<<
"<
#{
href
}
>; rel=preload; as=stylesheet"
tag_options
=
{
"rel"
=>
"stylesheet"
,
"media"
=>
"screen"
,
"href"
=>
path_to_stylesheet
(
source
,
path_options
)
"href"
=>
href
}.
merge!
(
options
)
tag
(
:link
,
tag_options
)
}.
join
(
"
\n
"
).
html_safe
request
.
send_early_hints
(
"Link"
=>
early_hints_links
.
join
(
"
\n
"
))
sources_tags
end
# Returns a link tag that browsers and feed readers can use to auto-detect
...
...
actionview/test/template/asset_tag_helper_test.rb
浏览文件 @
bd2542b7
...
...
@@ -19,6 +19,7 @@ def protocol() "http://" end
def
ssl?
()
false
end
def
host_with_port
()
"localhost"
end
def
base_url
()
"http://www.example.com"
end
def
send_early_hints
(
links
)
end
end
.
new
@controller
.
request
=
@request
...
...
@@ -653,7 +654,9 @@ def setup
@controller
=
BasicController
.
new
@controller
.
config
.
relative_url_root
=
"/collaboration/hieraki"
@request
=
Struct
.
new
(
:protocol
,
:base_url
).
new
(
"gopher://"
,
"gopher://www.example.com"
)
@request
=
Struct
.
new
(
:protocol
,
:base_url
)
do
def
send_early_hints
(
links
);
end
end
.
new
(
"gopher://"
,
"gopher://www.example.com"
)
@controller
.
request
=
@request
end
...
...
actionview/test/template/javascript_helper_test.rb
浏览文件 @
bd2542b7
...
...
@@ -6,11 +6,15 @@ class JavaScriptHelperTest < ActionView::TestCase
tests
ActionView
::
Helpers
::
JavaScriptHelper
attr_accessor
:output_buffer
attr_reader
:request
setup
do
@old_escape_html_entities_in_json
=
ActiveSupport
.
escape_html_entities_in_json
ActiveSupport
.
escape_html_entities_in_json
=
true
@template
=
self
@request
=
Class
.
new
do
def
send_early_hints
(
links
)
end
end
.
new
end
def
teardown
...
...
railties/lib/rails/commands/server/server_command.rb
浏览文件 @
bd2542b7
...
...
@@ -127,6 +127,7 @@ class ServerCommand < Base # :nodoc:
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 @@ def server_options
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 @@ def restart_command
"bin/rails server
#{
@server
}
#{
@original_options
.
join
(
" "
)
}
--restart"
end
def
early_hints
options
[
:early_hints
]
end
def
pid
File
.
expand_path
(
options
[
:pid
])
end
...
...
railties/test/commands/server_test.rb
浏览文件 @
bd2542b7
...
...
@@ -81,6 +81,18 @@ def test_caching_with_option
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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录