A platform for community discussion. Free, open, simple.
PERF: Skip the categories topic list for crawlers (#42881)
Régis Hanole5aca7217abb
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex dc8c639fa0e..353f53db305 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -65,14 +65,7 @@ class CategoriesController < ApplicationControllerMultiJson.dump(CategoryListSerializer.new(@category_list, scope: guardian)),)- @topic_list = fetch_topic_list-- if @topic_list.present? && @topic_list.topics.present?- store_preloaded(- @topic_list.preload_key,- MultiJson.dump(TopicListSerializer.new(@topic_list, scope: guardian)),- )- end+ preload_topic_listrenderend@@ -884,6 +877,20 @@ class CategoriesController < ApplicationController@category_list = CategoryList.new(guardian, category_options)end+ # The crawler layout renders categories only and emits no preloaded data, so+ # building and serializing the list would be pure waste.+ def preload_topic_list+ return if use_crawler_layout?++ @topic_list = fetch_topic_list+ return if @topic_list.blank? || @topic_list.topics.blank?++ store_preloaded(+ @topic_list.preload_key,+ MultiJson.dump(TopicListSerializer.new(@topic_list, scope: guardian)),+ )+ end+def fetch_topic_list(topics_filter: nil)style =if topics_filterdiff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rbindex 1b413e75318..4bf436623ff 100644--- a/spec/requests/categories_controller_spec.rb+++ b/spec/requests/categories_controller_spec.rb@@ -25,6 +25,18 @@ RSpec.describe CategoriesController doendend+ it "does not build a topic list for crawlers" do+ SiteSetting.categories_topics = 5+ SiteSetting.categories_topics.times { Fabricate(:topic) }++ CategoriesController.any_instance.expects(:fetch_topic_list).never++ get "/categories", headers: { "HTTP_USER_AGENT" => "Googlebot" }++ expect(response.status).to eq(200)+ expect(response.body).to have_tag("body.crawler")+ end+it "Shows correct title if category list is set for homepage" doSiteSetting.top_menu = "categories|latest"get "/"