Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions lib/discourse_lean_site_data.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions spec/requests/lean_category_types_spec.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions sub_plugins/lean_site_data.rb
Original file line number Diff line number Diff line change
@@ -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"
Comment on lines +14 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear the cache when toggling the master switch

When an admin changes jtech_enabled, this handler does not invalidate the serialized category cache: enabling the plugin reaches the callback but fails this name check, while disabling it prevents the plugin-gated on callback from running at all. Consequently, a populated cache can continue serving either full or stripped type definitions for up to its 30-minute lifetime, so the master switch does not take effect promptly. Use an ungated enabled-change handler and clear Site's cache for both switches.

AGENTS.md reference: AGENTS.md:L21-L21

Useful? React with 👍 / 👎.

end
end
Loading