Navigation
Read this when changing how pages live and die: keys, keepalive, Nuxt app hooks, scroll, focus, links, history, View Transitions, and SSR.
Page lifecycle
mount → initial state → intro → settled → outro → end state → unmount
On a Nuxt route change with GSAP in the transition hooks:
- Navigation. A
<NuxtLink>click ornavigateTo.page:loading:start, route middleware, guards, the page chunk (usually prefetched),beforeResolve, commit. The old page is untouched. - Incoming setup.
<NuxtPage>renders the new page under Suspense:page:start, then its setup and data run off-screen. The old page stays visible and interactive. - Outro. Suspense resolves:
page:finish, thenpage:loading:end. Vue unmounts the old page component and hands its root toonBeforeLeaveandonLeave. Underout-inthe new root waits; withoutmodeit is inserted now. - End state. The outro's
done. Vue removes the old root;onAfterLeaveandpage:transition:finishfire; Nuxt scrolls one frame later. - Initial state and intro.
onBeforeEnterbefore insertion,onEnterafter, the page'sonMountedin the same flush. Write start values, then play. - Settled. The intro's
done, thenonAfterEnter. Clear temporary styles. CSS owns the page.
One navigation at a time is Vue Router's job: a new navigation cancels a pending one, and Nuxt remounts the Suspense. Your job is that every hook call ends in done and every timeline can be killed.
When a page is reused versus remounted
<NuxtPage> keys the page on the pageKey prop, else definePageMeta({ key }), else the matched route path with params filled in. So:
/posts/1to/posts/2changes the key: remount, full transition,page:startandpage:finish.- Query or hash changes keep the key: the component is reused, no transition, no
page:start.page:loading:startandpage:loading:endstill fire. Animate in place fromonBeforeRouteUpdateor awatchon the route. key: route => route.fullPathremounts on query changes too. Use it only when a query means a different screen.key: 'static'or<NuxtPage page-key="static">never remounts across params. Param changes become in-page updates and the hooks never run.- Hash-only links are same-page. Leave them native; Nuxt scrolls to the target with the router's
scrollBehaviorType.
Nuxt's own test for "this navigation changes the page" is the same: the key differs or the matched components differ. View transitions and scroll use it.
Route lifetime under keepalive
definePageMeta({ keepalive: true }), <NuxtPage keepalive>, or app.keepalive wraps pages in <KeepAlive>. A page is then deactivated instead of unmounted and reactivated instead of remounted. DOM and state survive; the lifecycle does not.
- Deactivate is unmount.
onDeactivatedfires when the leave starts. Revert the page's context there. The root still playsonLeave, then moves to KeepAlive's off-document storage. - Stored pages measure zero. Do not create or refresh ScrollTrigger, Flip, or split instances while deactivated.
- Activate is mount.
onActivatedfires after the root is inserted, and also once afteronMountedon first mount. Setup runs again on DOM that holds settled or half-reverted values. Write initial state explicitly withsetorfromTo; afromtween that trusts a fresh node is wrong.onMounteddoes not run again. - The hooks run the same.
onBeforeEnterandonEnterfire on reactivation with the stored root asel;onLeavefires on deactivation. - Cached pages leak. Scope page-level CSS variables,
overflowlocks, andwill-changeto the page root, or undo them inonDeactivated. includeandexcludematch component names. Underfuture.compatibilityVersion: 5page component names are normalized to route names.
Without keepalive the page unmounts at leave start. Design for the keepalive case anyway; it costs nothing when unmount happens.
Nuxt app hooks
Register with useNuxtApp().hook(name, fn) in a plugin, or useRuntimeHook (3.14+) in a component so it unregisters with the scope. In navigation order:
page:loading:start: inrouter.beforeEach, before middleware. Also when thepageKeyprop changes. Fires for query-only changes and for navigations that later fail.page:start: the incoming page's Suspense went pending. Its setup and data are running; the old page is on screen.page:finish: that Suspense resolved. This is when the leave starts, not the end of the transition.page:loading:end: afterpage:finish, or on navigation failure or router error. Pair withpage:loading:startfor a global navigating state, lock, or cover.page:transition:finish: the outgoing page'sonAfterLeave. Underout-inthe new root has just been inserted and itsonEnterhas not run yet.page:view-transition:start: only withexperimental.viewTransition; passes theViewTransition.app:suspense:resolve: the root Suspense resolved on the client, the first-load counterpart ofpage:finish.app:mountedfires earlier, whenmount()returns.link:prefetch: a<NuxtLink>is about to prefetch.
Use these for app-level state: cover, lock, loading indicator, analytics, announcing. Use the transition hooks for anything that touches the page's elements. For work after the intro, onAfterEnter is the only hook that fires then.
Scroll and focus
Nuxt's default scrollBehavior (override it in app/router.options.ts) waits for page:loading:end, then for the outgoing leave to finish, then one animation frame, then scrolls: savedPosition on back and forward, the hash target if any, otherwise top. definePageMeta({ scrollToTop: false }), or a function of to and from, disables it per route. Same-path hash changes scroll at once.
So the window moves after the outro and around the first frame of the intro. Keep initial state independent of scroll position (transforms, autoAlpha), create ScrollTrigger instances after that scroll (onAfterEnter, or page:transition:finish plus a frame), and refresh in onAfterEnter. Do not add a second scroll reset.
Override scrollBehavior only to change the position logic. Keep the wait: returning a promise that resolves after page:transition:finish is Vue Router's documented way to delay scroll for a transition.
With a smooth scroller, follow Smooth scrolling. Create it once in a client plugin (plugins/scroll.client.ts) and share it through a small module holder or nuxtApp.provide, so the transition hooks and router options reach the same instance, which runs before hydration on a document that already holds the server HTML and before any page's onMounted, never in a page or layout. Lenis needs nothing else. ScrollSmoother needs its wrapper and content rendered by app.vue around <NuxtLayout>, fixed chrome outside them, and data-allow-mismatch="style" (Vue 3.5) on both so the inline styles it writes before hydration are not reported as mismatches. Map the table onto the hooks: stop() in onLeave, or at the click when the outro starts there. Nuxt scrolls after the leave's done plus one frame, so onEnter runs before the window moves; the sync point is a scrollBehavior in app/router.options.ts that keeps the default's wait (copy it from node_modules/nuxt/dist/pages/runtime/router.options.js), passes the resolved position to the scroller (scrollTo(position.top, { immediate: true }) for a saved or top position; scrollTo(position.el, { immediate: true, offset: -(position.top ?? 0) }) for a hash), and returns the same position for Vue Router to apply. A false result means no scroll and no sync. Then resize() and start() in onAfterEnter, after that hook created the page's triggers. scrollToTop: false returns false. Nuxt leaves history.scrollRestoration on auto until the first client navigation and takes manual from there (4.5; read pages/runtime/plugins/router.js), so reloads restore natively; leave it alone. A failed or cancelled navigation fires page:loading:end without page:finish; start() there, in the handler that releases the lock, but only when no page:finish followed the matching page:loading:start and no newer navigation is pending, since the hook fires for every navigation. The first client navigation receives START_LOCATION as from, and the default scrollBehavior scrolls at once in that case, before the leave; treat from.matched.length === 0 as unknown and keep the wait. The default's source imports Nuxt internals and vue-router; write the wait with public hooks instead of copying it.
Focus: in onAfterEnter, move focus to the page root or its heading only if focus is on body. Give the target tabindex="-1" and a visible focus style. autoAlpha: 0 is visibility: hidden and refuses focus, so focus at settled, not at insert. Keep <NuxtRouteAnnouncer> for the announcement.
Outro before navigation
Vue keeps the old page through onLeave, so an outro that merely plays before the old page disappears needs no interception. But Vue Router commits the URL before onLeave runs: by the time the hooks see the old root, the address bar already shows the destination and the incoming page has loaded. If the outro must play with the URL unchanged, which is what "outro before navigation" usually means, the hooks cannot do it; the outro has to run in a global route middleware that awaits it, and onLeave then only hands done over for an element that is already at its end state. Do not port a Next.js transition-aware link that prevents default, animates, then pushes; here it doubles the outro or races the hooks.
The ways to move the outro earlier:
- The outro must cover the load. By default the leave waits for the incoming page. To start the outro at click time, use
<NuxtLink custom v-slot="{ href, navigate }">, start the outro and callnavigate()together, and letonLeavecalldoneat once when the outro has already reached end state, or attachdoneto the running timeline. Make the page non-interactive from the click. Leave modified clicks, external URLs,target, anddownloadto the native anchor. - Nothing may load and the URL must wait until the outro ends. A global route middleware
awaits the outro on the client (import.meta.client && !useNuxtApp().isHydrating) and then returns. Route middleware runs afterpage:loading:startand before guards, chunk loading, the URL commit, and the incoming setup. Never await it on history navigation. The lock cannot live in this middleware: returningfalsefor a second click while the first is awaiting makes Vue Router cancel the first as well, since the pending navigation is now the second, and both are lost. Put the lock in front of the router, in the link component, so the second click never becomes a navigation. - A leave must be refused.
onBeforeRouteLeavereturningfalsecancels the navigation before anything moves. Use it for unsaved work, not for animation locks; returningfalseon back or forward also rewrites the URL back.
Shared elements need no interception. Follow Transition archetypes: captureShared in onBeforeLeave, where the old root is laid out and the new one is not yet inserted in either mode, and let the outro dim everything but that element. Keep the state in the transition module keyed by the destination route, and clear it in onAfterEnter and onEnterCancelled. playShared(state, target) in onEnter with the target queried inside el: under no mode the old element is still in the DOM, so Flip must be told which one. Nuxt's own scroll lands a frame after the leave's done, mid-morph, so first move the scroller to where the router will put the page (the top, or the hash target) with scrollTo(..., { immediate: true }); the later scrollBehavior then finds it there, and playShared corrects for the change since capture. A history navigation on the quiet path below captures nothing.
Back and forward
History navigation runs the same middleware, hooks, and transition as a click, and Vue Router does not tell a guard how a navigation started. What works:
useRouter().options.history.listen((to, from, info) => ...)fires forpopstatenavigations only, before the router runs, withinfo.directionofback,forward, or empty. Record the destination it reports and let the hooks compare it with the route they are entering; do not clear a boolean onpage:loading:end, which fires beforeonLeaveandonEnterand so wipes the flag before the hooks read it. Vue Router marks this API alpha; checknode_modules/vue-routerafter upgrades.- A
popstatelistener onwindowfires in the same dispatch, before any guard.event.hasUAVisualTransitionmeans the browser already animated a swipe gesture; skip the intro then, as Nuxt's own view-transition plugin does. scrollBehaviorreceivessavedPositiononly on history navigation, but that is after commit and after the leave.
Give history navigation a quieter path: a short outro or an immediate done, then intro-only with no travel, since the user is returning. The hooks still fire; do not skip done.
The bfcache does not apply to same-document navigation. A full reload of a Nuxt page is a fresh hydration and takes the first-load path.
Curtains and preloaders
Follow Transition archetypes. Both live in app.vue outside <NuxtLayout>, since pages and layouts leave.
- Curtain. The leave starts only after the incoming page resolved, so
cover()composed intoonLeavehides the swap, not the load. To cover before the URL changes, compose it into the outro that runs before navigation, as in Outro before navigation, whereonLeaveonly callsdone. The link records its request, curtain or shared element, in module state keyed by destination for that outro to read. Underout-inthe old root leaves atdoneandonEnterinserts the new one covered: write start values there and callreveal()fromonEnter, overlapping the intro, withdonestill bound to the intro. A history navigation on the quiet path never covers; reveal only if still closed. A route withpageTransition: falseruns no hooks and gets no curtain. A failed navigation firespage:loading:endwithoutpage:finish: reveal there if closed, with the lock release. - Preloader. The
app.head.scriptinline script decides before paint: on a first visit it sets a root mark beside the pre-paint mark and records the visit insessionStorage;app.vuerenders the preloader markup on every request and CSS shows it only under that mark.finish()runs fromapp:suspense:resolveonce deadline-bounded fonts and hero media report, and the page's first-load intro waits on it instead of playing fromonMounted. Route changes never show it again.
View Transitions
experimental.viewTransition: true (or 'always', or { enabled, types }) calls document.startViewTransition in router.beforeResolve for navigations that change the page, runs the route swap and Suspense inside its update callback, and finishes it on page:finish. true skips it under prefers-reduced-motion; 'always' does not. Per page: definePageMeta({ viewTransition }) with enabled, types, toTypes, and fromTypes (4.4+; functions of to and from work here, not in nuxt.config). Style with ::view-transition-* and html:active-view-transition-type(...). page:view-transition:start hands you the ViewTransition to read or add types. A popstate with hasUAVisualTransition skips it.
Vue's <Transition> has no View Transitions mode of its own; this plugin is the integration.
Rules for mixing:
- Never run the
pageTransitionhooks and a view transition on the same navigation. The DOM is frozen while the browser captures, and Suspense resolves while the old page is still leaving, so the new snapshot is wrong or blank. Follow Nuxt's recipe: a global middleware setsto.meta.pageTransition = falseandto.meta.layoutTransition = falsewhendocument.startViewTransitionexists, and your GSAP path lives in the page'sonMounted. - One engine per element per transition. Name only elements that morph, set the name only for that navigation, and clear it.
- GSAP intros for elements the transition touched start after
transition.finished; take theViewTransitionfrompage:view-transition:start. Elements outside it can start atonMounted. - Data fetched in page setup runs inside the frozen update; Nuxt's docs list this as a known issue. Prefer prefetched or cached data on routes that use it.
- Add
::view-transition { pointer-events: none }. - Reduced motion covers both:
truealready skips the browser side; take the GSAP reduced path.
Use view transitions for a shared-element morph or a whole-page slide keyed by types. Use the GSAP hooks for everything sequenced, interruptible, or scroll-linked. Pick per route, not per element.
SSR and first paint
GSAP runs only in the browser.
- Register GSAP and plugins in a
.client.tsplugin or theuseGSAPcomposable fromgsap-frameworks; guard module-level side effects withimport.meta.client. onMounted,onActivated, and the transition hooks never run on the server.<script setup>top level andwatchEffectdo.<ClientOnly>renders its slot only after mount and itsfallbackon the server; the slot is not in the server HTML at all. Use it for a canvas or a plugin-driven widget that cannot render as HTML, not to hide a page.- With
ssr: falsethe client renders everything and first-paint hiding is unnecessary.
Hydration happens after the server HTML has painted, so a first-load intro needs initial state before first paint, applied by CSS under a mark that only JavaScript sets:
- Add an inline script through
app.head.scriptinnuxt.configwithinnerHTMLandtagPosition: 'head'. It runs before the body parses. It sets a root mark such asdata-motion="js"on<html>, and skips the mark underprefers-reduced-motionso that path paints settled. Do not put adata-phaseon<html>: the page root owns that attribute, and a second one on the document root reads to CSS, tests, and observers as another page changing phase. - Vue hydrates
#__nuxt, not<html>, so attributes set there cause no hydration mismatch. - One CSS rule hides intro targets only under the mark and only while the page root is in its initial phase, such as
html[data-motion="js"] [data-page][data-phase="initial"] [data-intro] { visibility: hidden }. The server renders the page root withdata-phase="initial", so the rule applies from first paint and releases when the controller advances the phase. Usevisibility, notdisplay, so layout is measured with the elements in place. - The first
onMountedregisters recovery, writes start values, attaches completion handlers, then advances the phase and plays. A setup exception rolls back partial changes before revealing content. - Keep the early failsafe active until preparation succeeds and the intro can run, not merely until a controller registers. Register rollback before setup, preserve the original deadline, and take the settled path after recovery. Follow Initialization and recovery.
- Content stays readable without JavaScript: no mark, no rule.
onPrehydrateinlines a callback before</body>, after the content has parsed and possibly painted. It is for DOM fixes right before hydration, not for the pre-paint mark.
Route navigations do not go through this: onBeforeEnter runs before the incoming root is inserted.
Cases to design for
- Navigation requested mid-intro.
- Two destinations clicked before the first resolves.
- Back or forward, including a swipe the browser already animated.
- A keepalive page shown again with settled DOM.
- A change of only query or hash.
- A slow incoming page: the old page stays on screen. Decide what the user sees for two seconds.
- A layout change, where the page transition does not run.
- A leave that reverts a pinned ScrollTrigger.
- Reload on a slow network: no settled flash, then a clean intro.
- Development hot reload, which re-runs setup on a live page.
Each case must end on one visible, interactive page with stable layout, no stale inline styles, and every done called.