Skip to content

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.rb
index 88297f4a897..cb84cd17f07 100644
--- a/app/controllers/list_controller.rb
+++ b/app/controllers/list_controller.rb
@@ -120,7 +120,7 @@ class ListController < ApplicationController
end
define_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 < ApplicationController
end
def category_default
- canonical_url "#{Discourse.base_url_no_prefix}#{@category.url}"
+ canonical_list_url(@category.url)
public_send(category_default_view, category: @category.id)
end
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index a9130246b86..97922ed9979 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -205,7 +205,12 @@ class TagsController < ::ApplicationController
canonical_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.rb
index 5e0271eebb0..f994c03b387 100644
--- a/lib/topic_list_responder.rb
+++ b/lib/topic_list_responder.rb
@@ -43,6 +43,13 @@ module TopicListResponder
private
+ 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_enabled
crawl_locale = I18n.locale
diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb
index 4d6a9599f4c..50eb4397233 100644
--- a/spec/requests/list_controller_spec.rb
+++ b/spec/requests/list_controller_spec.rb
@@ -1407,6 +1407,19 @@ RSpec.describe ListController do
expect(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)
+ end
end
context "for category default view" do
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index 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'" do
SiteSetting.pm_tags_allowed_for_groups = "1|2|3"