Files
novalon-website/e2e/reduced-motion-hydration.spec.ts
T
zhangxiang d0090cea56 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。
2026-10-20 08:00:20 +08:00

69 lines
3.2 KiB
TypeScript
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.
import { test, expect } from './fixtures';
/**
* 减少动效(prefers-reduced-motion: reduce)下的 hydration 与可见性回归。
*
* 缺陷原型(2026-10-20 从生产排查出来):`useReducedMotion` 用 `window.matchMedia` 作 useState 初值,
* 于是 SSR 恒 false、客户端首帧 true;组件再用它决定 `initial`/`variants`,
* 导致 ①React #418(hydration 不匹配)②减少动效用户看到大片元素停在 opacity:0。
*
* 断言两条,都不依赖像素:
* ① 页面加载与滚动过程中**不得出现 hydration 相关错误**(console.error 或 pageerror);
* ② 滚到底部后,末段标题的 opacity 必须为 1(内容真的可见,而不是停在初始隐藏态)。
*
* 敏感性:把 hooks/use-reduced-motion.ts 的 `useState(false)` 改回 matchMedia 初值,
* 或把 scroll-reveal 的 `initial={…}` 改回 `shouldReduceMotion ? {} : …` ⇒ 本例应转红。
*/
// 用 contextOptions(BrowserContextOptions 的正式字段)而不是 test.use 的便捷项:
// 后者不在本仓自定义 fixtures 的 TestOptions 推导里(type-check:e2e 会拦),且等价。
test.use({ contextOptions: { reducedMotion: 'reduce' } });
const HYDRATION_HINTS = [/hydrat/i, /did not match/i, /#418/, /Minified React error/];
function collectErrors(page: import('@playwright/test').Page) {
const hits: string[] = [];
page.on('pageerror', (e) => {
const s = String(e);
if (HYDRATION_HINTS.some((r) => r.test(s))) hits.push(`pageerror: ${s.slice(0, 240)}`);
});
page.on('console', (m) => {
if (m.type() !== 'error') return;
const s = m.text();
if (HYDRATION_HINTS.some((r) => r.test(s))) hits.push(`console: ${s.slice(0, 240)}`);
});
return hits;
}
const CASES = [
{ path: '/about', lateHeading: /准备好开始了吗/ },
{ path: '/', lateHeading: /现在有什么卡住你了/ },
];
for (const { path, lateHeading } of CASES) {
test(`${path} 在 reduce 下无 hydration 错误且末段可见`, async ({ page }) => {
const errors = collectErrors(page);
await page.goto(path, { waitUntil: 'networkidle', timeout: 60000 });
// 自证:确实处在 reduce 偏好下,否则本用例什么都没测到
expect(
await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches),
'reducedMotion 未生效',
).toBe(true);
const h1 = page.locator('h1').first();
await expect(h1).toBeVisible({ timeout: 15000 });
const h1Opacity = await h1.evaluate((el) => Number(getComputedStyle(el).opacity));
expect(h1Opacity, '首屏 h1 不得停在 opacity:0').toBe(1);
// 滚到底部,让 whileInView 触发;reduce 下应「一步到位」变可见
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
await page.waitForTimeout(1000);
const late = page.getByRole('heading', { name: lateHeading }).first();
await expect(late, '末段标题应存在(页面完整)').toBeVisible({ timeout: 10000 });
await expect
.poll(async () => late.evaluate((el) => Number(getComputedStyle(el).opacity)), { timeout: 5000 })
.toBe(1);
expect(errors, `出现 hydration 错误:\n${errors.join('\n')}`).toEqual([]);
});
}