Skip to content

Commit

Permalink
Add a canonical URL meta tag
Browse files Browse the repository at this point in the history
This is an optional meta tag which defaults to not being shown.
It's built from the base_path of the content item which means it
strips out any query parameters or fragments.
  • Loading branch information
thomasleese committed May 10, 2018
1 parent 8f9ed42 commit d1d9cfb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Add an optional `canonical` meta tag.

# 7.2.0

* Add department colours to components (PR #296)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ examples:
links:
world_locations:
- analytics_identifier: WL3
with_default_canonical_path:
data:
content_item:
base_path: /test
canonical_path: true
with_overridden_canonical_path:
data:
content_item:
base_path: /test
canonical_path: /this-is-a-test-path
15 changes: 15 additions & 0 deletions lib/govuk_publishing_components/presenters/meta_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def meta_tags
meta_tags = add_political_tags(meta_tags)
meta_tags = add_taxonomy_tags(meta_tags)
meta_tags = add_step_by_step_tags(meta_tags)
meta_tags = add_canonical_tag(meta_tags)
meta_tags
end

Expand Down Expand Up @@ -106,6 +107,20 @@ def add_step_by_step_tags(meta_tags)
meta_tags
end

def add_canonical_tag(meta_tags)
if local_assigns.key?(:canonical_path)
canonical_path = if local_assigns[:canonical_path] == true
content_item[:base_path]
else
local_assigns[:canonical_path]
end

meta_tags["canonical"] = Plek.new.website_root + canonical_path
end

meta_tags
end

def has_content_history?
(content_item[:public_updated_at] && details[:first_public_at] && content_item[:public_updated_at] != details[:first_public_at]) ||
(details[:change_history] && details[:change_history].size > 1)
Expand Down
24 changes: 24 additions & 0 deletions spec/components/meta_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ def example_document_for(schema_name, example_name)
assert_meta_tag('govuk:schema-name', 'publication')
end

it "renders the canonical URL in a meta tag if requested" do
render_component(
content_item: { base_path: "/test" },
canonical_path: true,
)

assert_meta_tag('canonical', 'http://www.dev.gov.uk/test')
end

it "renders the canonical URL in a meta tag if overridden" do
render_component(
content_item: { base_path: "/test" },
canonical_path: "/test2",
)

assert_meta_tag('canonical', 'http://www.dev.gov.uk/test2')
end

it "doesn't renders the canonical URL in a meta tag if not requested" do
assert_empty render_component(content_item: {
base_path: "/test"
}).strip
end

it "renders organisations in a meta tag with angle brackets" do
content_item = {
links: {
Expand Down

0 comments on commit d1d9cfb

Please sign in to comment.