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

+9 -3
View File
@@ -48,14 +48,20 @@ export default function MarketingTemplate({
// 2026-10-20:**不再早返回 `<>{children}</>`** —— 那会让树结构与 SSR 不同
// (服务端永远渲染 motion.div,客户端 reduce 用户渲染裸 Fragment)⇒ React #418。
// 改为结构恒定,只在挂载后把 variants 换成空组(时长 0、无位移)。
// 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: {}, enter: {}, exit: {} }
? {
initial: { opacity: 1, y: 0 },
enter: { opacity: 1, y: 0, transition: { duration: 0 } },
}
: pageVariants;
return (
<motion.div
initial="initial"
initial={shouldReduceMotion ? false : 'initial'}
animate="enter"
exit="exit"
variants={variants}
+22 -33
View File
@@ -1,6 +1,6 @@
'use client';
import { motion, AnimatePresence } from 'framer-motion';
import { motion } from 'framer-motion';
import { usePathname } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useReducedMotion } from '@/hooks/use-reduced-motion';
@@ -22,15 +22,6 @@ const pageVariants = {
staggerChildren: 0.05,
},
},
exit: {
opacity: 0,
y: -8,
scale: 0.98,
transition: {
duration: 0.2,
ease: [0.22, 1, 0.36, 1] as const,
},
},
};
export function PageTransition({ children }: { children: React.ReactNode }) {
@@ -47,34 +38,33 @@ export function PageTransition({ children }: { children: React.ReactNode }) {
return <>{children}</>;
}
// 2026-10-20(多端复核):reduce 下**不能**在 mounted 之后切回裸 children。
// 原先 `if (!mounted || shouldReduceMotion) return children` 会让已挂载的 motion.div
// 在 reduce 状态落地的瞬间被 AnimatePresence 的 exit 链接管,而在 headless/移动仿真里
// exit 链没有稳定落点,实测整页祖先停在 opacity 0.15–0.35(桌面 1440 甚至 0)。
// 现在无论是否 reduce 都保留同一个 motion.div,只用零时长 + 可见变体把动画「一步到位」。
// 2026-10-20(生产导航白屏):**不再用 AnimatePresence 包裹路由内容**。
// 原写法 `<AnimatePresence mode="wait"><motion.div key={pathname} exit="exit">` 会让新页面
// 等旧页面退场完成后才挂载;而 Next App Router 在导航过程中会多次重渲染本组件,每次新的
// children 引用都会让 AnimatePresence 重跑 diff 并把退场完成标记重置,200ms 退场动画结束后
// 切换 state 的机会被错过 ⇒ 同一个 motion.div 永久停在 exit 变体(实测 inline opacity:0;
// translateY(-8px)),整页白屏,刷新(走 mounted=false 的裸 children 路径)才恢复。
// 现在只保留入场:key 随 pathname 变化触发重挂,入场动画仍按令牌层 280ms 播放。
// 回归守卫:e2e/client-navigation-visibility.spec.ts。
const reducedVariants = {
initial: { opacity: 1, y: 0, scale: 1 },
animate: { opacity: 1, y: 0, scale: 1, transition: { duration: 0 } },
exit: { opacity: 1, transition: { duration: 0 } },
};
const variants = shouldReduceMotion ? reducedVariants : pageVariants;
return (
<AnimatePresence mode="wait" initial={false}>
<motion.div
key={pathname}
variants={variants}
initial={shouldReduceMotion ? false : 'initial'}
animate="animate"
exit="exit"
style={{
willChange: 'transform, opacity',
backfaceVisibility: 'hidden',
}}
>
{children}
</motion.div>
</AnimatePresence>
<motion.div
key={pathname}
variants={variants}
initial={shouldReduceMotion ? false : 'initial'}
animate="animate"
style={{
willChange: 'transform, opacity',
backfaceVisibility: 'hidden',
}}
>
{children}
</motion.div>
);
}
@@ -125,8 +115,7 @@ export function StaggerChildren({
{child}
</motion.div>
))
: children
}
: children}
</motion.div>
);
}