fix(motion): 冷首访白屏兜底(JS 就绪前可见) #51
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { test, expect } from './fixtures';
|
||||
|
||||
/**
|
||||
* 冷首访可见性守卫(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 });
|
||||
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 }) => {
|
||||
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
|
||||
.poll(() => page.evaluate(() => document.documentElement.getAttribute('data-content-ready')), {
|
||||
timeout: 5000,
|
||||
message: '水合后应置位 data-content-ready(把控制权交还 framer)',
|
||||
})
|
||||
.toBe('1');
|
||||
|
||||
await page.waitForTimeout(400);
|
||||
const after = await h1.evaluate((el) => Number(getComputedStyle(el).opacity));
|
||||
expect(after, '交接后不得闪回 opacity:0').toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { motion, type Variants } from 'framer-motion';
|
||||
import { useReducedMotion } from '@/hooks/use-reduced-motion';
|
||||
|
||||
@@ -36,6 +37,15 @@ export default function MarketingTemplate({
|
||||
}) {
|
||||
const shouldReduceMotion = useReducedMotion();
|
||||
|
||||
// 2026-10-20:水合后把首屏可见性的兜底交还给 framer(见 globals.css 的 data-content-ready 规则)。
|
||||
// 800ms > 最长入场 280ms,保证交接时入场动画已结束、不会闪一下。
|
||||
useEffect(() => {
|
||||
const id = window.setTimeout(() => {
|
||||
document.documentElement.setAttribute('data-content-ready', '1');
|
||||
}, 800);
|
||||
return () => window.clearTimeout(id);
|
||||
}, []);
|
||||
|
||||
// 2026-10-20:**不再早返回 `<>{children}</>`** —— 那会让树结构与 SSR 不同
|
||||
// (服务端永远渲染 motion.div,客户端 reduce 用户渲染裸 Fragment)⇒ React #418。
|
||||
// 改为结构恒定,只在挂载后把 variants 换成空组(时长 0、无位移)。
|
||||
|
||||
@@ -1513,3 +1513,17 @@ p, li, span {
|
||||
font-style: normal;
|
||||
/* 子集只覆盖源码实际用字(1050 码位);CMS 新增中文由后面的系统衬线兜底 */
|
||||
}
|
||||
|
||||
/* ─────────────────────────────────────────────────────────────────────────
|
||||
首屏可见性兜底(2026-10-20,冷首访白屏)
|
||||
全站 167 处 framer-motion 入场把初始 opacity 内联成 0
|
||||
(实测 SSR: style="opacity:0;transform:translateY(30px)"),
|
||||
而它只能由 JS 水合后驱动 ⇒ 冷首访/弱网/脚本失败时整页白(实测限速下 3–5s)。
|
||||
规则:`data-content-ready` 未置位前,强制这些入场元素可见;由营销页 template
|
||||
在水合后 800ms(> 最长入场 280ms + 余量)置位,把控制权交还 framer。
|
||||
失败也是敞开的:若 JS 始终不执行,则永不置位 ⇒ 内容保持可见。
|
||||
───────────────────────────────────────────────────────────────────────── */
|
||||
html:not([data-content-ready]) [style*="opacity:0"],
|
||||
html:not([data-content-ready]) [style*="opacity: 0"] {
|
||||
opacity: 1 !important;
|
||||
}
|
||||
Reference in new issue
Block a user