-
Notifications
You must be signed in to change notification settings - Fork 22
Force asset manager preflight login #5789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eYinka
wants to merge
1
commit into
main
Choose a base branch
from
force-asset-manager-preflight-login--2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,33 @@ | ||
| /* Warms up the Asset Manager draft-assets session cookie via a single | ||
| * placeholder image request, then reloads any draft images already on | ||
| * the page once that request has settled (success or failure). | ||
| */ | ||
| /* istanbul ignore next */ | ||
| window.GOVUK = window.GOVUK || {}; | ||
|
|
||
| (function (root) { | ||
| function draftAssetImages () { | ||
| return [...document.images].filter((image) => | ||
| image.src.includes('assets.') // currently, asset urls in draft preview point to their live link: 'assets.xyz' instead of 'draft-assets.xyz'. Created a backlog item for this: https://gov-uk.atlassian.net/browse/WHIT-4008. | ||
| ) | ||
| } | ||
|
|
||
| function reloadDraftImages (draftAssets) { | ||
| draftAssets.forEach((image) => { | ||
| // re-setting src tries to fetch the image again, with the hope that a prior success is just served from cache | ||
| image.setAttribute('src', image.getAttribute('src')) | ||
| }) | ||
| } | ||
|
|
||
| root.GOVUK.warmUpAssetManagerSession = function (placeholderAssetUrl) { | ||
| const draftAssets = draftAssetImages() | ||
| // fewer than 2 images means there's no concurrent-request race to fix, so nothing to warm up | ||
| if (draftAssets.length < 2) return | ||
|
|
||
| const reload = () => reloadDraftImages(draftAssets) | ||
| const placeholder = new Image() | ||
| placeholder.onload = reload | ||
| placeholder.onerror = reload | ||
| placeholder.src = placeholderAssetUrl | ||
| } | ||
| }(window)) |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,4 +24,5 @@ | |
| <%= yield %> | ||
| </main> | ||
| </div> | ||
| <%= render "shared/asset_manager_session" %> | ||
| <% end %> | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -14,4 +14,5 @@ | |
| <span id="Top"></span> | ||
| <%= yield %> | ||
| </main> | ||
| <%= render "shared/asset_manager_session" %> | ||
| <% end %> | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -60,5 +60,6 @@ | |
| </div> | ||
| <% end %> | ||
| </main> | ||
| <%= render "shared/asset_manager_session" %> | ||
| </div> | ||
| <% end %> | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -34,5 +34,6 @@ | |
| </div> | ||
| </div> | ||
| </main> | ||
| <%= render "shared/asset_manager_session" %> | ||
| </div> | ||
| <% end %> | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,5 +17,6 @@ | |
| <span id="Top"></span> | ||
| <%= yield %> | ||
| </main> | ||
| <%= render "shared/asset_manager_session" %> | ||
| </div> | ||
| <% end %> | ||
This file contains hidden or 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,18 @@ | ||
| <% if draft_host? %> | ||
| <% | ||
| host = if GovukEnvironment.current == "staging" | ||
| "staging.publishing.service.gov.uk" | ||
| elsif GovukEnvironment.current == "integration" | ||
| "integration.publishing.service.gov.uk" | ||
| else | ||
| "publishing.service.gov.uk" | ||
| end | ||
| placeholder_asset_url = "https://draft-assets.#{host}/media/5e59279b86650c53b2cefbfe/placeholder.jpg" | ||
| %> | ||
| <%= javascript_include_tag "lib/asset-manager-session.js", integrity: false %> | ||
| <%= javascript_tag nonce: true do %> | ||
| window.addEventListener("load", function () { | ||
| GOVUK.warmUpAssetManagerSession('<%= placeholder_asset_url %>') | ||
| }) | ||
| <% end %> | ||
| <% end %> | ||
This file contains hidden or 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,90 @@ | ||
| describe('GOVUK.warmUpAssetManagerSession', function () { | ||
| let originalImage | ||
|
|
||
| const addImage = (src) => { | ||
| const img = document.createElement('img') | ||
| img.src = src | ||
| document.body.appendChild(img) | ||
| return img | ||
| } | ||
|
|
||
| const trackReloads = (image) => { | ||
| const reloadedUrls = [] | ||
| const originalSetAttribute = image.setAttribute.bind(image) | ||
| image.setAttribute = (name, value) => { | ||
| if (name === 'src') reloadedUrls.push(value) | ||
| originalSetAttribute(name, value) | ||
| } | ||
| return reloadedUrls | ||
| } | ||
|
|
||
| const stubImage = ({ succeeds }) => { | ||
| class FakeImage { | ||
| get src () { | ||
| return this._src | ||
| } | ||
|
|
||
| set src (value) { | ||
| this._src = value | ||
| succeeds | ||
| ? this.onload && this.onload() | ||
| : this.onerror && this.onerror() | ||
| } | ||
| } | ||
| window.Image = FakeImage | ||
| } | ||
|
|
||
| beforeEach(function () { | ||
| originalImage = window.Image | ||
| }) | ||
|
|
||
| afterEach(function () { | ||
| window.Image = originalImage | ||
| document.querySelectorAll('img').forEach((img) => img.remove()) | ||
| }) | ||
|
|
||
| it('does nothing when fewer than two draft images are on the page', function () { | ||
| addImage('https://draft-assets.test.gov.uk/media/1/one.jpg') | ||
| stubImage({ succeeds: true }) | ||
|
|
||
| GOVUK.warmUpAssetManagerSession('https://draft-assets.test.gov.uk/media/placeholder/placeholder.jpg') | ||
|
|
||
| expect(document.images[0].src).toEqual( | ||
| 'https://draft-assets.test.gov.uk/media/1/one.jpg' | ||
| ) | ||
| }) | ||
|
|
||
| it('retries all draft images once the placeholder request succeeds', function () { | ||
| const first = addImage('https://draft-assets.test.gov.uk/media/1/one.jpg') | ||
| const second = addImage('https://draft-assets.test.gov.uk/media/2/two.jpg?foo=bar') | ||
| const firstReloads = trackReloads(first) | ||
| const secondReloads = trackReloads(second) | ||
| stubImage({ succeeds: true }) | ||
|
|
||
| GOVUK.warmUpAssetManagerSession('https://draft-assets.test.gov.uk/media/placeholder/placeholder.jpg') | ||
|
|
||
| expect(firstReloads).toEqual(['https://draft-assets.test.gov.uk/media/1/one.jpg']) | ||
| expect(secondReloads).toEqual(['https://draft-assets.test.gov.uk/media/2/two.jpg?foo=bar']) | ||
| }) | ||
|
|
||
| it('also retries all draft images when the placeholder request errors', function () { | ||
| const first = addImage('https://draft-assets.test.gov.uk/media/1/one.jpg') | ||
| addImage('https://draft-assets.test.gov.uk/media/2/two.jpg') | ||
| const firstReloads = trackReloads(first) | ||
| stubImage({ succeeds: false }) | ||
|
|
||
| GOVUK.warmUpAssetManagerSession('https://draft-assets.test.gov.uk/media/placeholder/placeholder.jpg') | ||
|
|
||
| expect(firstReloads).toEqual(['https://draft-assets.test.gov.uk/media/1/one.jpg']) | ||
| }) | ||
|
|
||
| it('ignores non-asset-manager images when counting/retrying', function () { | ||
| const other = addImage('https://static.test.gov.uk/media/1/one.jpg') | ||
| addImage('https://draft-assets.test.gov.uk/media/2/two.jpg') | ||
| stubImage({ succeeds: true }) | ||
|
|
||
| GOVUK.warmUpAssetManagerSession('https://draft-assets.test.gov.uk/media/placeholder/placeholder.jpg') | ||
|
|
||
| expect(other.src).toEqual('https://static.test.gov.uk/media/1/one.jpg') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice.