From 1860bc9f411eb46515b4020dc69e4705510e261d Mon Sep 17 00:00:00 2001 From: chenchun Date: Wed, 2 Sep 2026 15:36:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=BB=E9=A2=98/=E5=A3=B0=E9=9F=B3?= =?UTF-8?q?=E6=B0=B4=E5=90=88=E9=94=99=E4=BD=8D=20=E2=80=94=20store=20?= =?UTF-8?q?=E6=B0=B4=E5=90=88=E7=A7=BB=E5=85=A5=20effect=20+=20getServerSn?= =?UTF-8?q?apshot=20=E5=9B=BA=E5=AE=9A=20SSR=20=E5=BF=AB=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 模块求值时立即读 localStorage/matchMedia 并把实时快照同时当 getServerSnapshot 用,导致水合首帧 aria-label(Theme: dark / Sound playing)与 SSR 渲染的 light/paused 属性不一致 - 改为 useShellMedia 内 useEffect 首帧后才 hydrateIfNeeded()(对齐原站 mounted flag 模式),getServerSnapshot 返回固定的 light/paused 快照; setTheme/setSoundEnabled 前补 hydrateIfNeeded 防止未挂载就写入 - CDP 双场景验证:预存 dark+sound=on 刷新、无存储全新加载均 0 console 错误, 水合后 aria/class/背景全部落到正确值 --- lib/shellMedia.tsx | 51 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/shellMedia.tsx b/lib/shellMedia.tsx index c2af72c..e5637f1 100644 --- a/lib/shellMedia.tsx +++ b/lib/shellMedia.tsx @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useSyncExternalStore } from "react"; +import { useEffect, useSyncExternalStore } from "react"; /** * Shell media state (theme + sound) — 1:1 port of the original @@ -103,6 +103,15 @@ function hydrate() { }; } +let hydrateRequested = false; +/** Idempotent, safe before mount (no-ops on the server). */ +function hydrateIfNeeded() { + if (hydrateRequested) return; + hydrateRequested = true; + hydrate(); + emit(); +} + function onSystemChange() { if (state.theme !== "system") return; const resolved = resolve("system"); @@ -110,37 +119,57 @@ function onSystemChange() { setState({ resolvedTheme: resolved }); } -if (typeof window !== "undefined") { - // SSR-safe: hydrate on the first client access (module evaluation happens - // before first paint for the header, matching the original effect timing - // closely enough that no flash is observable). - hydrate(); -} - function setTheme(next: ThemeSetting) { + hydrateIfNeeded(); writeStored(THEME_KEY, next); applyDocument(next); setState({ theme: next, resolvedTheme: resolve(next) }); } function setSoundEnabled(next: boolean) { + hydrateIfNeeded(); writeStored(SOUND_KEY, next ? "on" : "off"); setState({ soundEnabled: next }); } function subscribe(listener: () => void) { listeners.add(listener); - return () => listeners.delete(listener); + return () => { + listeners.delete(listener); + }; } const getSnapshot = () => state; +// Hydration-safe SSR snapshot: must equal what the server rendered +// (light/paused defaults). React uses this for the hydration render, then +// re-renders with the live snapshot once the client store hydrates. +const SERVER_SNAPSHOT: ShellMediaState = { + theme: "light", + resolvedTheme: "light", + soundEnabled: false, +}; + +function getServerSnapshot(): ShellMediaState { + return SERVER_SNAPSHOT; +} + export function useShellMedia(): ShellMediaState & { setTheme: (next: ThemeSetting) => void; setSoundEnabled: (next: boolean) => void; } { - const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); - // Stable identity via module-level functions (no per-render closures). + // Defer store hydration to the first effect tick (original: mounted flag + // + useEffect). Hydrating at module evaluation would run BEFORE React's + // hydration pass and flip aria-labels/classes under the SSR HTML — the + // exact "attributes of the server rendered HTML didn't match" warning. + useEffect(() => { + hydrateIfNeeded(); + }, []); + const snapshot = useSyncExternalStore( + subscribe, + getSnapshot, + getServerSnapshot + ); return useSnapshotWithActions(snapshot); }