Skip to content

Commit 36be2d2

Browse files
ChrisBAshtoneYinka
authored andcommitted
Attempt: reload all draft assets after re-establishing session
If 2 or more draft assets are in a draft document, we force a load of our placeholder asset (to guarantee there's a working Asset Manager session) then reload all of the potentially bad images.
1 parent a8f2296 commit 36be2d2

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

app/views/shared/_footer_navigation.html.erb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,38 @@
88
</div>
99
</div>
1010
<% end %>
11+
12+
<% if draft_host? %>
13+
<%
14+
host = if GovukEnvironment.current == "staging"
15+
"staging.publishing.service.gov.uk"
16+
elsif GovukEnvironment.current == "integration"
17+
"integration.publishing.service.gov.uk"
18+
else
19+
"publishing.service.gov.uk"
20+
end
21+
placeholder_asset_url = "https://draft-assets.#{host}/media/5e59279b86650c53b2cefbfe/placeholder.jpg"
22+
%>
23+
<script type="text/javascript" nonce="<%= content_security_policy_nonce %>">
24+
const setAssetManagerSession = () => {
25+
const draftAssets = [...document.images].filter((image) =>
26+
image.src.includes("assets.")
27+
);
28+
29+
if (draftAssets.length < 2) return;
30+
31+
const retryDraftAssets = () => {
32+
draftAssets.forEach((image) => {
33+
image.src = `${image.src}${image.src.includes("?") ? "&" : "?"}_asset_manager_retry=${Date.now()}`;
34+
});
35+
};
36+
37+
const placeholder = new Image();
38+
placeholder.onload = retryDraftAssets;
39+
placeholder.onerror = retryDraftAssets;
40+
placeholder.src = "<%= placeholder_asset_url %>";
41+
}
42+
43+
window.addEventListener("load", setAssetManagerSession);
44+
</script>
45+
<% end %>

0 commit comments

Comments
 (0)