fix(motion): 冷首访白屏——JS 就绪前强制入场元素可见,水合后再交还 framer
用户反馈「首次访问白屏,需要刷新才可以」。用禁用缓存 + 限速复现并量化:
745ms 时 SSR HTML 已在(正文 1177 字),但 h1 的 opacity 仍为 0;
直到 5945ms 才变 1 ⇒ 冷首访/弱网下 3–5 秒白屏,刷新后脚本已在缓存里所以「刷新就好」。
根因是全站性的:167 处 framer 入场把初始 opacity 内联成 0
(SSR 实测 style="opacity:0;transform:translateY(30px)"),而它只能由 JS 驱动;
页面级 template 的 initial 也是 {opacity:0,y:8}。逐文件改既大又有误伤风险
(面板/轮播的 opacity 是状态语义,不是入场)。
修法(一处兜底 + 一处交接,失败也是敞开的):
- globals.css 新增规则:当 html 上没有 data-content-ready 时,
把内联 opacity:0 的入场元素强制为 1(同时匹配 "opacity:0" 与 "opacity: 0" 两种写法)。
JS 尚未执行时内容可见;若脚本永远失败,则永不置位,内容一直可见。
- 营销页 template:水合后 800ms(> 最长入场 280ms + 余量)置位 data-content-ready,
把控制权交还 framer,避免「闪一下」。
回归守卫 e2e/first-paint-visibility.spec.ts:
① 禁用 JS 时 h1 内联仍 opacity:0 但计算值为 1(兜底生效,且证明前提仍在);
② 正常加载时 data-content-ready 置位、交接前后 h1 均为 1。
变异验证:删掉 CSS 规则 ⇒ 2 条全红;恢复 ⇒ 2 条绿。
复算:tsc / type-check:e2e 0、check:visual 0、视觉 chromium-desktop 25/25、reduce 回归 2 绿。
This commit was merged in pull request #51.
This commit is contained in:
1 parent
12999348bc
commit
ca3df3221d
3 files changed
+74
No files matched your search
@@ -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