diff --git a/public/theme-init.js b/public/theme-init.js deleted file mode 100644 index d4dedbe..0000000 --- a/public/theme-init.js +++ /dev/null @@ -1,38 +0,0 @@ -// 主题初始化脚本 - 立即执行,避免主题闪烁 -(function () { - try { - // 应用主题函数 - function applyTheme(themeId, css) { - const html = document.documentElement; - - // 移除所有主题属性 - html.removeAttribute('data-theme'); - - // 应用主题 - if (themeId !== 'default') { - html.setAttribute('data-theme', themeId); - } - - // 应用自定义CSS - if (css) { - let customStyleEl = document.getElementById('custom-theme-css'); - if (!customStyleEl) { - customStyleEl = document.createElement('style'); - customStyleEl.id = 'custom-theme-css'; - document.head.appendChild(customStyleEl); - } - customStyleEl.textContent = css; - } - } - - // 应用默认主题避免闪烁,等待GlobalThemeLoader加载全站配置 - applyTheme('default', ''); - console.log('主题初始化完成,等待加载全站配置'); - - // 注意:GlobalThemeLoader会在React组件挂载后加载并应用全站主题配置 - } catch (error) { - console.error('主题初始化失败:', error); - } -})(); - - diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 9f72b19..9b453e9 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -6,6 +6,7 @@ import { Inter } from 'next/font/google'; import './globals.css'; import { getConfig } from '@/lib/config'; +import { db } from '@/lib/db'; import { GlobalErrorIndicator } from '../components/GlobalErrorIndicator'; import { SiteProvider } from '../components/SiteProvider'; @@ -43,6 +44,24 @@ export default async function RootLayout({ }) { const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage'; + // 预先获取主题配置 + let themeConfig = { + defaultTheme: 'default', + customCSS: '', + allowUserCustomization: true + }; + + try { + if (storageType !== 'localstorage') { + const adminConfig = await db.getAdminConfig(); + if (adminConfig?.ThemeConfig) { + themeConfig = adminConfig.ThemeConfig; + } + } + } catch (error) { + console.error('服务端获取主题配置失败:', error); + } + let siteName = process.env.NEXT_PUBLIC_SITE_NAME || 'OrangeTV'; let announcement = process.env.ANNOUNCEMENT || @@ -94,6 +113,7 @@ export default async function RootLayout({ CUSTOM_CATEGORIES: customCategories, FLUID_SEARCH: fluidSearch, REQUIRE_DEVICE_CODE: requireDeviceCode, + THEME_CONFIG: themeConfig, }; return ( @@ -112,8 +132,60 @@ export default async function RootLayout({ }} /> - {/* 主题初始化脚本 - 立即执行避免主题闪烁 */} -