Skip to content

Commit 481175f

Browse files
committed
DEV: Add backward compatibility for tag object arrays
What is the problem? Discourse core PR #36678 changes `site.top_tags` and `site.category_top_tags` from string arrays (e.g., `["support", "bug"]`) to object arrays (e.g., `[{id: 12, name: "support", slug: "support"}]`). This breaks the `PopularTags` component which filters and displays tags as strings. What is the solution? Normalize the tags array by mapping and extracting names using `typeof tag === "string" ? tag : tag.name` before filtering against excluded tags. This ensures backward compatibility with both the old string format and the new object format.
1 parent f3e1845 commit 481175f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

javascripts/discourse/components/popular-tags.gjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ export default class PopularTags extends Component {
2424
[];
2525
const category = currentRoute.attributes?.category;
2626

27+
const normalizedTags = tags.map((tag) =>
28+
typeof tag === "string" ? tag : tag.name
29+
);
2730
if (excludedTags.length !== 0) {
28-
this.topTags = tags
31+
this.topTags = normalizedTags
2932
.filter((tag) => {
3033
return excludedTags.indexOf(tag) === -1;
3134
})
3235
.slice(0, count);
3336
} else {
34-
this.topTags = (tags || []).slice(0, count);
37+
this.topTags = normalizedTags.slice(0, count);
3538
}
3639

3740
if (this.topTags.length === 0) {

spec/system/popular_tags_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Popular tags sidebar block", type: :system do
4+
fab!(:tag) { Fabricate(:tag, name: "support") }
5+
fab!(:topic) { Fabricate(:topic, tags: [tag]) }
6+
fab!(:post) { Fabricate(:post, topic:) }
7+
fab!(:user)
8+
9+
let!(:theme) { upload_theme_component }
10+
11+
before do
12+
SiteSetting.tagging_enabled = true
13+
sign_in(user)
14+
end
15+
16+
it "loads theme without errors" do
17+
visit("/")
18+
expect(page).to have_no_css(".broken-theme-alert")
19+
end
20+
end

0 commit comments

Comments
 (0)