import type { Metadata } from "next"; import { Toaster } from "sonner"; import { ThemeProvider } from "@/components/theme-provider"; import { FaviconUpdater } from "@/components/favicon-updater"; import "./globals.css"; import { SessionProvider } from "next-auth/react"; import { getSiteSettings } from "@/lib/db/queries"; export async function generateMetadata(): Promise { const settings = await getSiteSettings(); const updatedAt = "updatedAt" in settings && settings.updatedAt ? new Date(settings.updatedAt).getTime() : Date.now(); const faviconUrl = `/api/favicon?v=${updatedAt}-${Date.now()}`; const metadataBase = process.env.AUTH_URL ? new URL(process.env.AUTH_URL) : undefined; return { metadataBase, title: { template: `%s | ${settings.siteName}`, default: settings.siteName, }, description: settings.siteName, icons: { icon: [ { url: faviconUrl, sizes: "any" }, { url: faviconUrl, type: "image/x-icon" }, ], apple: faviconUrl, shortcut: faviconUrl, }, }; } export const viewport = { maximumScale: 1, // Disable auto-zoom on mobile Safari }; const LIGHT_THEME_COLOR = "hsl(0 0% 100%)"; const DARK_THEME_COLOR = "hsl(240deg 10% 3.92%)"; const THEME_COLOR_SCRIPT = `\ (function() { var html = document.documentElement; var meta = document.querySelector('meta[name="theme-color"]'); if (!meta) { meta = document.createElement('meta'); meta.setAttribute('name', 'theme-color'); document.head.appendChild(meta); } function updateThemeColor() { var isDark = html.classList.contains('dark'); meta.setAttribute('content', isDark ? '${DARK_THEME_COLOR}' : '${LIGHT_THEME_COLOR}'); } var observer = new MutationObserver(updateThemeColor); observer.observe(html, { attributes: true, attributeFilter: ['class'] }); updateThemeColor(); })();`; const CHUNK_ERROR_RECOVERY_SCRIPT = `\ (function() { // 安全地访问sessionStorage(某些浏览器设置可能禁止访问) function safeGetSessionStorage(key) { try { return sessionStorage.getItem(key); } catch (e) { return null; } } function safeSetSessionStorage(key, value) { try { sessionStorage.setItem(key, value); return true; } catch (e) { return false; } } function safeRemoveSessionStorage(key) { try { sessionStorage.removeItem(key); return true; } catch (e) { return false; } } var hasReloaded = safeGetSessionStorage('chunk-reload-attempted'); function shouldAutoRecover(message) { if (!message) return false; return ( message.includes('Failed to load chunk') || message.includes('Loading chunk') || message.includes('/_next/static/chunks/') || message.includes('Failed to find Server Action') || message.includes('Server Components render') || message.includes('Application error: a client-side exception') ); } function triggerAutoRecover(reason) { if (hasReloaded) return; console.warn('[RuntimeRecover] 检测到前端运行时异常,自动刷新页面...', reason || ''); safeSetSessionStorage('chunk-reload-attempted', 'true'); setTimeout(function() { window.location.reload(); }, 800); } window.addEventListener('error', function(event) { if (shouldAutoRecover(event && event.message)) { triggerAutoRecover(event && event.message); } }, true); window.addEventListener('unhandledrejection', function(event) { var reason = event && event.reason; var reasonText = ''; if (typeof reason === 'string') reasonText = reason; else if (reason && typeof reason.message === 'string') reasonText = reason.message; if (shouldAutoRecover(reasonText)) { triggerAutoRecover(reasonText); } }); // 页面加载成功后清除标记 window.addEventListener('load', function() { setTimeout(function() { safeRemoveSessionStorage('chunk-reload-attempted'); }, 2000); }); })();`; export default async function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { // 获取网站设置中的默认主题 const settings = await getSiteSettings(); const defaultTheme = settings.defaultTheme || "system"; return (