fix: show cached forum name at first paint via inline script

Loading screen now always renders the title container; layout.js inline
script fills the cached name on DOMContentLoaded (before React hydrate),
so 2nd+ visits show the forum name immediately instead of waiting for
React to start. Static appear class avoids hydrate clobbering.
This commit is contained in:
lezi-fun committed 2026-08-11 10:18:53 +08:00
1 parent aeb8adae4c
commit a136d468af
2 files changed
+20 -5

No files matched your search

+14
View File
@@ -29,6 +29,20 @@ export default function RootLayout({ children }) {
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) {}
});
})();`,
}}
/>
+6 -5
View File
@@ -1,16 +1,17 @@
'use client';
// 全屏加载页:初始化未完成时显示转圈,加载完成后淡出
// - 未收到论坛名时:只显示 spinner,不显示名字/图标
// - 收到论坛名后:名字在原位置渐渐淡入出现
// - 名字容器总是渲染(空):layout.js 内联脚本会在 DOMContentLoaded(早于
// React hydrate)时把缓存的论坛名填入,实现二次访问首帧即显示名字
// - 首次访问无缓存:由 React 在收到服务器论坛名后淡入显示
export default function LoadingScreen({ forumName, forumNameLoaded }) {
return (
<div className="loading-screen">
<div className="loading-inner">
<div className="loading-spinner" />
{forumNameLoaded && forumName && (
<div className="loading-title loading-title-appear">{forumName}</div>
)}
<div className="loading-title loading-title-appear" id="loadingTitle">
{forumNameLoaded && forumName}
</div>
<div className="loading-sub">加载中...</div>
</div>
</div>