fix(motion): reduce 偏好下 hydration 不匹配(React #418)且元素停在 opacity:0

线上 /about 白屏排查(§16):30 条路由 + 6 种环境里唯一可复现的缺陷是
webkit/chromium + prefers-reduced-motion:reduce ⇒ pageerror #418,main 内 14 个元素
opacity 恒 0(正常环境只有 3 个首屏以下的滚动入场项)。

根因:useReducedMotion 用 window.matchMedia 作 useState 初值 ⇒ SSR false、客户端首帧 true;
scroll-reveal / template / page-transition 又用它决定 initial/variants/甚至早返回不同树结构,
与 SSR HTML 不一致;且 reduce 时 whileInView 被清空 ⇒ 元素停在 initial 的 opacity:0。

修法(结构恒定,减少动效改用「时长 0」表达):
- hook 首帧恒 false,挂载后 effect 同步真实偏好;
- ScrollReveal/StaggerReveal 的 initial/whileInView/variants 不再按 reduce 清空;
- template.tsx 与 StaggerChildren 去掉早返回裸结构,改空 variants。

回归守卫 e2e/reduced-motion-hydration.spec.ts(/about 与 /):断言 reduce 自证生效、
无 hydration 错误、h1 与末段标题 opacity=1。变异验证:hook 改回 matchMedia 初值 ⇒ 2 红,恢复 ⇒ 2 绿。

另:runbook 明确禁止对 dist/_next/static 用 --delete(回访者旧 HTML 会引用上一代 chunk);
本轮已从备份 --ignore-existing 合并回旧代 101 个文件。复算:tsc/type-check:e2e 0、
jest 35 套件 462 例 0、check:visual 0、check:motion 0、视觉 chromium-desktop 25/25。
This commit was merged in pull request #46.
This commit is contained in:
zhangxiang committed 2026-10-20 08:00:20 +08:00
1 parent 524c1b6575
commit d0090cea56
20 files changed
+341 -22

No files matched your search

+7 -4
View File
@@ -36,16 +36,19 @@ export default function MarketingTemplate({
}) {
const shouldReduceMotion = useReducedMotion();
if (shouldReduceMotion) {
return <>{children}</>;
}
// 2026-10-20:**不再早返回 `<>{children}</>`** —— 那会让树结构与 SSR 不同
// (服务端永远渲染 motion.div,客户端 reduce 用户渲染裸 Fragment)⇒ React #418。
// 改为结构恒定,只在挂载后把 variants 换成空组(时长 0、无位移)。
const variants = shouldReduceMotion
? { initial: {}, enter: {}, exit: {} }
: pageVariants;
return (
<motion.div
initial="initial"
animate="enter"
exit="exit"
variants={pageVariants}
variants={variants}
>
{children}
</motion.div>
+4 -6
View File
@@ -91,10 +91,8 @@ export function StaggerChildren({
}) {
const shouldReduceMotion = useReducedMotion();
if (shouldReduceMotion) {
return <div className={className}>{children}</div>;
}
// 2026-10-20:不再早返回裸 div —— 结构必须与 SSR 一致(否则减少动效用户挂载后会整棵树重挂,
// 与 use-reduced-motion 的 hydration 修复同一原则)。减少动效只把 stagger 压到 0。
return (
<motion.div
className={className}
@@ -103,8 +101,8 @@ export function StaggerChildren({
variants={{
visible: {
transition: {
staggerChildren: 0.06,
delayChildren: 0.05,
staggerChildren: shouldReduceMotion ? 0 : 0.06,
delayChildren: shouldReduceMotion ? 0 : 0.05,
},
},
}}
+11 -5
View File
@@ -79,12 +79,16 @@ export function ScrollReveal({
return (
<motion.div
className={className}
initial={shouldReduceMotion ? {} : getInitialState(variant, yOffset, xOffset)}
whileInView={shouldReduceMotion ? {} : getAnimateState(variant)}
// 2026-10-20:initial/whileInView **不随 reduce 清空**。
// 清空只会让元素停在 initial 的 opacity:0 上(减少动效用户看到空白),
// 而且与 SSR HTML 不一致会触发 React #418。减少动效用「时长 0」表达:
// 元素照常从 initial 走到 animate,只是一步到位。
initial={getInitialState(variant, yOffset, xOffset)}
whileInView={getAnimateState(variant)}
viewport={{ once, margin, amount }}
transition={{
duration: shouldReduceMotion ? 0 : duration,
delay,
delay: shouldReduceMotion ? 0 : delay,
ease: EASE_OUT,
}}
>
@@ -131,8 +135,10 @@ export function StaggerReveal({
};
const itemVariants = {
hidden: shouldReduceMotion ? {} : getInitialState(variant, yOffset, xOffset),
visible: shouldReduceMotion ? {} : getAnimateState(variant),
// 同 ScrollReveal:状态本身不随 reduce 清空(清空会让元素停在 opacity:0,
// 且与 SSR 首帧不一致 ⇒ React #418)。减少动效只把 stagger/时长压到 0。
hidden: getInitialState(variant, yOffset, xOffset),
visible: getAnimateState(variant),
};
return (
+8 -6
View File
@@ -1,12 +1,12 @@
import { useEffect, useState } from 'react';
export function useReducedMotion() {
const [shouldReduceMotion, setShouldReduceMotion] = useState(() => {
if (typeof window !== 'undefined') {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
return false;
});
// 2026-10-20(生产白屏排查):**首帧必须与服务器一致**。
// 浏览器里用 `window.matchMedia` 作 useState 初值时,SSR 恒为 false、客户端首帧为 true
// ⇒ 任何依赖它的 initial/variants 都会与 SSR HTML 不同 ⇒ React #418(hydration 不匹配),
// 且减少动效用户会看到大片元素停在 opacity:0(实测 /about 上 14 个)。
// 改为:首帧恒 false,挂载后用 effect 同步真实偏好(此时已过 hydration,改值安全)。
const [shouldReduceMotion, setShouldReduceMotion] = useState(false);
useEffect(() => {
if (typeof window === 'undefined') {
@@ -14,6 +14,8 @@ export function useReducedMotion() {
}
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
// 立即同步一次:挂载后才生效,不会影响 hydration
setShouldReduceMotion(mediaQuery.matches);
const handleChange = (event: MediaQueryListEvent) => {
setShouldReduceMotion(event.matches);