page-transition-animation
Page Transition Animation (Next.js App Router)
Implement page enter/exit transitions in the Next.js App Router, and fix the single most common failure: Framer Motion AnimatePresence exit animations never fire on navigation. The reason is structural — in the App Router, when navigating, Next.js unmounts the old route's content and mounts the new one almost immediately. AnimatePresence can only animate an exit if the exiting element stays mounted under a persistent AnimatePresence wrapper long enough to run the animation. Because the router swaps children out from under it, the exit is skipped. Two approaches solve this: a pathname-keyed AnimatePresence with the FrozenRouter pattern, or the View Transitions API (native / next-view-transitions).
When to use
Use when adding animated page transitions in a Next.js App Router app, when a Framer Motion exit prop does nothing on route change, when an old page disappears instantly instead of animating out, when choosing between Framer Motion and the View Transitions API for Next.js, or when debugging why AnimatePresence won't animate between routes.
Why exit doesn't fire (the core problem)
AnimatePresence detects removal of a direct keyed child. On App Router navigation:
- The wrapper must persist across the navigation. If
AnimatePresencelives in a component that itself unmounts, there is nothing left to run the exit. - The child's
keymust change per route so AnimatePresence sees "old removed, new added". - During the brief overlap, the outgoing subtree must still render its old content — but the App Router has already swapped the route context, so the outgoing tree would otherwise render the new page's data. This is what FrozenRouter fixes.
Solution A: template.tsx + keyed AnimatePresence + FrozenRouter
template.tsx is the App Router's purpose-built hook for this: unlike layout.tsx (which persists), a template remounts on every navigation, giving each route a fresh instance — ideal for per-route enter animations. Combine it with a pathname-keyed AnimatePresence and FrozenRouter for clean exit + enter.