fix: 主题/声音水合错位 — store 水合移入 effect + getServerSnapshot 固定 SSR 快照
- 模块求值时立即读 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/背景全部落到正确值
This commit is contained in:
+40
-11
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useSyncExternalStore } from "react";
|
import { useEffect, useSyncExternalStore } from "react";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shell media state (theme + sound) — 1:1 port of the original
|
* 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() {
|
function onSystemChange() {
|
||||||
if (state.theme !== "system") return;
|
if (state.theme !== "system") return;
|
||||||
const resolved = resolve("system");
|
const resolved = resolve("system");
|
||||||
@@ -110,37 +119,57 @@ function onSystemChange() {
|
|||||||
setState({ resolvedTheme: resolved });
|
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) {
|
function setTheme(next: ThemeSetting) {
|
||||||
|
hydrateIfNeeded();
|
||||||
writeStored(THEME_KEY, next);
|
writeStored(THEME_KEY, next);
|
||||||
applyDocument(next);
|
applyDocument(next);
|
||||||
setState({ theme: next, resolvedTheme: resolve(next) });
|
setState({ theme: next, resolvedTheme: resolve(next) });
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSoundEnabled(next: boolean) {
|
function setSoundEnabled(next: boolean) {
|
||||||
|
hydrateIfNeeded();
|
||||||
writeStored(SOUND_KEY, next ? "on" : "off");
|
writeStored(SOUND_KEY, next ? "on" : "off");
|
||||||
setState({ soundEnabled: next });
|
setState({ soundEnabled: next });
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscribe(listener: () => void) {
|
function subscribe(listener: () => void) {
|
||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
return () => listeners.delete(listener);
|
return () => {
|
||||||
|
listeners.delete(listener);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const getSnapshot = () => state;
|
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 & {
|
export function useShellMedia(): ShellMediaState & {
|
||||||
setTheme: (next: ThemeSetting) => void;
|
setTheme: (next: ThemeSetting) => void;
|
||||||
setSoundEnabled: (next: boolean) => void;
|
setSoundEnabled: (next: boolean) => void;
|
||||||
} {
|
} {
|
||||||
const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
// Defer store hydration to the first effect tick (original: mounted flag
|
||||||
// Stable identity via module-level functions (no per-render closures).
|
// + 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);
|
return useSnapshotWithActions(snapshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user