Skip to content

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

FIX: Keep the locale param on topic redirects and pagination link tags (#45326)

Arpit Jalan3124b154d3e4
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index a7c1b4ffdef..600bd9b5b41 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -197,7 +197,11 @@ class TopicsController < ApplicationController
url = @topic_view.topic.relative_url
url += ".json" if request.format.json?
url += ".md" if request.format.md?
- url += "?page=#{last_page}" if last_page > 1
+ locale = params[Discourse::LOCALE_PARAM]
+ query = {}
+ query[:page] = last_page if last_page > 1
+ query[Discourse::LOCALE_PARAM] = locale if locale.present?
+ url += "?#{query.to_query}" if query.present?
return redirect_to url, status: :moved_permanently
end
end
@@ -1479,9 +1483,9 @@ class TopicsController < ApplicationController
opts =
params.slice(
- *%i[page print filter_top_level_replies preview_theme_id include_raw].concat(
- additional_allowed_query_parameters,
- ),
+ *%i[page print filter_top_level_replies preview_theme_id include_raw],
+ Discourse::LOCALE_PARAM,
+ *additional_allowed_query_parameters,
)
opts.delete(:page) if params[:page] == 0
diff --git a/lib/middleware/crawler_hooks.rb b/lib/middleware/crawler_hooks.rb
index f9088eeb3dc..abbe7eff925 100644
--- a/lib/middleware/crawler_hooks.rb
+++ b/lib/middleware/crawler_hooks.rb
@@ -3,6 +3,8 @@
module Middleware
class CrawlerHooks
NON_LOCALIZABLE_PATH_PREFIXES = %w[/uploads/ /secure-uploads/ /secure-media-uploads/].freeze
+ # Anchors, plus the pagination link tags, which crawlers follow like anchors.
+ LOCALIZABLE_LINK_TAGS = %w[a link[rel='prev'] link[rel='next']].freeze
def initialize(app)
@app = app
@@ -35,7 +37,7 @@ module Middleware
html_fragment = Nokogiri::HTML5.parse(response.body)
html_fragment
- .css("a[href^='/'], a[href^='#{Discourse.base_url}']")
+ .css(localizable_link_selector)
.each do |link|
uri = Addressable::URI.parse(link["href"])
next if non_localizable_path?(uri.path)
@@ -50,6 +52,13 @@ module Middleware
response
end
+ def localizable_link_selector
+ LOCALIZABLE_LINK_TAGS
+ .product(["/", Discourse.base_url])
+ .map { |tag, prefix| "#{tag}[href^='#{prefix}']" }
+ .join(", ")
+ end
+
def non_localizable_path?(path)
return false if path.blank?
NON_LOCALIZABLE_PATH_PREFIXES.any? { |prefix| path.start_with?(prefix) }
diff --git a/spec/lib/middleware/crawler_hooks_spec.rb b/spec/lib/middleware/crawler_hooks_spec.rb
index 1c6c0fe0afa..3a9f13e44fa 100644
--- a/spec/lib/middleware/crawler_hooks_spec.rb
+++ b/spec/lib/middleware/crawler_hooks_spec.rb
@@ -418,6 +418,52 @@ describe Middleware::CrawlerHooks do
expect(transformed).to include("href=\"/secure-media-uploads/original/1X/ghi.pdf\"")
end
+ it "appends the locale param to pagination link tags but not to canonical or alternate ones" do
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.content_localization_crawler_param = true
+
+ html = [
+ "<html><head>" \
+ "<link rel=\"prev\" href=\"/t/topic-slug/123\">" \
+ "<link rel=\"next\" href=\"#{Discourse.base_url}/t/topic-slug/123?page=3\">" \
+ "<link rel=\"canonical\" href=\"/t/topic-slug/123?page=2\">" \
+ "<link rel=\"alternate\" href=\"/t/topic-slug/123?page=2\" hreflang=\"x-default\">" \
+ "</head><body></body></html>",
+ ]
+ def html.body
+ join("")
+ end
+
+ test_env =
+ Rack::MockRequest.env_for(
+ "https://discourse.site/t/topic-slug/123?page=2",
+ params: {
+ Discourse::LOCALE_PARAM => "fr",
+ },
+ )
+ request = Rack::Request.new(test_env)
+
+ middleware_instance =
+ Middleware::CrawlerHooks.new(
+ lambda { |_| [200, { "X-Discourse-Crawler-View" => "true" }, []] },
+ )
+
+ transformed_response =
+ middleware_instance.send(:transform_response, request: request, response: html)
+
+ links =
+ Nokogiri::HTML5
+ .parse(transformed_response.first)
+ .css("link")
+ .map { |link| [link["rel"], link["href"]] }
+ expect(links).to contain_exactly(
+ ["prev", "/t/topic-slug/123?#{Discourse::LOCALE_PARAM}=fr"],
+ ["next", "#{Discourse.base_url}/t/topic-slug/123?page=3&#{Discourse::LOCALE_PARAM}=fr"],
+ %w[canonical /t/topic-slug/123?page=2],
+ %w[alternate /t/topic-slug/123?page=2],
+ )
+ end
+
it "modifies external links that start with the base URL" do
SiteSetting.content_localization_enabled = true
SiteSetting.content_localization_crawler_param = true
diff --git a/spec/requests/crawler_hreflang_spec.rb b/spec/requests/crawler_hreflang_spec.rb
index df76e6a0d9c..2c150d8270f 100644
--- a/spec/requests/crawler_hreflang_spec.rb
+++ b/spec/requests/crawler_hreflang_spec.rb
@@ -324,6 +324,45 @@ describe "Crawler hreflang tags" do
)
end
+ it "keeps the locale param on the pagination link tags" do
+ topic_path = post.topic.relative_url
+ pagination_links = -> do
+ Nokogiri
+ .HTML5(response.body)
+ .css("link[rel='prev'], link[rel='next']")
+ .map { |link| [link["rel"], link["href"]] }
+ end
+
+ get "#{topic_url}?#{Discourse::LOCALE_PARAM}=ja",
+ headers: {
+ "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
+ }
+
+ expect(pagination_links.call).to eq(
+ [["next", "#{topic_path}?page=2&#{Discourse::LOCALE_PARAM}=ja"]],
+ )
+
+ get "#{topic_url}?page=2&#{Discourse::LOCALE_PARAM}=ja",
+ headers: {
+ "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
+ }
+
+ expect(pagination_links.call).to eq(
+ [["prev", "#{topic_path}?#{Discourse::LOCALE_PARAM}=ja"]],
+ )
+
+ get "/latest?#{Discourse::LOCALE_PARAM}=ja",
+ headers: {
+ "User-Agent" => "Googlebot/2.1 (+http://www.google.com/bot.html)",
+ }
+
+ next_link = Nokogiri.HTML5(response.body).at_css("link[rel='next']")
+ expect(Addressable::URI.parse(next_link["href"]).query_values).to include(
+ "page" => "1",
+ Discourse::LOCALE_PARAM => "ja",
+ )
+ 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: {
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index aa3d74e8e6f..7a97d16ab85 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -3601,6 +3601,26 @@ RSpec.describe TopicsController do
expect(response).to redirect_to("/t/#{topic_with_posts.slug}/#{topic_with_posts.id}?page=2")
end
+ it "keeps the locale param when redirecting an over-range page" do
+ topic_with_posts = Fabricate(:topic)
+ Fabricate.times(25, :post, topic: topic_with_posts)
+ Topic.reset_highest(topic_with_posts.id)
+
+ get "/t/#{topic_with_posts.slug}/#{topic_with_posts.id}",
+ params: {
+ :page => 5,
+ Discourse::LOCALE_PARAM => "ja",
+ }
+
+ expect(response).to redirect_to(
+ "/t/#{topic_with_posts.slug}/#{topic_with_posts.id}?page=2&#{Discourse::LOCALE_PARAM}=ja",
+ )
+
+ get "/t/#{topic.slug}/#{topic.id}", params: { :page => 2, Discourse::LOCALE_PARAM => "ja" }
+
+ expect(response).to redirect_to("/t/#{topic.slug}/#{topic.id}?#{Discourse::LOCALE_PARAM}=ja")
+ end
+
it "uses viewer-visible post count when deciding the last valid page (whispers)" do
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
@@ -3924,6 +3944,16 @@ RSpec.describe TopicsController do
end
end
+ it "keeps the locale param when redirecting to the correct slug" do
+ get "/t/#{topic.slug}", params: { Discourse::LOCALE_PARAM => "ja" }
+
+ expect(response).to redirect_to("#{topic.relative_url}?#{Discourse::LOCALE_PARAM}=ja")
+
+ get "/t/wrong-slug/#{topic.id}", params: { Discourse::LOCALE_PARAM => "ja" }
+
+ expect(response).to redirect_to("#{topic.relative_url}?#{Discourse::LOCALE_PARAM}=ja")
+ end
+
context "with registered redirect_to_correct_topic_additional_query_parameters" do
let(:modifier_block) { Proc.new { |allowed_params| allowed_params << :silly_param } }
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index a5d97d31d56..00d81b42efb 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -266,7 +266,7 @@ describe "Content Localization" do
expect(sidebar).to have_section_link("戦略")
SiteSetting.set_locale_from_cookie = false
- page.refresh
+ topic_page.visit_topic(topic)
modal = topic_page.open_content_language_preferences
expect(modal).to have_read_only_interface_language("English")
end