Skip to content

A platform for community discussion. Free, open, simple.

FIX: Point hreflang alternates at the same page of a topic (#45103)

Arpit Jalan43c8e6832a9e
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 802a2fe7da3..a7c1b4ffdef 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -223,6 +223,8 @@ class TopicsController < ApplicationController
response.headers["X-Robots-Tag"] = "noindex" unless @topic_view.topic.visible
canonical_url UrlHelper.absolute_without_cdn(@topic_view.canonical_path)
+ # The canonical can be an embedding page, which has no translated versions.
+ @hreflang_url = UrlHelper.absolute_without_cdn(@topic_view.current_page_path)
# provide hint to crawlers only for now
# we would like to give them a bit more signal about age of data
diff --git a/app/views/common/_hreflang_tags.html.erb b/app/views/common/_hreflang_tags.html.erb
index 0edda88f68c..828a0fa9b2a 100644
--- a/app/views/common/_hreflang_tags.html.erb
+++ b/app/views/common/_hreflang_tags.html.erb
@@ -1,11 +1,9 @@
<% if ContentLocalization.crawler_locale_param_enabled? %>
<% locales = SiteSetting.content_localization_supported_locales&.split('|') || [] %>
<% if locales.any? %>
- <% current_path = request.path %>
- <% canonical_url = "#{Discourse.base_url}#{current_path}" %>
- <link rel="alternate" href="<%= canonical_url %>" hreflang="x-default" />
+ <link rel="alternate" href="<%= canonical_url_for_locale(nil) %>" hreflang="x-default" />
<% locales.each do |locale| %>
- <link rel="alternate" href="<%= canonical_url %>?<%= Discourse::LOCALE_PARAM %>=<%= locale %>" hreflang="<%= locale.tr('_', '-') %>" />
+ <link rel="alternate" href="<%= canonical_url_for_locale(locale) %>" hreflang="<%= locale.tr('_', '-') %>" />
<% end %>
<% end %>
<% end %>
diff --git a/lib/canonical_url.rb b/lib/canonical_url.rb
index 377dec193cc..7ef8e7a123e 100644
--- a/lib/canonical_url.rb
+++ b/lib/canonical_url.rb
@@ -53,6 +53,15 @@ module CanonicalURL
def canonical_link_tag(url = nil)
tag("link", rel: "canonical", href: url || @canonical_url || default_canonical)
end
+
+ # Pass nil for the x-default URL.
+ def canonical_url_for_locale(locale)
+ uri = Addressable::URI.parse(@hreflang_url || @canonical_url || default_canonical)
+ query = (uri.query_values || {}).except(Discourse::LOCALE_PARAM)
+ query[Discourse::LOCALE_PARAM] = locale if locale
+ uri.query_values = query.presence
+ uri.to_s
+ end
end
end
diff --git a/spec/requests/crawler_hreflang_spec.rb b/spec/requests/crawler_hreflang_spec.rb
index 2488ba473ff..bb3017ce393 100644
--- a/spec/requests/crawler_hreflang_spec.rb
+++ b/spec/requests/crawler_hreflang_spec.rb
@@ -147,6 +147,74 @@ describe "Crawler hreflang tags" do
end
end
+ context "when viewing a later page of a topic" do
+ fab!(:replies) { Fabricate.times(25, :post, topic: post.topic, user:) }
+
+ let(:topic_url) { "#{Discourse.base_url}/t/#{post.topic.slug}/#{post.topic.id}" }
+
+ def hreflang_links
+ Nokogiri
+ .HTML5(response.body)
+ .css("link[rel='alternate'][hreflang]")
+ .map { |link| [link["hreflang"], link["href"]] }
+ end
+
+ it "points each language alternate to the same page" do
+ get "#{topic_url}?page=2&#{Discourse::LOCALE_PARAM}=ja",
+ headers: {
+ "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
+ }
+
+ expect(hreflang_links).to contain_exactly(
+ ["x-default", "#{topic_url}?page=2"],
+ ["en", "#{topic_url}?page=2&#{Discourse::LOCALE_PARAM}=en"],
+ ["ja", "#{topic_url}?page=2&#{Discourse::LOCALE_PARAM}=ja"],
+ ["es", "#{topic_url}?page=2&#{Discourse::LOCALE_PARAM}=es"],
+ )
+ end
+
+ it "points the alternates of a reply link to the page that holds the reply" do
+ get "#{topic_url}/#{replies.last.post_number}",
+ headers: {
+ "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
+ }
+
+ expect(hreflang_links).to include(
+ ["x-default", "#{topic_url}?page=2"],
+ ["ja", "#{topic_url}?page=2&#{Discourse::LOCALE_PARAM}=ja"],
+ )
+ end
+ end
+
+ it "includes the subfolder once in language alternates" do
+ set_subfolder "/forum"
+
+ get "/t/#{post.topic.slug}/#{post.topic.id}",
+ env: {
+ "SCRIPT_NAME" => "/forum",
+ },
+ headers: {
+ "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
+ }
+
+ expect(response.body).to include(
+ %(<link rel="alternate" href="#{Discourse.base_url_no_prefix}/forum/t/#{post.topic.slug}/#{post.topic.id}" hreflang="x-default" />),
+ )
+ end
+
+ it "keeps language alternates on the forum when the topic canonicalizes to an embedding page on the same host" do
+ SiteSetting.embed_set_canonical_url = true
+ topic_embed = Fabricate(:topic_embed, post:, embed_url: "#{Discourse.base_url}/blog/article")
+ topic_url = "#{Discourse.base_url}/t/#{post.topic.slug}/#{post.topic.id}"
+
+ get topic_url, headers: { "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)" }
+
+ expect(response.body).to include(
+ %(<link rel="alternate" href="#{topic_url}?#{Discourse::LOCALE_PARAM}=ja" hreflang="ja" />),
+ )
+ expect(response.body).not_to include("#{topic_embed.embed_url}?#{Discourse::LOCALE_PARAM}=")
+ end
+
it "does not append ?tl= to canonical for non-crawler requests" do
get "/t/#{post.topic.slug}/#{post.topic.id}?#{Discourse::LOCALE_PARAM}=ja"