rails_http_preload 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/rails_http_preload/middleware.rb +16 -9
- data/lib/rails_http_preload/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e16c99f5f30f2e9646d3618e76c888bdd32a41ad2c83b08fa682343517f5f4ea
|
4
|
+
data.tar.gz: ff66a5d7d823080a01265c76c6880023e98f5215e320b20ef2aa1952006fd551
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db26d1e098f70e2f1b1e23282e44a92d01ebcbb723693588c5e5d18530e2bc4c5068b7c21ed328a729907e42b82ec35b377b990c398175a8da7a880b34558eda
|
7
|
+
data.tar.gz: 79d9346fe00db3a23e25bf23de8ce9fc928d0640073365d3f3b8a8be8d7b535e5a9e4655d190b81919cbf5c3bb2c0b05e1278e1b2be8ae06644449b5084f5990
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -47,7 +47,7 @@ In the best case scenario, your observed Largest Contentful Paint times should b
|
|
47
47
|
|
48
48
|
### Worst Case Scenario
|
49
49
|
|
50
|
-
In the worst case, nothing happens. For example, your CDN doesn't support early hints. No problem, you've just added
|
50
|
+
In the worst case, nothing happens. For example, your CDN doesn't support early hints. No problem, you've just added a few bytes of HTTP headers to your responses, it's basically a no-op. You cannot really create new bugs or bad behavior by including this header.
|
51
51
|
## Installation
|
52
52
|
|
53
53
|
Add this line to your application's Gemfile:
|
@@ -13,7 +13,7 @@ module RailsHttpPreload
|
|
13
13
|
def call(env)
|
14
14
|
response = @app.call(env)
|
15
15
|
|
16
|
-
response[1]["Link"] = preconnect_header if required?(response, ActionDispatch::Request.new(env))
|
16
|
+
response[1]["Link"] = preconnect_header(response) if required?(response, ActionDispatch::Request.new(env))
|
17
17
|
|
18
18
|
response
|
19
19
|
end
|
@@ -21,8 +21,7 @@ module RailsHttpPreload
|
|
21
21
|
# TODO: Should we just add it to every response anyway and let the browser
|
22
22
|
# figure it out?
|
23
23
|
def required?(response, request)
|
24
|
-
html?(response) &&
|
25
|
-
!already_connected?(request)
|
24
|
+
config.asset_host.present? && html?(response) && !already_connected?(request)
|
26
25
|
end
|
27
26
|
|
28
27
|
# Adding this header to, for example, a JSON response would be pointless
|
@@ -31,18 +30,26 @@ module RailsHttpPreload
|
|
31
30
|
response[1]["Content-Type"].match?("html")
|
32
31
|
end
|
33
32
|
|
33
|
+
def current_asset_host
|
34
|
+
ActionController::Base.helpers.compute_asset_host("", host: config.asset_host)
|
35
|
+
end
|
36
|
+
|
34
37
|
# If the asset host is equal to the request domain, no need to add.
|
35
38
|
def already_connected?(request)
|
36
|
-
|
37
|
-
ActionController::Base.helpers.compute_asset_host("", host: config.asset_host) ==
|
38
|
-
ActionController::Base.helpers.compute_asset_host("", { protocol: protocol, host: request.domain })
|
39
|
+
request.base_url == current_asset_host
|
39
40
|
end
|
40
41
|
|
41
|
-
def preconnect_header
|
42
|
-
[
|
43
|
-
|
42
|
+
def preconnect_header(response)
|
43
|
+
header = [
|
44
|
+
current_asset_host,
|
44
45
|
*config.additional_urls
|
45
46
|
].compact.map { |url| create_link_header(url) }.join(", ")
|
47
|
+
|
48
|
+
if response[1]["Link"]
|
49
|
+
"#{header}, #{response[1]["Link"]}"
|
50
|
+
else
|
51
|
+
header
|
52
|
+
end
|
46
53
|
end
|
47
54
|
|
48
55
|
def create_link_header(url)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_http_preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Berkopec
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
|
-
rubygems_version: 3.
|
98
|
+
rubygems_version: 3.4.12
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: Automatically add an HTTP header to Rails apps to use 103 Early Hints
|