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。
This commit is contained in:
zhangxiang committed 2026-10-20 14:00:41 +08:00
1 parent 092ed385b0
commit 2f86c4419a
7 files changed
+167 -54

No files matched your search

+37 -8
View File
@@ -1,5 +1,11 @@
import { test, expect } from './fixtures';
declare global {
interface Window {
__firstPaint?: Array<{ ready: string | null; opacity: number }>;
}
}
/**
* 冷首访可见性守卫(2026-10-20)。
*
@@ -10,13 +16,16 @@ import { test, expect } from './fixtures';
*
* 断言两条,都能失败:
* ① 禁用 JS(等价"脚本没跑起来"):h1 的内联样式仍是 opacity:0,但**计算值必须为 1**;
* ② 正常加载:水合后置位 data-content-ready,且 h1 仍可见(交接不闪回 0)。
* ② 正常加载:水合后置位 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 {
@@ -29,12 +38,26 @@ test.describe('首屏可见性(无 JS / 水合交接)', () => {
await ctx.close();
});
test('水合后 800ms 置位 data-content-ready 且 h1 仍可见', async ({ page }) => {
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 });
const h1 = page.locator('h1').first();
await expect(h1).toBeVisible({ timeout: 10000 });
const early = await h1.evaluate((el) => Number(getComputedStyle(el).opacity));
expect(early, '首帧(水合前后)h1 必须是可见的').toBe(1);
await expect(page.locator('h1').first()).toBeVisible({ timeout: 10000 });
await expect
.poll(() => page.evaluate(() => document.documentElement.getAttribute('data-content-ready')), {
@@ -44,7 +67,13 @@ test.describe('首屏可见性(无 JS / 水合交接)', () => {
.toBe('1');
await page.waitForTimeout(400);
const after = await h1.evaluate((el) => Number(getComputedStyle(el).opacity));
expect(after, '交接后不得闪回 opacity:0').toBe(1);
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);
});
});