From b0a98ac49ca138e6274dbc16dc99365a2f639929 Mon Sep 17 00:00:00 2001 From: RedwindA <128586631+RedwindA@users.noreply.github.com> Date: Sat, 17 Oct 2026 14:03:48 +0800 Subject: [PATCH] fix(web): apply theme class before first paint so Dark Reader detects dark mode (#7571) Dark Reader checks html.dark / color-scheme only once during page load, but the theme class was added by ThemeProvider after React mounted. Resolve the saved theme in an inline head script and declare color-scheme: dark on .dark. --- web/index.html | 17 ++++ .../__tests__/theme-boot-script.test.ts | 95 +++++++++++++++++++ web/src/styles/theme.css | 3 + 3 files changed, 115 insertions(+) create mode 100644 web/src/context/__tests__/theme-boot-script.test.ts diff --git a/web/index.html b/web/index.html index d097f46fd4..b91e7b6b5e 100644 --- a/web/index.html +++ b/web/index.html @@ -14,6 +14,23 @@ /> + + diff --git a/web/src/context/__tests__/theme-boot-script.test.ts b/web/src/context/__tests__/theme-boot-script.test.ts new file mode 100644 index 0000000000..88361668b7 --- /dev/null +++ b/web/src/context/__tests__/theme-boot-script.test.ts @@ -0,0 +1,95 @@ +/* +Copyright (C) 2023-2026 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ +import { readFileSync } from 'node:fs' +import { resolve } from 'node:path' + +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { THEME_STORAGE_KEYS } from '@/lib/theme-storage' + +// The inline script in index.html must apply the theme class before React +// mounts; Dark Reader checks `html.dark` only once while the page loads. +const html = readFileSync( + resolve(import.meta.dirname, '../../../index.html'), + 'utf8' +) +const bootScript = html.match(/