Files
forumlify/components/Toast.js
T
lezi-fun 82bc2eb885 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.
2026-08-11 10:52:21 +08:00

49 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
// Toast 通知系统:右上角弹出,自动消失(替代 alert)
// 用法:const { toast } = useToast(); toast('发布成功', 'success');
// 注意:不用 createPortal(SSR 时 document 不存在会造成 hydration mismatch,
// 导致整树重建)。toast-container 是 position:fixed,渲染位置不影响视觉。
import { createContext, useContext, useState, useCallback, useRef } from 'react';
const ToastContext = createContext(null);
export function useToast() {
return useContext(ToastContext);
}
let idCounter = 0;
export function ToastProvider({ children }) {
const [toasts, setToasts] = useState([]);
const timersRef = useRef({});
const toast = useCallback((message, type = 'success', duration = 3000) => {
const id = ++idCounter;
setToasts((prev) => [...prev, { id, message, type }]);
if (timersRef.current[id]) clearTimeout(timersRef.current[id]);
timersRef.current[id] = setTimeout(() => {
setToasts((prev) => prev.filter((t) => t.id !== id));
delete timersRef.current[id];
}, duration);
}, []);
const dismiss = useCallback((id) => {
setToasts((prev) => prev.filter((t) => t.id !== id));
if (timersRef.current[id]) { clearTimeout(timersRef.current[id]); delete timersRef.current[id]; }
}, []);
return (
<ToastContext.Provider value={{ toast }}>
{children}
<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>
);
}