现象:生产环境所有站内跳转白屏,地址与标题已切换,刷新才恢复。
根因: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。
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
'use client';
|
||
|
||
import { useEffect } from 'react';
|
||
import { motion, type Variants } from 'framer-motion';
|
||
import { useReducedMotion } from '@/hooks/use-reduced-motion';
|
||
|
||
const pageVariants: Variants = {
|
||
initial: {
|
||
opacity: 0,
|
||
y: 8,
|
||
},
|
||
enter: {
|
||
opacity: 1,
|
||
y: 0,
|
||
transition: {
|
||
duration: 0.28,
|
||
// DEC-N3(2026-10-08):原 [0.16, 1, 0.3, 1] 是令牌层之外的写死曲线,
|
||
// 归口到 CONTEXT.md:77 的统一缓动 ease-ink = [0.22, 1, 0.36, 1](globals.css --ease-ink)。
|
||
ease: [0.22, 1, 0.36, 1] as [number, number, number, number],
|
||
},
|
||
},
|
||
exit: {
|
||
opacity: 0,
|
||
y: -4,
|
||
transition: {
|
||
duration: 0.2,
|
||
// 同上:退场同样走 ease-ink(与 page-transition.tsx exit 的曲线一致)。
|
||
ease: [0.22, 1, 0.36, 1] as [number, number, number, number],
|
||
},
|
||
},
|
||
};
|
||
|
||
export default function MarketingTemplate({
|
||
children,
|
||
}: {
|
||
children: React.ReactNode;
|
||
}) {
|
||
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。
|
||
// 2026-10-20(导航白屏修复的连带回归):reduce 变体不能写成 `{}`。motion.div 挂载时
|
||
// hook 仍是 false(首帧与 SSR 一致),它按 pageVariants 开始 0→1 的入场;effect 切到
|
||
// reduce 后 `animate="enter"` 若映射到空变体,framer 会停在当前帧(实测 opacity 0.2)
|
||
// 而不是「一步到位」。改为显式给可见终值 + 0 时长。
|
||
const variants = shouldReduceMotion
|
||
? {
|
||
initial: { opacity: 1, y: 0 },
|
||
enter: { opacity: 1, y: 0, transition: { duration: 0 } },
|
||
}
|
||
: pageVariants;
|
||
|
||
return (
|
||
<motion.div
|
||
initial={shouldReduceMotion ? false : 'initial'}
|
||
animate="enter"
|
||
exit="exit"
|
||
variants={variants}
|
||
>
|
||
{children}
|
||
</motion.div>
|
||
);
|
||
}
|