Files
novalon-website/e2e/first-paint-visibility.spec.ts
T
zhangxiang 2f86c4419a fix(motion): 站内跳转白屏——路由内容不再走 AnimatePresence wait 退场
现象:生产环境所有站内跳转白屏,地址与标题已切换,刷新才恢复。
根因:AnimatePresence mode="wait" 要求新页等旧页退场完成才挂载;App Router
导航期间的重复渲染会重置退场完成标记,切换 state 的窗口被错过,
motion.div 永久停在 exit 变体(opacity:0; translateY(-8px))。
修法:路由内容只保留 key={pathname} 的 280ms 入场;首屏仍由 mounted 渲染
裸 children;reduce 变体改为显式可见终值,避免切到空变体时冻结在半透明。
守卫:新增 client-navigation-visibility(导航后落定再读祖先链乘积);
首屏用例改逐帧采样;视觉用例改逐屏滚动触发 whileInView。
验证:E2E 20/20(四浏览器)、视觉 125/125、单测 1815、type-check/lint 0 error。
2026-10-20 14:00:41 +08:00

80 lines
3.8 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';
declare global {
interface Window {
__firstPaint?: Array<{ ready: string | null; opacity: number }>;
}
}
/**
* 冷首访可见性守卫(2026-10-20)。
*
* 缺陷原型:全站 framer 入场把初始 opacity 内联成 0(SSR 实测 `style="opacity:0;transform:translateY(30px)"`),
* 只能由 JS 水合后驱动 ⇒ 冷首访/弱网(实测限速下 3–5s)整页白,刷新后才好。
* 修复用 globals.css 的 `html:not([data-content-ready])` 规则在 JS 就绪前强制可见,
* 营销页 template 在水合后 800ms 置位交还 framer。
*
* 断言两条,都能失败:
* ① 禁用 JS(等价"脚本没跑起来"):h1 的内联样式仍是 opacity:0,但**计算值必须为 1**;
* ② 正常加载:水合后置位 data-content-ready,且 h1 在交接前后每一帧都可见(不闪回 0)。
*/
test.describe('首屏可见性(无 JS / 水合交接)', () => {
test('禁用 JS 时 h1 计算 opacity 为 1(内联仍是 0,靠兜底规则)', async ({ browser }) => {
const ctx = await browser.newContext({ javaScriptEnabled: false, viewport: { width: 1280, height: 900 } });
const page = await ctx.newPage();
await page.goto('/about', { waitUntil: 'domcontentloaded', timeout: 60000 });
// JS 关闭时 CSS 仍可能晚于 DOM 到达;不等到 load 就取 computed style,
// 会把「样式表还没到」误读成「兜底规则失效」(2026-10-20 并行跑实测)。
await page.waitForLoadState('load', { timeout: 30000 });
const snap = await page.evaluate(() => {
const h1 = document.querySelector('h1') as HTMLElement | null;
return {
inline: h1?.getAttribute('style') ?? null,
computed: h1 ? Number(getComputedStyle(h1).opacity) : -1,
};
});
expect(snap.inline, '前提:SSR 内联样式仍带 opacity:0(否则本用例失去意义)').toContain('opacity:0');
expect(snap.computed, '无 JS 时首屏必须可见(CSS 兜底生效)').toBe(1);
await ctx.close();
});
test('水合后 800ms 置位 data-content-ready 且 h1 始终可见', async ({ page }) => {
// 单点读 h1 会与「水合后子树重挂、入场动画重新开始」的瞬间竞争(并行跑偶发读到 0)。
// 改为逐帧采样:交接前(data-content-ready 未置位)每一帧都必须可见,交接后也必须可见。
await page.addInitScript(() => {
const samples: Array<{ ready: string | null; opacity: number }> = [];
window.__firstPaint = samples;
const tick = () => {
const h1 = document.querySelector('h1');
if (h1) {
samples.push({
ready: document.documentElement.getAttribute('data-content-ready'),
opacity: Number(getComputedStyle(h1).opacity),
});
}
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
});
await page.goto('/about', { waitUntil: 'domcontentloaded', timeout: 60000 });
await expect(page.locator('h1').first()).toBeVisible({ timeout: 10000 });
await expect
.poll(() => page.evaluate(() => document.documentElement.getAttribute('data-content-ready')), {
timeout: 5000,
message: '水合后应置位 data-content-ready(把控制权交还 framer)',
})
.toBe('1');
await page.waitForTimeout(400);
const samples = await page.evaluate(() => window.__firstPaint ?? []);
const beforeHandoff = samples.filter((s) => s.ready === null);
expect(beforeHandoff.length, '采样必须覆盖交接前(否则本用例什么都没测到)').toBeGreaterThan(0);
expect(
Math.min(...beforeHandoff.map((s) => s.opacity)),
'交接前 h1 任一帧都不得为 0',
).toBe(1);
expect(samples.at(-1)?.opacity, '交接后不得闪回 opacity:0').toBe(1);
});
});