A platform for community discussion. Free, open, simple.
FIX: Keep the page number in canonical URLs for category and tag lists (#45229)
Arpit Jalan7f346f62649d
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex 88297f4a897..cb84cd17f07 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -120,7 +120,7 @@ class ListController < ApplicationControllerenddefine_method("category_#{filter}") do- canonical_url "#{Discourse.base_url_no_prefix}#{@category.url}"+ canonical_list_url(@category.url)public_send(filter, category: @category.id)end@@ -149,7 +149,7 @@ class ListController < ApplicationControllerenddef category_default- canonical_url "#{Discourse.base_url_no_prefix}#{@category.url}"+ canonical_list_url(@category.url)public_send(category_default_view, category: @category.id)enddiff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rbindex a9130246b86..97922ed9979 100644--- a/app/controllers/tags_controller.rb+++ b/app/controllers/tags_controller.rb@@ -205,7 +205,12 @@ class TagsController < ::ApplicationControllercanonical_params = params.slice(:category_slug_path_with_id, :tag_slug, :tag_id)canonical_method = url_method(canonical_params)- canonical_url "#{Discourse.base_url_no_prefix}#{public_send(canonical_method, *canonical_params.values.map { |t| t.force_encoding("UTF-8") })}"+ canonical_list_url(+ public_send(+ canonical_method,+ *canonical_params.values.map { |t| t.force_encoding("UTF-8") },+ ),+ )if @list.topics.size == 0 && @tag_name != "none" && !Tag.where_name(@tag_name).exists?raise Discourse::NotFound.new("tag not found", check_permalinks: true)diff --git a/lib/topic_list_responder.rb b/lib/topic_list_responder.rbindex 5e0271eebb0..f994c03b387 100644--- a/lib/topic_list_responder.rb+++ b/lib/topic_list_responder.rb@@ -43,6 +43,13 @@ module TopicListResponderprivate+ def canonical_list_url(path)+ url = "#{Discourse.base_url_no_prefix}#{path}"+ page = params[:page]+ url = "#{url}?page=#{page.to_i}" if TopicQuery.validate?(:page, page) && page.to_i > 0+ canonical_url(url)+ end+def localize_topic_list_content(list)return if list.topics.blank? || !SiteSetting.content_localization_enabledcrawl_locale = I18n.localediff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 4d6a9599f4c..50eb4397233 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -1407,6 +1407,19 @@ RSpec.describe ListController doexpect(response.status).to eq(200)expect(css_select("link[rel=canonical]").length).to eq(1)end++ it "keeps the page number on later pages of a category" do+ category_url = "#{Discourse.base_url}/c/#{category.slug}/#{category.id}"++ get "/c/#{category.slug}/#{category.id}?page=2&order=views"+ expect(css_select("link[rel=canonical]").first["href"]).to eq("#{category_url}?page=2")++ get "/c/#{category.slug}/#{category.id}/l/latest?page=2"+ expect(css_select("link[rel=canonical]").first["href"]).to eq("#{category_url}?page=2")++ get "/c/#{category.slug}/#{category.id}?page=0"+ expect(css_select("link[rel=canonical]").first["href"]).to eq(category_url)+ endendcontext "for category default view" dodiff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rbindex 98a41ee9b3c..5261cba3895 100644--- a/spec/requests/tags_controller_spec.rb+++ b/spec/requests/tags_controller_spec.rb@@ -670,6 +670,22 @@ RSpec.describe TagsController do)end+ it "keeps the page number in the canonical URL on later pages of a tag" do+ tag_url = "#{Discourse.base_url}/tag/#{tag.slug}/#{tag.id}"++ get "/tag/#{tag.slug}/#{tag.id}?page=2&order=views"+ expect(response.body).to have_tag(+ "link",+ with: {+ rel: "canonical",+ href: "#{tag_url}?page=2",+ },+ )++ get "/tag/#{tag.slug}/#{tag.id}?page=0"+ expect(response.body).to have_tag("link", with: { rel: "canonical", href: tag_url })+ end+it "handles special tag 'none'" doSiteSetting.pm_tags_allowed_for_groups = "1|2|3"