From 27b4888934c0f010e3a8b80cbba2d7fdf16fbc5b Mon Sep 17 00:00:00 2001 From: TripleU <182648541+TripleU613@users.noreply.github.com> Date: Sun, 22 Nov 2026 19:49:50 -0500 Subject: [PATCH] Categories: pages carry which types each category is, not each type's form Every page preloads all the categories it may show, and each carried the full definition of every type it is: description, and every field of the type's settings form with labels and defaults. On prod that was 108 KB of every page for 58 categories. Outside the category editor the browser only asks which types a category is and shows a type's name and icon; the editor loads the category again through find_by_slug (CategorySerializer), which keeps the full definitions. The site's list now sends each type's id, name, title and icons: 9.7 KB on prod. jtech_lean_category_types (on) switches it off again; changing it clears the cached category list. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BUGDaR7qv2d8CuVp7UGSTs --- config/locales/server.en.yml | 7 ++++ config/settings.yml | 5 +++ lib/discourse_lean_site_data.rb | 33 +++++++++++++++++ plugin.rb | 1 + spec/requests/lean_category_types_spec.rb | 45 +++++++++++++++++++++++ sub_plugins/lean_site_data.rb | 17 +++++++++ 6 files changed, 108 insertions(+) create mode 100644 lib/discourse_lean_site_data.rb create mode 100644 spec/requests/lean_category_types_spec.rb create mode 100644 sub_plugins/lean_site_data.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 1d66d958..3a2e8ef9 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -106,6 +106,13 @@ en: loads its full stylesheet as before, so nothing on screen changes; turn this off to send every colour again. + jtech_lean_category_types: Send the categories in every page with each + of their types' id, name, title and icon only, not the full definition + (description and every field of its settings form). With many + categories that was over 100 KB of every page. The category editor + loads the category again with everything, so editing is unchanged; + turn this off to send the full definitions again. + jtech_trust_level_flair: Show each member's trust level as their avatar flair when they haven't picked one, using the flair of their highest trust level group that has one (set the flair on the trust level groups diff --git a/config/settings.yml b/config/settings.yml index 8ecd9b42..4caf4271 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -44,6 +44,11 @@ jtech: jtech_lean_palettes: default: true client: false + # The categories every page preloads say which types they are, without + # each type's full definition (lib/discourse_lean_site_data.rb). + jtech_lean_category_types: + default: true + client: false # Members without a flair of their own show their trust level group's # (lib/discourse_trust_level_flair.rb). Off by default: it sets users' flair. jtech_trust_level_flair: diff --git a/lib/discourse_lean_site_data.rb b/lib/discourse_lean_site_data.rb new file mode 100644 index 00000000..0c484cbe --- /dev/null +++ b/lib/discourse_lean_site_data.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# Every page preloads all the categories it may show, and each one carried +# the full definition of every type it is (category_types): title, +# description, every field of its settings form with labels and defaults, +# about 3.8 KB per category, so 113 KB on each page with 47 categories. +# Outside the category editor the browser only asks which types a category +# is (Category#isType, a plugin's `categoryTypes.documentation`), and shows +# a type's name and icon. The editor loads the category again through +# /c/:slug/find_by_slug.json (CategorySerializer), which keeps everything. +module DiscourseLeanSiteData + # What a type keeps in the preload + TYPE_KEYS = %i[id name title icon svg_icon].freeze + + def self.enabled? + SiteSetting.jtech_enabled && SiteSetting.jtech_lean_category_types + end + + def self.lean_types(types) + return types unless types.is_a?(Hash) + types.transform_values { |type| type.is_a?(Hash) ? type.slice(*TYPE_KEYS) : type } + end + + module SiteCategorySerializerExtension + def category_types + types = super + # Only the site's own list: CategorySerializer (the editor's) and the + # admin one inherit from this and keep the full definitions + return types unless instance_of?(::SiteCategorySerializer) && DiscourseLeanSiteData.enabled? + DiscourseLeanSiteData.lean_types(types) + end + end +end diff --git a/plugin.rb b/plugin.rb index 3cae5991..6b7e9007 100644 --- a/plugin.rb +++ b/plugin.rb @@ -50,6 +50,7 @@ fair_scores ads reaction_levels + lean_site_data ].each do |sub| path = File.expand_path("sub_plugins/#{sub}.rb", __dir__) instance_eval(File.read(path), path, 1) diff --git a/spec/requests/lean_category_types_spec.rb b/spec/requests/lean_category_types_spec.rb new file mode 100644 index 00000000..1f3cfb95 --- /dev/null +++ b/spec/requests/lean_category_types_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "rails_helper" + +# The categories every page preloads say which types they are, without each +# type's full definition; the category editor still gets all of it. +RSpec.describe "Category types in the site data" do + fab!(:admin) + fab!(:category) + + before do + SiteSetting.jtech_enabled = true + SiteSetting.enable_simplified_category_creation = true + Site.clear_cache + end + + def site_types(id) + get "/site.json" + expect(response.status).to eq(200) + response.parsed_body["categories"].find { it["id"] == id }["category_types"] + end + + it "sends each type's id, name, title and icon only" do + types = site_types(category.id) + expect(types.keys).to include("discussion") + expect(types["discussion"].keys).to match_array(%w[id name title icon svg_icon]) + expect(types["discussion"]["id"]).to eq("discussion") + end + + it "still gives the category editor the full definitions" do + sign_in(admin) + get "/c/#{category.slug}/find_by_slug.json" + expect(response.status).to eq(200) + expect(response.parsed_body.dig("category", "category_types", "discussion")).to include( + "configuration_schema", + "description", + ) + end + + it "sends the full definitions when switched off" do + site_types(category.id) + SiteSetting.jtech_lean_category_types = false + expect(site_types(category.id)["discussion"]).to include("configuration_schema") + end +end diff --git a/sub_plugins/lean_site_data.rb b/sub_plugins/lean_site_data.rb new file mode 100644 index 00000000..d205ac38 --- /dev/null +++ b/sub_plugins/lean_site_data.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true +# Jtech sub-plugin: less in the data every page preloads, where the browser +# never reads it there (lib/discourse_lean_site_data.rb). Loaded by +# Jtech/plugin.rb in the Plugin::Instance context. + +require_relative "../lib/discourse_lean_site_data" + +after_initialize do + reloadable_patch do + ::SiteCategorySerializer.prepend(DiscourseLeanSiteData::SiteCategorySerializerExtension) + end + + # The site's category list is serialized once and cached + on(:site_setting_changed) do |name, _old, _new| + Site.clear_cache if name.to_s == "jtech_lean_category_types" + end +end