From 052a2a51d2b5d912f32c1d62c016109c673aa40b Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 11 May 2018 08:46:47 +0100 Subject: [PATCH 1/3] Add canonical URL to content items This generates a canonical URL for content items based on its web_url combined with a part slug if necessary. --- app/presenters/content_item_presenter.rb | 8 ++++++++ test/presenters/content_item_presenter_test.rb | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/presenters/content_item_presenter.rb b/app/presenters/content_item_presenter.rb index dac8a18cf..216c62b3a 100644 --- a/app/presenters/content_item_presenter.rb +++ b/app/presenters/content_item_presenter.rb @@ -53,6 +53,14 @@ def web_url Plek.current.website_root + content_item["base_path"] end + def canonical_url + if requesting_a_part? + web_url + "/" + part_slug + else + web_url + end + end + private def display_date(timestamp, format = "%-d %B %Y") diff --git a/test/presenters/content_item_presenter_test.rb b/test/presenters/content_item_presenter_test.rb index 68835adfb..b5ed60b20 100644 --- a/test/presenters/content_item_presenter_test.rb +++ b/test/presenters/content_item_presenter_test.rb @@ -21,6 +21,18 @@ class ContentItemPresenterTest < ActiveSupport::TestCase assert_equal "Type", ContentItemPresenter.new("document_type" => "Type").document_type end + test "#canonical_url without a part" do + assert_equal "https://www.test.gov.uk/test", ContentItemPresenter.new("base_path" => "/test").canonical_url + end + + test "#canonical_url with a part" do + example_with_parts = govuk_content_schema_example('travel_advice', 'full-country') + request_path = example_with_parts['base_path'] + '/safety-and-security' + presented_example = TravelAdvicePresenter.new(example_with_parts, request_path) + + assert_equal "https://www.test.gov.uk/foreign-travel-advice/albania/safety-and-security", presented_example.canonical_url + end + test "available_translations sorts languages by locale with English first" do translated = govuk_content_schema_example('case_study', 'translated') locales = ContentItemPresenter.new(translated).available_translations From 95d5569f2217a6ba0056b0fba444c52227ecaa74 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 11 May 2018 08:48:35 +0100 Subject: [PATCH 2/3] Present canonical URL in a link tag --- app/views/layouts/application.html.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1fe8056fe..8ff190fc6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -25,6 +25,8 @@ <% end %> + + From 8f2992d5fe3daddef75d7febe446382539c1b35b Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 11 May 2018 08:48:45 +0100 Subject: [PATCH 3/3] Use canonical URL in opengraph tags instead of web URL This ensures that a content item with parts will have the correct URL. --- app/views/layouts/application.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 8ff190fc6..9ae1711ac 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -29,7 +29,7 @@ - +