Skip to content

Commit

Permalink
Merge pull request #898 from alphagov/canonical-meta-tag
Browse files Browse the repository at this point in the history
Canonical meta tag
  • Loading branch information
thomasleese authored May 11, 2018
2 parents 43641cc + 8f2992d commit 03eb0f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
<meta name="description" content="<%= strip_tags(@content_item.description) %>" />
<% end %>

<link rel="canonical" href="<%= @content_item.canonical_url %>" />

<meta property="og:site_name" content="GOV.UK" />
<meta property="og:type" content="article" />
<meta property="og:url" content="<%= @content_item.web_url %>" />
<meta property="og:url" content="<%= @content_item.canonical_url %>" />
<meta property="og:title" content="<%= @content_item.page_title %>" />
<meta property="og:description" content="<%= strip_tags(@content_item.description) %>" />

Expand Down
12 changes: 12 additions & 0 deletions test/presenters/content_item_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 03eb0f8

Please sign in to comment.