Skip to content

Commit 75a57a0

Browse files
committed
Auto-append a cachebust URL to preview links
On the "preview" links we surface on the document summary screen, we want to append a `?cachebust={timestamp}` parameter, where `timestamp` is the current time in seconds (unix epoch time) I have auto-appended a cachebust URL query parameter to: - Normal document preview - Translation preview We get some confusing support tickets where multiple publishers coalescee around the same document. A lot of the time, the underlying reason for the support ticket is caching. For example, two publishers previewing a document and seeing different things, because one of them had an older version cached already. Adding a cachebust string will force a fresh preview everytime the publisher clicks on the preview link, which should minimise the number of support tickets coming our way.
1 parent 0f5896d commit 75a57a0

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

app/components/admin/editions/show/preview_component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
<% if versioning_completed %>
1111
<p class="govuk-body">
12-
<%= preview_link(primary_locale_link_text, local_edition.public_url(draft: true)) %>
12+
<%= preview_link(primary_locale_link_text, local_edition.public_url({ draft: true }.merge(cachebust_url_options))) %>
1313
</p>
1414

1515
<% if available_in_multiple_languages %>
1616
<% translation_preview_links = local_edition.non_primary_translation_locales.map do |locale|
1717
preview_link(
1818
"Preview on website - #{locale.native_and_english_language_name} (opens in new tab)",
19-
local_edition.public_url(locale: locale.code, draft: true),
19+
local_edition.public_url({ locale: locale.code, draft: true }.merge(cachebust_url_options)),
2020
)
2121
end %>
2222
<%= render "govuk_publishing_components/components/details", {

features/step_definitions/edition_update_slug_steps.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Then(/^I can see the preview URL of the publication "([^"]*)" contains "([^"]*)"$/) do |title, new_slug|
22
visit admin_edition_path(Publication.latest_edition.find_by!(title:))
3-
expect(page).to have_link "Preview on website (opens in new tab)", href: "https://draft-origin.test.gov.uk/government/publications/#{new_slug}"
3+
expect(page).to have_link "Preview on website (opens in new tab)", href: %r{\Ahttps://draft-origin\.test\.gov\.uk/government/publications/#{Regexp.escape(new_slug)}\?cachebust=\d+\z}
44
end
55

66
Then(/^I cannot see the option to keep the current page URL$/) do
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Then(/^I should see a link to the preview version of the publication "([^"]*)"$/) do |publication_title|
22
publication = Publication.find_by!(title: publication_title)
33
visit admin_edition_path(publication)
4-
expected_preview_url = "https://draft-origin.test.gov.uk/government/publications/#{publication.slug}"
4+
expected_preview_url = "%r{\Ahttps://draft-origin\.test\.gov\.uk/government/publications/#{Regexp.escape(publication.slug)}\?cachebust=\d+\z}"
55

66
expect(expected_preview_url).to eq(find("a[target='_blank']")[:href])
77
end

test/components/admin/editions/show/preview_component_test.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,28 @@ class Admin::Editions::Show::PreviewComponentTest < ViewComponent::TestCase
2323

2424
test "renders a link with tracking to preview the document when the edition is english only" do
2525
edition = build_stubbed(:publication, document: @document)
26+
Admin::Editions::Show::PreviewComponent.any_instance.stubs(:cachebust_url_options).returns(cachebust: 123)
2627

2728
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
28-
assert_selector "a[href='#{edition.public_url(draft: true)}']", text: "Preview on website (opens in new tab)"
29+
assert_selector "a[href='#{edition.public_url(draft: true, cachebust: 123)}']", text: "Preview on website (opens in new tab)"
2930
end
3031

3132
test "renders a link with tracking to preview the document when the edition is a foreign language only edition" do
3233
edition = build_stubbed(:publication, document: @document, primary_locale: :fr)
34+
Admin::Editions::Show::PreviewComponent.any_instance.stubs(:cachebust_url_options).returns(cachebust: 123)
3335

3436
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
35-
assert_selector "a[href='#{edition.public_url(draft: true)}']", text: "Preview on website (opens in new tab)"
37+
assert_selector "a[href='#{edition.public_url(draft: true, cachebust: 123)}']", text: "Preview on website (opens in new tab)"
3638
end
3739

3840
test "renders a link with tracking to preview the document for each translation when there are multiple translations" do
3941
edition = create(:publication, translated_into: %i[fr es], document: @document)
42+
Admin::Editions::Show::PreviewComponent.any_instance.stubs(:cachebust_url_options).returns(cachebust: 123)
4043

4144
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
42-
assert_selector "a[href='#{edition.public_url(draft: true)}']", text: "Preview on website - English (opens in new tab)"
43-
assert_selector "a[href='#{edition.public_url(locale: 'fr', draft: true)}']", visible: false, text: "Preview on website - French (Français) (opens in new tab)"
44-
assert_selector "a[href='#{edition.public_url(locale: 'es', draft: true)}']", visible: false, text: "Preview on website - Spanish (Español) (opens in new tab)"
45+
assert_selector "a[href='#{edition.public_url(draft: true, cachebust: 123)}']", text: "Preview on website - English (opens in new tab)"
46+
assert_selector "a[href='#{edition.public_url(locale: 'fr', draft: true, cachebust: 123)}']", visible: false, text: "Preview on website - French (Français) (opens in new tab)"
47+
assert_selector "a[href='#{edition.public_url(locale: 'es', draft: true, cachebust: 123)}']", visible: false, text: "Preview on website - Spanish (Español) (opens in new tab)"
4548
end
4649

4750
test "renders sharable preview functionality when edition is a pre-publication state" do

0 commit comments

Comments
 (0)