FIX: Error in CSS generation for anon on login_required (#20978)

Followup to 6ad9e4ad06,
I was not aware that `site.categories` is undefined if
the user is anon and the site is login_required, this
handles that scenario and does not continue trying to
generate CSS.
This commit is contained in:
Martin Brennan 2023-04-05 19:08:44 +10:00 committed by GitHub
parent 171e8b679d
commit c63783cf30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 2 deletions

View File

@ -12,6 +12,11 @@ export default {
initialize(container) {
this.site = container.lookup("service:site");
// If the site is login_required and the user is anon there will be no categories preloaded.
if (!this.site.categories) {
return;
}
const generatedCssVariables = [
":root {",
...this.site.categories.map(

View File

@ -14,6 +14,15 @@ export default {
* ones in core are CategoryHashtagType and TagHashtagType.
*/
initialize(container) {
this.site = container.lookup("service:site");
// 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) {
return;
}
let generatedCssClasses = [];
Object.values(getHashtagTypeClasses()).forEach((hashtagTypeClass) => {

View File

@ -9,7 +9,7 @@ export default class CategoryHashtagType extends HashtagTypeBase {
}
get preloadedData() {
return this.site.categories;
return this.site.categories || [];
}
generateColorCssClasses(model) {

View File

@ -1,4 +1,4 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { acceptance, exists } from "discourse/tests/helpers/qunit-helpers";
import { visit } from "@ember/test-helpers";
import { test } from "qunit";
@ -30,3 +30,19 @@ acceptance("CSS Generator", function (needs) {
);
});
});
acceptance(
"CSS Generator | Anon user in login_required site",
function (needs) {
needs.site({ categories: null });
needs.settings({ login_required: true });
test("category CSS variables are not generated", async function (assert) {
await visit("/");
const cssTag = document.querySelector(
"style#category-color-css-generator"
);
assert.notOk(exists(cssTag));
});
}
);