Skip to content

Commit 7132e2f

Browse files
committed
fix(themes): don't leak custom theme css after preview (@fehmer)
1 parent 852d158 commit 7132e2f

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

frontend/__tests__/components/core/Theme.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ describe("Theme component", () => {
8585
it("removes CSS when theme has no CSS", async () => {
8686
themeSignalMock.mockImplementation(() => ({ name: "light" }) as any);
8787
const { css } = renderComponent();
88-
expect(css.getAttribute("href")).toBe("");
88+
expect(css).not.toBeInTheDocument();
8989
});
9090

9191
it("removes CSS when theme is custom", async () => {
9292
themeSignalMock.mockImplementation(() => ({ name: "custom" }) as any);
9393
const { css } = renderComponent();
94-
expect(css.getAttribute("href")).toBe("");
94+
expect(css).not.toBeInTheDocument();
9595
});
9696

9797
it("handles CSS load error", () => {

frontend/src/ts/components/core/Theme.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Link, Meta, MetaProvider, Style } from "@solidjs/meta";
2-
import { createEffect, createMemo, JSXElement } from "solid-js";
2+
import { createEffect, createMemo, JSXElement, Show } from "solid-js";
33

44
import { themes } from "../../constants/themes";
55
import { createDebouncedEffectOn } from "../../hooks/effects";
@@ -52,27 +52,39 @@ export function Theme(): JSXElement {
5252
}`);
5353
});
5454

55+
const isThemeWithCss = () => {
56+
const name = getThemeName();
57+
return name !== "custom" && (themes[name]?.hasCss ?? false);
58+
};
59+
5560
createEffect(() => {
5661
const name = getThemeName();
57-
const hasCss = name !== "custom" && (themes[name].hasCss ?? false);
62+
const hasCss = isThemeWithCss();
63+
5864
console.debug(
5965
`Theme component ${hasCss ? "loading style" : "removing style"} for theme ${name}`,
6066
);
61-
if (hasCss) showLoaderBar();
67+
if (hasCss) {
68+
showLoaderBar();
69+
} else {
70+
hideLoaderBar();
71+
}
6272
linkEl()?.setAttribute("href", hasCss ? `/themes/${name}.css` : "");
6373
});
6474

6575
return (
6676
<MetaProvider>
6777
<Style id="theme" ref={styleRef} />
68-
<Link
69-
ref={linkRef}
70-
rel="stylesheet"
71-
id="currentTheme"
72-
data-name={getTheme().name}
73-
onError={onError}
74-
onLoad={onLoad}
75-
/>
78+
<Show when={isThemeWithCss()}>
79+
<Link
80+
ref={linkRef}
81+
rel="stylesheet"
82+
id="currentTheme"
83+
data-name={getTheme().name}
84+
onError={onError}
85+
onLoad={onLoad}
86+
/>
87+
</Show>
7688
<Meta id="metaThemeColor" name="theme-color" content={getTheme().bg} />
7789
<FavIcon theme={getTheme()} />
7890
</MetaProvider>

0 commit comments

Comments
 (0)