DEV: No longer preload categories (#24950)

Categories will no longer be preloaded when `lazy_load_categories` is
enabled through PreloadStore.

Instead, the list of site categories will continue to be populated
by `Site.updateCategory` as more and more categories are being loaded
from different sources (topic lists, category selectors, etc).
This commit is contained in:
Bianca Nenciu 2023-12-28 14:36:33 +02:00 committed by GitHub
parent c4396c6acf
commit 14269232ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 17 additions and 22 deletions

View File

@ -9,7 +9,7 @@ export default {
this.site = owner.lookup("service:site");
// If the site is login_required and the user is anon there will be no categories preloaded.
if (!this.site.categories) {
if (!this.site.categories?.length) {
return;
}

View File

@ -12,7 +12,7 @@ export default {
this.site = owner.lookup("service:site");
// If the site is login_required and the user is anon there will be no categories preloaded.
if (!this.site.categories) {
if (!this.site.categories?.length) {
return;
}

View File

@ -18,7 +18,7 @@ export default {
// If the site is login_required and the user is anon there will be no categories
// preloaded, so there will be no category color CSS variables generated by
// the category-color-css-generator initializer.
if (!this.site.categories) {
if (!this.site.categories?.length) {
return;
}

View File

@ -20,6 +20,7 @@ const Site = RestModel.extend({
this._super(...arguments);
this.topicCountDesc = ["topic_count:desc"];
this.categories = this.categories || [];
},
@discourseComputed("notification_types")

View File

@ -4,9 +4,6 @@
class Site
include ActiveModel::Serialization
# Number of categories preloaded when lazy_load_categories is enabled
LAZY_LOAD_CATEGORIES_LIMIT = 10
cattr_accessor :preloaded_category_custom_fields
def self.reset_preloaded_category_custom_fields
@ -157,11 +154,7 @@ class Site
self.class.categories_callbacks.each { |callback| callback.call(categories, @guardian) }
if SiteSetting.lazy_load_categories
categories[0...Site::LAZY_LOAD_CATEGORIES_LIMIT]
else
categories
end
categories
end
end

View File

@ -238,6 +238,10 @@ class SiteSerializer < ApplicationSerializer
object.categories.map { |c| c.to_h }
end
def include_categories?
!SiteSetting.lazy_load_categories
end
def markdown_additional_options
Site.markdown_additional_options
end

View File

@ -183,17 +183,6 @@ RSpec.describe Site do
DiscoursePluginRegistry.clear_modifiers!
end
end
context "when lazy_load_categories" do
before { SiteSetting.lazy_load_categories = true }
it "limits the number of categories" do
stub_const(Site, "LAZY_LOAD_CATEGORIES_LIMIT", 1) do
categories = Site.new(Guardian.new).categories
expect(categories.size).to eq(1)
end
end
end
end
it "omits groups user can not see" do

View File

@ -131,6 +131,14 @@ RSpec.describe SiteSerializer do
expect(serialized[:shared_drafts_category_id]).to eq(nil)
end
it "does not include categories if lazy_load_categories" do
SiteSetting.lazy_load_categories = true
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
expect(serialized[:categories]).to eq(nil)
end
describe "#anonymous_default_navigation_menu_tags" do
fab!(:user)
fab!(:tag) { Fabricate(:tag, name: "dev", description: "some description") }