Skip to content

Commit

Permalink
feat: skip canonical on noindex pages
Browse files Browse the repository at this point in the history
  • Loading branch information
salzig committed Jul 3, 2020
1 parent 038c3d3 commit e32562b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ Canonical link element tells a search engine what is the canonical or main URL
for a content which have multiple URLs. The search engine will always return
that URL, and link popularity and authority will be applied to that URL.

Note: If you like follow a hint of John Mueller that you shouldn't mix canonical with noindex, then you can
set `MetaTags.config.skip_canonical_links_on_noindex = true` and we'll handle it for you.

```ruby
set_meta_tags canonical: "http://yoursite.com/canonical/url"
# <link rel="canonical" href="http://yoursite.com/canonical/url">
Expand Down
7 changes: 7 additions & 0 deletions lib/meta_tags/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class Configuration
# - an array of strings or symbols representing their names or name-prefixes.
attr_reader :property_tags

# Configure whenever Meta-Tags should skip canonicals on pages with noindex: true
# "shouldn't mix noindex & rel=canonical comes from: they're very contradictory pieces of information for us."
# - John Mueller (Webmaster Trends Analyst at Google)
# https://www.reddit.com/r/TechSEO/comments/8yahdr/2_questions_about_the_canonical_tag/e2dey9i/
attr_accessor :skip_canonical_links_on_noindex

# Initializes a new instance of Configuration class.
def initialize
reset_defaults!
Expand Down Expand Up @@ -77,6 +83,7 @@ def reset_defaults!
@property_tags = default_property_tags.dup
@open_meta_tags = true
@minify_output = false
@skip_canonical_links_on_noindex = false
end
end
end
16 changes: 15 additions & 1 deletion lib/meta_tags/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def render(view)
render_with_normalization(tags, :description)
render_with_normalization(tags, :keywords)
render_refresh(tags)
render_canonical_link(tags)
render_noindex(tags)
render_alternate(tags)
render_open_search(tags)
Expand Down Expand Up @@ -150,7 +151,7 @@ def render_open_search(tags)
# @param [Array<Tag>] tags a buffer object to store tag in.
#
def render_links(tags)
[ :amphtml, :canonical, :prev, :next, :image_src, :manifest ].each do |tag_name|
[ :amphtml, :prev, :next, :image_src, :manifest ].each do |tag_name|
href = meta_tags.extract(tag_name)
if href.present?
@normalized_meta_tags[tag_name] = href
Expand All @@ -159,6 +160,19 @@ def render_links(tags)
end
end

# Renders canonical link
#
# @param [Array<Tag>] tags a buffer object to store tag in.
#
def render_canonical_link(tags)
href = meta_tags.extract(:canonical) # extract, so its not used anywhere else
return if MetaTags.config.skip_canonical_links_on_noindex && meta_tags[:noindex]
return unless href.present?

@normalized_meta_tags[:canonical] = href
tags << Tag.new(:link, rel: :canonical, href: href)
end

# Renders complex hash objects.
#
# @param [Array<Tag>] tags a buffer object to store tag in.
Expand Down
31 changes: 31 additions & 0 deletions spec/view_helper/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@
expect(meta).to have_tag('link', with: { href: "http://example.com/base/url", rel: "canonical" })
end
end

it 'does display canonical url when page is marked as noindex per default' do
subject.set_meta_tags(canonical: 'http://example.com/base/url', noindex: true)
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to have_tag('link', with: { href: "http://example.com/base/url", rel: "canonical" })
end
end

describe 'with config.skip_canonical_links_on_noindex is set' do
around do |example|
default = MetaTags.config.skip_canonical_links_on_noindex
MetaTags.config.skip_canonical_links_on_noindex = true
example.run
MetaTags.config.skip_canonical_links_on_noindex = default
end

it 'does display canonical url when page is marked as index' do
subject.set_meta_tags(canonical: 'http://example.com/base/url', index: true)
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to have_tag('link', with: { href: "http://example.com/base/url", rel: "canonical" })
end
end

it 'does not display canonical url when page is marked as noindex' do
subject.set_meta_tags(canonical: 'http://example.com/base/url', noindex: true)
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).not_to have_tag('link', with: { href: "http://example.com/base/url", rel: "canonical" })
expect(meta).not_to have_tag('meta', with: { content: "http://example.com/base/url", name: "canonical" })
end
end
end
end

describe 'displaying alternate url' do
Expand Down

0 comments on commit e32562b

Please sign in to comment.