FEATURE: Send a 'noindex' header in non-canonical responses (#15026)

* FEATURE: Optionally send a 'noindex' header in non-canonical responses

This will be used in a SEO experiment.

Co-authored-by: David Taylor <david@taylorhq.com>
This commit is contained in:
Rafael dos Santos Silva
2021-11-25 16:58:39 -03:00
committed by GitHub
parent 1166afa4e8
commit 5647819de4
5 changed files with 56 additions and 17 deletions

View File

@ -2,6 +2,8 @@
module CanonicalURL
module ControllerExtensions
ALLOWED_CANONICAL_PARAMS = %w(page)
def canonical_url(url_for_options = {})
case url_for_options
when Hash
@ -10,22 +12,27 @@ module CanonicalURL
@canonical_url = url_for_options
end
end
def default_canonical
@default_canonical ||= begin
canonical = +"#{Discourse.base_url_no_prefix}#{request.path}"
allowed_params = params.select { |key| ALLOWED_CANONICAL_PARAMS.include?(key) }
if allowed_params.present?
canonical << "?#{allowed_params.keys.zip(allowed_params.values).map { |key, value| "#{key}=#{value}" }.join("&")}"
end
canonical
end
end
def self.included(base)
base.helper_method :default_canonical
end
end
module Helpers
ALLOWED_CANONICAL_PARAMS = %w(page)
def canonical_link_tag(url = nil)
tag('link', rel: 'canonical', href: url || @canonical_url || default_canonical)
end
def default_canonical
canonical = +"#{Discourse.base_url_no_prefix}#{request.path}"
allowed_params = params.select { |key| ALLOWED_CANONICAL_PARAMS.include?(key) }
if allowed_params.present?
canonical << "?#{allowed_params.keys.zip(allowed_params.values).map { |key, value| "#{key}=#{value}" }.join("&")}"
end
canonical
end
end
end