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

+9 -2
View File
@@ -8,13 +8,20 @@ const nextConfig = {
{ source: '/uploads/:name', destination: '/api/uploads/:name' },
];
},
// 论坛是动态内容:HTML 页面不缓存,避免 CDN/浏览器拿到旧版本
// 缓存策略:动态页面/API no-cache(浏览器每次验证 → 304);
// 静态资源(_next/static)immutable 长缓存(Next.js 默认,但需在 no-cache 之后覆盖)
async headers() {
return [
{
source: '/:path*',
headers: [
{ key: 'Cache-Control', value: 'no-cache, no-store, must-revalidate' },
{ key: 'Cache-Control', value: 'no-cache, must-revalidate' },
],
},
{
source: '/_next/static/:path*',
headers: [
{ key: 'Cache-Control', value: 'public, max-age=31536000, immutable' },
],
},
];