-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add uncapped preload headers for style, script assets (#11504)
* Add unlimited preload headers for style, script assets changelog: Internal, Performance, Add preload headers for all style, script assets * Update spec assertions for dropped whitespace * Add specs * Append with string mutation See: https://github.com/18F/identity-idp/pull/11504/files#r1841273216 Co-authored-by: Zach Margolis <[email protected]> * Use plain tag helper for generating script tag We don't need any of this anymore: - Asset pipeline lookup - Preload headers handling - Server push - Multiple source concatenation - Nonce handling crossorigin as a boolean attribute is same as crossorigin=anonymous * Add spec for preload_links_header attribute handling * Evaluate crossorigin once Previously evaluated per script, even though the result would always be the same * Micro-optimize append * Lowercase header key * Use presence to toggle between true/nil * Sync specs to use lowercase link key --------- Co-authored-by: Zach Margolis <[email protected]>
- Loading branch information
1 parent
f53a5fd
commit 0ad50f9
Showing
6 changed files
with
114 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class AssetPreloadLinker | ||
def self.append(headers:, as:, url:, crossorigin: false, integrity: nil) | ||
header = +headers['link'].to_s | ||
header << ',' if header != '' | ||
header << "<#{url}>;rel=preload;as=#{as}" | ||
header << ';crossorigin' if crossorigin | ||
header << ";integrity=#{integrity}" if integrity | ||
headers['link'] = header | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe AssetPreloadLinker do | ||
describe '.append' do | ||
let(:link) { nil } | ||
let(:as) { 'script' } | ||
let(:url) { '/script.js' } | ||
let(:crossorigin) { nil } | ||
let(:integrity) { nil } | ||
let(:headers) { { 'link' => link } } | ||
subject(:result) do | ||
AssetPreloadLinker.append(**{ headers:, as:, url:, crossorigin:, integrity: }.compact) | ||
end | ||
|
||
context 'with absent link value' do | ||
let(:link) { nil } | ||
|
||
it 'returns a string with only the appended link' do | ||
expect(result).to eq('</script.js>;rel=preload;as=script') | ||
end | ||
end | ||
|
||
context 'with empty link value' do | ||
let(:link) { '' } | ||
|
||
it 'returns a string with only the appended link' do | ||
expect(result).to eq('</script.js>;rel=preload;as=script') | ||
end | ||
end | ||
|
||
context 'with non-empty link value' do | ||
let(:link) { '</a.js>;rel=preload;as=script' } | ||
|
||
it 'returns a comma-separated link value of the new and existing link' do | ||
expect(result).to eq('</a.js>;rel=preload;as=script,</script.js>;rel=preload;as=script') | ||
end | ||
|
||
context 'with existing link value as frozen string' do | ||
let(:link) { '</a.js>;rel=preload;as=script'.freeze } | ||
|
||
it 'returns a comma-separated link value of the new and existing link' do | ||
expect(result).to eq('</a.js>;rel=preload;as=script,</script.js>;rel=preload;as=script') | ||
end | ||
end | ||
end | ||
|
||
context 'with crossorigin option' do | ||
let(:crossorigin) { true } | ||
|
||
it 'includes crossorigin link param' do | ||
expect(result).to eq('</script.js>;rel=preload;as=script;crossorigin') | ||
end | ||
end | ||
|
||
context 'with integrity option' do | ||
let(:integrity) { 'abc123' } | ||
|
||
it 'includes integrity link param' do | ||
expect(result).to eq('</script.js>;rel=preload;as=script;integrity=abc123') | ||
end | ||
end | ||
end | ||
end |