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:
1 parent
b5fafdbc09
commit
82bc2eb885
6 files changed
+58
-72
No files matched your search
+15
-16
@@ -12,13 +12,9 @@ export function useApp() {
|
||||
|
||||
export default function AppProvider({ children }) {
|
||||
const [currentUser, setCurrentUser] = useState(null);
|
||||
// 初始化时先用 localStorage 缓存的论坛名,避免闪烁(默认名 → 正确名)
|
||||
const [forumName, setForumName] = useState(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
return localStorage.getItem('forumlify-forum-name') || 'Forumlify';
|
||||
}
|
||||
return 'Forumlify';
|
||||
});
|
||||
// 固定初始值(SSR/客户端一致,避免 hydration mismatch 导致加载圈重启);
|
||||
// 缓存论坛名由 useEffect 读取,首帧显示靠内联脚本(body 首个子元素)
|
||||
const [forumName, setForumName] = useState('Forumlify');
|
||||
const [theme, setThemeState] = useState('light');
|
||||
const [view, setView] = useState('feed'); // feed | post | new | admin | settings | messages | user | custom
|
||||
const [currentPostId, setCurrentPostId] = useState(null);
|
||||
@@ -27,14 +23,8 @@ export default function AppProvider({ children }) {
|
||||
const [sort, setSort] = useState('latest');
|
||||
const [refreshKey, setRefreshKey] = useState(0);
|
||||
const [ready, setReady] = useState(false); // 初始加载完成标记(控制加载页/淡入)
|
||||
// 有缓存论坛名即视为"已加载"(第二次访问立即显示缓存名,不必等服务器);
|
||||
// 首次访问无缓存 → 等服务器返回后才显示名字
|
||||
const [forumNameLoaded, setForumNameLoaded] = useState(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
return !!localStorage.getItem('forumlify-forum-name');
|
||||
}
|
||||
return false;
|
||||
});
|
||||
// 固定 false(避免 hydration mismatch);有缓存时由 useEffect 立即置 true
|
||||
const [forumNameLoaded, setForumNameLoaded] = useState(false);
|
||||
const loadedRef = useRef(false);
|
||||
|
||||
const refresh = useCallback(() => setRefreshKey((k) => k + 1), []);
|
||||
@@ -75,8 +65,17 @@ export default function AppProvider({ children }) {
|
||||
});
|
||||
}, []);
|
||||
|
||||
// 初始化:恢复登录、加载论坛名
|
||||
// 初始化:读取缓存论坛名(立即显示),恢复登录、加载服务器论坛名
|
||||
useEffect(() => {
|
||||
// 缓存论坛名(hydrate 后立即生效,首帧显示由内联脚本完成)
|
||||
try {
|
||||
const cached = localStorage.getItem('forumlify-forum-name');
|
||||
if (cached) {
|
||||
setForumName(cached);
|
||||
setForumNameLoaded(true);
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
if (loadedRef.current) return;
|
||||
loadedRef.current = true;
|
||||
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
'use client';
|
||||
|
||||
// 全屏加载页:初始化未完成时显示转圈,加载完成后淡出
|
||||
// - 名字容器总是渲染(空):layout.js 内联脚本会在 DOMContentLoaded(早于
|
||||
// React hydrate)时把缓存的论坛名填入,实现二次访问首帧即显示名字
|
||||
// - 首次访问无缓存:由 React 在收到服务器论坛名后淡入显示
|
||||
export default function LoadingScreen({ forumName, forumNameLoaded }) {
|
||||
// - 名字容器用 dangerouslySetInnerHTML(空):React 完全不管理其文本内容,
|
||||
// 内联脚本(body 首个子元素,DOMContentLoaded)填入缓存名后不会被 React
|
||||
// 检测/重写 → 无 hydration mismatch,加载圈不会重启,名字不会二次出现
|
||||
export default function LoadingScreen() {
|
||||
return (
|
||||
<div className="loading-screen">
|
||||
<div className="loading-inner">
|
||||
<div className="loading-spinner" />
|
||||
{/* suppressHydrationWarning: 内联脚本会在 hydrate 前填入缓存名,
|
||||
避免 React 检测到文本不匹配而重写 DOM(名字二次出现/转圈重启) */}
|
||||
<div className="loading-title loading-title-appear" id="loadingTitle" suppressHydrationWarning>
|
||||
{forumNameLoaded && forumName}
|
||||
</div>
|
||||
<div className="loading-title loading-title-appear" id="loadingTitle" dangerouslySetInnerHTML={{ __html: '' }} />
|
||||
<div className="loading-sub">加载中...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+9
-11
@@ -2,8 +2,9 @@
|
||||
|
||||
// Toast 通知系统:右上角弹出,自动消失(替代 alert)
|
||||
// 用法:const { toast } = useToast(); toast('发布成功', 'success');
|
||||
// 注意:不用 createPortal(SSR 时 document 不存在会造成 hydration mismatch,
|
||||
// 导致整树重建)。toast-container 是 position:fixed,渲染位置不影响视觉。
|
||||
import { createContext, useContext, useState, useCallback, useRef } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
|
||||
const ToastContext = createContext(null);
|
||||
|
||||
@@ -35,16 +36,13 @@ export function ToastProvider({ children }) {
|
||||
return (
|
||||
<ToastContext.Provider value={{ toast }}>
|
||||
{children}
|
||||
{typeof document !== 'undefined' && createPortal(
|
||||
<div className="toast-container">
|
||||
{toasts.map((t) => (
|
||||
<div key={t.id} className={'toast toast-' + t.type} onClick={() => dismiss(t.id)}>
|
||||
{t.message}
|
||||
</div>
|
||||
))}
|
||||
</div>,
|
||||
document.body
|
||||
)}
|
||||
<div className="toast-container">
|
||||
{toasts.map((t) => (
|
||||
<div key={t.id} className={'toast toast-' + t.type} onClick={() => dismiss(t.id)}>
|
||||
{t.message}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user