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
+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