rails_http_preload 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +18 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +186 -0
- data/README.md +89 -0
- data/Rakefile +16 -0
- data/lib/rails_http_preload/middleware.rb +56 -0
- data/lib/rails_http_preload/railtie.rb +16 -0
- data/lib/rails_http_preload/version.rb +5 -0
- data/lib/rails_http_preload.rb +21 -0
- data/rails_http_preload.gemspec +38 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1ab167b50cca5eecf4e8b289c407f12cd14fd96b343bd4562276c797fc8d0b7f
|
4
|
+
data.tar.gz: 9fc88a983e9e44f7c03ee37e8eb915c8f7766ad63410f8e18fc82b1279fbb855
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ac3bb7fd8268eba569957770020c54fde8b67409d3376c41f6ac30e4373eb64e73636047338dd47dcc684b1f33da16af2d84e2171b8ec7d84a1105ebd72e180
|
7
|
+
data.tar.gz: 28f2a8d1585a17d899279ffab85979d5eb4744af9c1f6cfdf3ea98008ae47320c9d154bc46d224a0f0be056614b4f9951a8c1323eb82d273cb38da1191363b9b
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Style/StringLiterals:
|
7
|
+
Enabled: true
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/StringLiteralsInInterpolation:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Layout/LineLength:
|
18
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nate.berkopec@gusto.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails_http_preload (0.1.0)
|
5
|
+
actionpack
|
6
|
+
actionview
|
7
|
+
rails
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actioncable (7.0.4)
|
13
|
+
actionpack (= 7.0.4)
|
14
|
+
activesupport (= 7.0.4)
|
15
|
+
nio4r (~> 2.0)
|
16
|
+
websocket-driver (>= 0.6.1)
|
17
|
+
actionmailbox (7.0.4)
|
18
|
+
actionpack (= 7.0.4)
|
19
|
+
activejob (= 7.0.4)
|
20
|
+
activerecord (= 7.0.4)
|
21
|
+
activestorage (= 7.0.4)
|
22
|
+
activesupport (= 7.0.4)
|
23
|
+
mail (>= 2.7.1)
|
24
|
+
net-imap
|
25
|
+
net-pop
|
26
|
+
net-smtp
|
27
|
+
actionmailer (7.0.4)
|
28
|
+
actionpack (= 7.0.4)
|
29
|
+
actionview (= 7.0.4)
|
30
|
+
activejob (= 7.0.4)
|
31
|
+
activesupport (= 7.0.4)
|
32
|
+
mail (~> 2.5, >= 2.5.4)
|
33
|
+
net-imap
|
34
|
+
net-pop
|
35
|
+
net-smtp
|
36
|
+
rails-dom-testing (~> 2.0)
|
37
|
+
actionpack (7.0.4)
|
38
|
+
actionview (= 7.0.4)
|
39
|
+
activesupport (= 7.0.4)
|
40
|
+
rack (~> 2.0, >= 2.2.0)
|
41
|
+
rack-test (>= 0.6.3)
|
42
|
+
rails-dom-testing (~> 2.0)
|
43
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
44
|
+
actiontext (7.0.4)
|
45
|
+
actionpack (= 7.0.4)
|
46
|
+
activerecord (= 7.0.4)
|
47
|
+
activestorage (= 7.0.4)
|
48
|
+
activesupport (= 7.0.4)
|
49
|
+
globalid (>= 0.6.0)
|
50
|
+
nokogiri (>= 1.8.5)
|
51
|
+
actionview (7.0.4)
|
52
|
+
activesupport (= 7.0.4)
|
53
|
+
builder (~> 3.1)
|
54
|
+
erubi (~> 1.4)
|
55
|
+
rails-dom-testing (~> 2.0)
|
56
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
57
|
+
activejob (7.0.4)
|
58
|
+
activesupport (= 7.0.4)
|
59
|
+
globalid (>= 0.3.6)
|
60
|
+
activemodel (7.0.4)
|
61
|
+
activesupport (= 7.0.4)
|
62
|
+
activerecord (7.0.4)
|
63
|
+
activemodel (= 7.0.4)
|
64
|
+
activesupport (= 7.0.4)
|
65
|
+
activestorage (7.0.4)
|
66
|
+
actionpack (= 7.0.4)
|
67
|
+
activejob (= 7.0.4)
|
68
|
+
activerecord (= 7.0.4)
|
69
|
+
activesupport (= 7.0.4)
|
70
|
+
marcel (~> 1.0)
|
71
|
+
mini_mime (>= 1.1.0)
|
72
|
+
activesupport (7.0.4)
|
73
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
74
|
+
i18n (>= 1.6, < 2)
|
75
|
+
minitest (>= 5.1)
|
76
|
+
tzinfo (~> 2.0)
|
77
|
+
ast (2.4.2)
|
78
|
+
builder (3.2.4)
|
79
|
+
concurrent-ruby (1.1.10)
|
80
|
+
crass (1.0.6)
|
81
|
+
date (3.3.3)
|
82
|
+
erubi (1.12.0)
|
83
|
+
globalid (1.0.0)
|
84
|
+
activesupport (>= 5.0)
|
85
|
+
i18n (1.12.0)
|
86
|
+
concurrent-ruby (~> 1.0)
|
87
|
+
json (2.6.3)
|
88
|
+
loofah (2.19.1)
|
89
|
+
crass (~> 1.0.2)
|
90
|
+
nokogiri (>= 1.5.9)
|
91
|
+
mail (2.8.0)
|
92
|
+
mini_mime (>= 0.1.1)
|
93
|
+
net-imap
|
94
|
+
net-pop
|
95
|
+
net-smtp
|
96
|
+
marcel (1.0.2)
|
97
|
+
method_source (1.0.0)
|
98
|
+
mini_mime (1.1.2)
|
99
|
+
mini_portile2 (2.8.1)
|
100
|
+
minitest (5.16.3)
|
101
|
+
net-imap (0.3.4)
|
102
|
+
date
|
103
|
+
net-protocol
|
104
|
+
net-pop (0.1.2)
|
105
|
+
net-protocol
|
106
|
+
net-protocol (0.2.1)
|
107
|
+
timeout
|
108
|
+
net-smtp (0.3.3)
|
109
|
+
net-protocol
|
110
|
+
nio4r (2.5.8)
|
111
|
+
nokogiri (1.13.10)
|
112
|
+
mini_portile2 (~> 2.8.0)
|
113
|
+
racc (~> 1.4)
|
114
|
+
parallel (1.22.1)
|
115
|
+
parser (3.1.3.0)
|
116
|
+
ast (~> 2.4.1)
|
117
|
+
racc (1.6.2)
|
118
|
+
rack (2.2.5)
|
119
|
+
rack-test (2.0.2)
|
120
|
+
rack (>= 1.3)
|
121
|
+
rails (7.0.4)
|
122
|
+
actioncable (= 7.0.4)
|
123
|
+
actionmailbox (= 7.0.4)
|
124
|
+
actionmailer (= 7.0.4)
|
125
|
+
actionpack (= 7.0.4)
|
126
|
+
actiontext (= 7.0.4)
|
127
|
+
actionview (= 7.0.4)
|
128
|
+
activejob (= 7.0.4)
|
129
|
+
activemodel (= 7.0.4)
|
130
|
+
activerecord (= 7.0.4)
|
131
|
+
activestorage (= 7.0.4)
|
132
|
+
activesupport (= 7.0.4)
|
133
|
+
bundler (>= 1.15.0)
|
134
|
+
railties (= 7.0.4)
|
135
|
+
rails-dom-testing (2.0.3)
|
136
|
+
activesupport (>= 4.2.0)
|
137
|
+
nokogiri (>= 1.6)
|
138
|
+
rails-html-sanitizer (1.4.4)
|
139
|
+
loofah (~> 2.19, >= 2.19.1)
|
140
|
+
railties (7.0.4)
|
141
|
+
actionpack (= 7.0.4)
|
142
|
+
activesupport (= 7.0.4)
|
143
|
+
method_source
|
144
|
+
rake (>= 12.2)
|
145
|
+
thor (~> 1.0)
|
146
|
+
zeitwerk (~> 2.5)
|
147
|
+
rainbow (3.1.1)
|
148
|
+
rake (13.0.6)
|
149
|
+
regexp_parser (2.6.1)
|
150
|
+
rexml (3.2.5)
|
151
|
+
rubocop (1.41.1)
|
152
|
+
json (~> 2.3)
|
153
|
+
parallel (~> 1.10)
|
154
|
+
parser (>= 3.1.2.1)
|
155
|
+
rainbow (>= 2.2.2, < 4.0)
|
156
|
+
regexp_parser (>= 1.8, < 3.0)
|
157
|
+
rexml (>= 3.2.5, < 4.0)
|
158
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
159
|
+
ruby-progressbar (~> 1.7)
|
160
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
161
|
+
rubocop-ast (1.24.1)
|
162
|
+
parser (>= 3.1.1.0)
|
163
|
+
ruby-progressbar (1.11.0)
|
164
|
+
thor (1.2.1)
|
165
|
+
timeout (0.3.1)
|
166
|
+
tzinfo (2.0.5)
|
167
|
+
concurrent-ruby (~> 1.0)
|
168
|
+
unicode-display_width (2.3.0)
|
169
|
+
websocket-driver (0.7.5)
|
170
|
+
websocket-extensions (>= 0.1.0)
|
171
|
+
websocket-extensions (0.1.5)
|
172
|
+
zeitwerk (2.6.6)
|
173
|
+
|
174
|
+
PLATFORMS
|
175
|
+
arm64-darwin-22
|
176
|
+
x86_64-linux
|
177
|
+
|
178
|
+
DEPENDENCIES
|
179
|
+
minitest
|
180
|
+
rack-test
|
181
|
+
rails_http_preload!
|
182
|
+
rake
|
183
|
+
rubocop
|
184
|
+
|
185
|
+
BUNDLED WITH
|
186
|
+
2.4.1
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# RailsHttpPreload
|
2
|
+
|
3
|
+
Automatically add a `link` header directing clients to `preconnect` to your `asset_host` to HTML document responses in Rails.
|
4
|
+
|
5
|
+
This gem is intended a test platform for including a new feature into Rails. As of this writing, this feature has not yet been included in Rails.
|
6
|
+
|
7
|
+
The 103 Early Hints status code is intended to allow application developers to signal to the client what critical resources will need to be loaded along with the requested resource. Early Hints can be returned to the client _before_ the actual response. This is much faster than waiting for the server think time to create the actual response.
|
8
|
+
|
9
|
+
CDNs can actually implement and serve Early Hints headers to clients without talking to the origin server. CDNs have decided to do this by caching at the point-of-presence (POP) `Link` headers in successful HTTP responses.
|
10
|
+
|
11
|
+
CDNs which currently support turning Link headers into Early Hints:
|
12
|
+
|
13
|
+
1. Cloudflare
|
14
|
+
2. Fastly
|
15
|
+
3. Others?
|
16
|
+
|
17
|
+
One possible use of early hints, pioneered by Shopify, is to include a `preload` link to the asset domain, if that domain is not the same as the domain of the request.
|
18
|
+
|
19
|
+
For example, before:
|
20
|
+
|
21
|
+
1. Client makes request for mydomain.com.
|
22
|
+
2. 500ms later, client receives 200 response for mydomain.com, and finds a CSS stylesheet at cdn.mydomain.com.
|
23
|
+
3. Client opens connection to cdn.mydomain.com (200ms)
|
24
|
+
4. Client downloads CSS stylesheet (200ms) and renders page.
|
25
|
+
|
26
|
+
With early hints, we can render this page 200ms faster:
|
27
|
+
|
28
|
+
1. Client makes request for mydomain.com.
|
29
|
+
2. At the CDNs PoP, client receives 103 Early Hint response with a `link` to `preconnect` to `cdn.mydomain.com`.
|
30
|
+
3. Client opens connection to cdn.mydomain.com while waiting for the 200 response.
|
31
|
+
4. Client receives 200 response after 500ms.
|
32
|
+
5. Client downloads CSS stylesheet (200ms) and renders page.
|
33
|
+
|
34
|
+
This gem's hypothesis is that **all Rails applications should include a link header to preconnect to the asset host domain if it is not the same as the requested document's domain**.
|
35
|
+
|
36
|
+
All this gem does is add this HTTP header to responses for HTML documents. For example, if you have a Rails app that's live at mydomain.com and whose `asset_host` config is at cdn.mydomain.com, we add this header:
|
37
|
+
|
38
|
+
```
|
39
|
+
link: <cdn.mydomain.com>; rel="preconnect"
|
40
|
+
```
|
41
|
+
|
42
|
+
The header is not added for non-HTML responses.
|
43
|
+
|
44
|
+
### Best Case Scenario
|
45
|
+
|
46
|
+
In the best case scenario, your observed Largest Contentful Paint times should be reduced. The amount will not be predictable, because it depends on how often clients connect to your website with an already-open socket to `cdn.mydomain.com`. Since Chrome and other browsers use socket pools which can keep connections open for a long time, this may happen more often than you think. Or connecting to `cdn.mydomain.com` may not be on the critical path for the Largest Contentful Paint event; i.e. other downloads happening in parallel with setting up that connection are blocking LCP.
|
47
|
+
|
48
|
+
### Worst Case Scenario
|
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.
|
51
|
+
## Installation
|
52
|
+
|
53
|
+
Add this line to your application's Gemfile:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
gem 'rails_http_preload'
|
57
|
+
```
|
58
|
+
|
59
|
+
And then execute:
|
60
|
+
|
61
|
+
$ bundle install
|
62
|
+
|
63
|
+
Or install it yourself as:
|
64
|
+
|
65
|
+
$ gem install rails_http_preload
|
66
|
+
|
67
|
+
## Usage
|
68
|
+
|
69
|
+
If all you want to add is a header for Rails application's `asset_host`, just add this gem and you're done.
|
70
|
+
|
71
|
+
If you would like to add `preconnect` headers to additional URLs, simply configure it in an initializer:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
RailsHttpPreload.config.additional_urls = %w[https://graphql.mydomain.com https://images.mydomain.com]
|
75
|
+
```
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gusto/rails_http_preload. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/gusto/rails_http_preload/blob/main/CODE_OF_CONDUCT.md).
|
86
|
+
|
87
|
+
## Code of Conduct
|
88
|
+
|
89
|
+
Everyone interacting in the RailsHttpPreload project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gusto/rails_http_preload/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_view/helpers/asset_url_helper"
|
4
|
+
require "active_support/concern"
|
5
|
+
require "action_dispatch"
|
6
|
+
|
7
|
+
module RailsHttpPreload
|
8
|
+
class Middleware
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
response = @app.call(env)
|
15
|
+
|
16
|
+
response[1]["Link"] = preconnect_header if required?(response, ActionDispatch::Request.new(env))
|
17
|
+
|
18
|
+
response
|
19
|
+
end
|
20
|
+
|
21
|
+
# TODO: Should we just add it to every response anyway and let the browser
|
22
|
+
# figure it out?
|
23
|
+
def required?(response, request)
|
24
|
+
html?(response) &&
|
25
|
+
!already_connected?(request)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Adding this header to, for example, a JSON response would be pointless
|
29
|
+
# TODO: remove from Turbo frame responses too
|
30
|
+
def html?(response)
|
31
|
+
response[1]["Content-Type"].match?("html")
|
32
|
+
end
|
33
|
+
|
34
|
+
# If the asset host is equal to the request domain, no need to add.
|
35
|
+
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
|
+
end
|
40
|
+
|
41
|
+
def preconnect_header
|
42
|
+
[
|
43
|
+
ActionController::Base.helpers.compute_asset_host("", host: config.asset_host),
|
44
|
+
*config.additional_urls
|
45
|
+
].compact.map { |url| create_link_header(url) }.join(", ")
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_link_header(url)
|
49
|
+
"<#{url}>; rel=preconnect"
|
50
|
+
end
|
51
|
+
|
52
|
+
def config
|
53
|
+
RailsHttpPreload.config
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails"
|
4
|
+
|
5
|
+
module RailsHttpPreload
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer "rails_http_preload.middleware" do |app|
|
8
|
+
app.config.middleware.use RailsHttpPreload::Middleware
|
9
|
+
|
10
|
+
config.after_initialize do
|
11
|
+
RailsHttpPreload.config.asset_host = app.config.asset_host
|
12
|
+
RailsHttpPreload.config.default_asset_host_protocol = app.config.action_controller.default_asset_host_protocol
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rails_http_preload/version"
|
4
|
+
require_relative "rails_http_preload/middleware"
|
5
|
+
require_relative "rails_http_preload/railtie"
|
6
|
+
|
7
|
+
module RailsHttpPreload
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
def self.config
|
11
|
+
@config ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
yield @config
|
16
|
+
end
|
17
|
+
|
18
|
+
class Configuration
|
19
|
+
attr_accessor :additional_urls, :asset_host, :default_asset_host_protocol
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/rails_http_preload/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rails_http_preload"
|
7
|
+
spec.version = RailsHttpPreload::VERSION
|
8
|
+
spec.authors = ["Nate Berkopec"]
|
9
|
+
spec.email = ["nate.berkopec@gusto.com"]
|
10
|
+
|
11
|
+
spec.summary = "Automatically add an HTTP header to Rails apps to use 103 Early Hints"
|
12
|
+
spec.description = "Automatically add a `link` header directing clients to " \
|
13
|
+
"`preconnect` to your `asset_host` to HTML document responses in Rails."
|
14
|
+
spec.homepage = "https://github.com/gusto/rails_http_preload"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/gusto/rails_http_preload"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/Gusto/rails_http_preload/blob/main/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
34
|
+
|
35
|
+
spec.add_dependency "actionpack"
|
36
|
+
spec.add_dependency "actionview"
|
37
|
+
spec.add_dependency "rails"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_http_preload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nate Berkopec
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionview
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Automatically add a `link` header directing clients to `preconnect` to
|
56
|
+
your `asset_host` to HTML document responses in Rails.
|
57
|
+
email:
|
58
|
+
- nate.berkopec@gusto.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/rails_http_preload.rb
|
71
|
+
- lib/rails_http_preload/middleware.rb
|
72
|
+
- lib/rails_http_preload/railtie.rb
|
73
|
+
- lib/rails_http_preload/version.rb
|
74
|
+
- rails_http_preload.gemspec
|
75
|
+
homepage: https://github.com/gusto/rails_http_preload
|
76
|
+
licenses: []
|
77
|
+
metadata:
|
78
|
+
allowed_push_host: https://rubygems.org/
|
79
|
+
homepage_uri: https://github.com/gusto/rails_http_preload
|
80
|
+
source_code_uri: https://github.com/gusto/rails_http_preload
|
81
|
+
changelog_uri: https://github.com/Gusto/rails_http_preload/blob/main/CHANGELOG.md
|
82
|
+
rubygems_mfa_required: 'true'
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 2.6.0
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubygems_version: 3.3.15
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Automatically add an HTTP header to Rails apps to use 103 Early Hints
|
102
|
+
test_files: []
|