fix: hydration mismatch (loading spinner restart) + cache strategy

Root cause of loading spinner restarting / name appearing twice:
React hydration mismatch → whole tree regenerated. Fixed all sources:
- AppProvider: remove typeof window in useState init (SSR/client diff);
  read cached forum name in useEffect instead
- ToastProvider: remove createPortal(document.body) (SSR absent →
  mismatch)
- LoadingScreen: name container uses dangerouslySetInnerHTML('') so
  React never manages/clobbers the inline-script-filled text
- layout.js: inline script moved to body first child (React official
  anti-FOUC pattern)

Cache: dynamic pages/API no-cache + ETag (304 on revalidate); static
/_next/static assets immutable 1y. Verified: 304 + immutable headers.
This commit is contained in:
lezi-fun committed 2026-08-11 10:52:21 +08:00
1 parent b5fafdbc09
commit 82bc2eb885
6 files changed
+58 -72

No files matched your search

+19 -33
View File
@@ -13,41 +13,27 @@ export default function RootLayout({ children }) {
return (
<html lang="zh-CN" suppressHydrationWarning>
<head>
{/* 首帧前同步主题,避免深色模式闪白(FOUC):手动设置优先,否则跟随系统 */}
<script
dangerouslySetInnerHTML={{
__html: `(function(){
try {
var t = localStorage.getItem('forumlify-theme');
if (!t) {
t = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ? 'dark' : 'light';
}
document.documentElement.setAttribute('data-theme', t);
} catch (e) {}
// 论坛名:有缓存立即写入 <title>(第二次访问首帧即显示),无缓存保持 Loading...
try {
var n = localStorage.getItem('forumlify-forum-name');
if (n) document.title = n;
} catch (e) {}
// 加载页名字:DOMContentLoaded(早于 React hydrate)时把缓存名填入,
// 二次访问首帧就显示论坛名,不必等 React 启动
document.addEventListener('DOMContentLoaded', function () {
try {
var n2 = localStorage.getItem('forumlify-forum-name');
if (n2) {
var el = document.getElementById('loadingTitle');
if (el && !el.textContent) {
el.textContent = n2;
document.title = n2;
}
}
} catch (e) {}
});
})();`,
}}
/>
</head>
<body>
{/* 首帧初始化(主题防 FOUC + 论坛名首帧显示):
script 放 body 首个子元素(children 前),React 官方防 FOUC 模式 */}
<script dangerouslySetInnerHTML={{ __html: `(function(){
try {
var t = localStorage.getItem('forumlify-theme');
if (!t) { t = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ? 'dark' : 'light'; }
document.documentElement.setAttribute('data-theme', t);
} catch (e) {}
try { var n = localStorage.getItem('forumlify-forum-name'); if (n) document.title = n; } catch (e) {}
document.addEventListener('DOMContentLoaded', function () {
try {
var n2 = localStorage.getItem('forumlify-forum-name');
if (n2) {
var el = document.getElementById('loadingTitle');
if (el && !el.textContent) { el.textContent = n2; document.title = n2; }
}
} catch (e) {}
});
})();` }} />
<ToastProvider>
<CustomCssLoader />
{children}
+1 -1
View File
@@ -27,7 +27,7 @@ function HomeInner() {
// 初始化未完成:显示全屏加载页,避免未加载完的界面闪烁
if (!ready) {
return <LoadingScreen forumName={forumName} forumNameLoaded={forumNameLoaded} />;
return <LoadingScreen />;
}
return (