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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ab167b50cca5eecf4e8b289c407f12cd14fd96b343bd4562276c797fc8d0b7f
4
- data.tar.gz: 9fc88a983e9e44f7c03ee37e8eb915c8f7766ad63410f8e18fc82b1279fbb855
3
+ metadata.gz: e16c99f5f30f2e9646d3618e76c888bdd32a41ad2c83b08fa682343517f5f4ea
4
+ data.tar.gz: ff66a5d7d823080a01265c76c6880023e98f5215e320b20ef2aa1952006fd551
5
5
  SHA512:
6
- metadata.gz: 5ac3bb7fd8268eba569957770020c54fde8b67409d3376c41f6ac30e4373eb64e73636047338dd47dcc684b1f33da16af2d84e2171b8ec7d84a1105ebd72e180
7
- data.tar.gz: 28f2a8d1585a17d899279ffab85979d5eb4744af9c1f6cfdf3ea98008ae47320c9d154bc46d224a0f0be056614b4f9951a8c1323eb82d273cb38da1191363b9b
6
+ metadata.gz: db26d1e098f70e2f1b1e23282e44a92d01ebcbb723693588c5e5d18530e2bc4c5068b7c21ed328a729907e42b82ec35b377b990c398175a8da7a880b34558eda
7
+ data.tar.gz: 79d9346fe00db3a23e25bf23de8ce9fc928d0640073365d3f3b8a8be8d7b535e5a9e4655d190b81919cbf5c3bb2c0b05e1278e1b2be8ae06644449b5084f5990
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-05-29
4
+
5
+ - Bugfix: blank asset hosts should not add a blank link header
6
+ - Bugfix: don't overwrite existing link headers in responses
3
7
  ## [0.1.0] - 2022-11-21
4
8
 
5
9
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_http_preload (0.1.0)
4
+ rails_http_preload (0.2.0)
5
5
  actionpack
6
6
  actionview
7
7
  rails
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 ~1kb 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.
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
- protocol = request.protocol.gsub("://", "")
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
- ActionController::Base.helpers.compute_asset_host("", host: config.asset_host),
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsHttpPreload
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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.1.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: 2023-05-26 00:00:00.000000000 Z
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.3.15
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