Same-document navigation
Read this only when the site fetches pages and swaps content without reloading the document.
Decide whether to own a router
A same-document router must intercept links, fetch and parse HTML, swap content, update the title and URL, handle back and forward, restore scroll, move focus, re-run page scripts, cancel stale requests, and announce route changes. Swup, Barba, and Taxi do all of that and expose hooks where GSAP fits. Prefer one unless the project already owns a router or the user asks for one.
With a library, everything below still applies. Map its leave and enter hooks to outro and intro, return the timeline's promise so the library waits, and keep the lock, the cover, and cleanup as described.
Page lifecycle
mount → initial state → intro → settled → outro → end state → unmount
- Outro. The link is intercepted and the outgoing content plays its outro on live DOM while the next page is fetched.
- End state. Outgoing targets finalize and completion runs once. Raise the cover over the route container.
- Unmount and mount. The router replaces the container's children with the parsed content, updates the title, and pushes the URL.
- Initial state. The new content is in the DOM, sized, and measured. Start values are written behind the cover.
- Intro. Lower the cover and play one timeline to settled.
- Settled. Clear temporary styles. CSS owns the page.
One navigation at a time. The first accepted destination wins through outro, fetch, swap, and cover. Release the lock when the intro starts, not when it ends, so a click mid-intro kills the intro and starts the outro from current values. Abort the in-flight fetch with an AbortController when a new destination is accepted.
A killed timeline never resolves its promise. Wrap await in a helper that also resolves on onInterrupt and returns whether the timeline completed.
Intercepting links
Listen on the document for clicks and resolve the closest a[href]. Leave native: modified clicks, non-primary buttons, other origins, target, download, mailto and tel, hash-only and same-location links, and links marked to opt out. Use pushState for accepted links and replaceState for links marked replace.
Where the Navigation API exists, one navigate listener with event.intercept({ handler }) covers link clicks, form submissions, and programmatic navigation, and reports navigationType. Keep the click path as the fallback for browsers without it.
Fetching and swapping
- Fetch with an
Accept: text/htmlheader and parse withDOMParser. Take the route container's content, thetitle, and anybodyattributes such asdata-pagethat pages differ on. - Scripts inside fetched HTML do not execute when inserted. Register page setups in one module keyed by
data-pageand call the matching one after the swap. Do not rely on inline page scripts. - Stylesheets some pages add must be loaded before those pages are revealed. Append the
linkand wait for itsloadevent. - Keep one route container. Persistent chrome lives outside it and never reloads. Its state, timelines, and listeners survive navigation, which is the reason to go same-document at all.
- Overlap old and new only inside a reserved wrapper so one box owns the geometry. Hold the container's height during the swap, or animate it between measured sizes on purpose.
- Keep the cover inside the route container, not fixed to the viewport, unless the effect is a full-screen wipe on purpose.
History, scroll, and focus
- Set
history.scrollRestorationtomanualand own it. On push, scroll to top or to the hash target after the swap. Save the scroll position in the history state before leaving sopopstatecan restore it after the swap, then refresh ScrollTrigger.manualalso turns off the browser's restore for a reload or a back and forward into a fresh document, so restore from the saved state at boot too. - Back and forward never run an outro. On
popstatethe URL has already changed. Fetch, swap, and run initial state and intro only, or wrap the swap indocument.startViewTransitionso it is not a hard cut. - After the swap, move focus to the route container or its main heading if focus was on removed content or on
body. Give the targettabindex="-1"and a focus style that fits the design. - Update
document.titleand announce the new page through a live region. - Do not transition to the current URL. Treat search-param changes as page changes only when they mean a different screen.
Smooth scrolling, curtains, and shared elements
One scroller per document means one per router lifetime: create it with the router, outside the route container, never in a page setup. Map Smooth scrolling onto the router's own steps: stop() as the outro starts, restore or reset the native position after the swap as above and then scrollTo(position, { immediate: true }), resize() once the page setup has created its triggers, start() at settled. A failed fetch falls back to location.assign; an aborted fetch that leaves the page current calls start() where it releases the lock. With Swup, Barba, or Taxi, put the same calls in the library's leave, content-replace, and enter hooks; read the installed version's hook list rather than memory, since names changed between majors. A same-page hash link is a scrollTo(hash), not a navigation.
A curtain from Transition archetypes is the one case for a fixed cover: it lives beside the route container in persistent chrome, cover() composes into the outro, the swap runs on its completion, and reveal() runs after start values are written on the new content, overlapping the intro. popstate never covers.
Capture a shared element with captureShared as the outro starts, keep the state in the router module keyed by destination, clear it when the navigation settles or is replaced, and playShared(state, target) after the swap once the target is measured and visible. The old node left with the swap, so nothing stale can match; pass the target anyway. Flip cannot cross a document load: a full-document site uses view-transition-name instead, see Combining with GSAP.
Cleanup on unmount
Before replacing the container's children:
- Revert the page's GSAP context, which kills its tweens, triggers, and splits and clears their inline styles.
- Remove listeners, observers, timers, and plugin instances the page created.
- Undo anything the page changed outside its container: scroll locks, root attributes, theme, body classes.
- Do not kill triggers globally. Chrome and the next page own theirs.
Combining with same-document View Transitions
document.startViewTransition(() => swap()) snapshots the old content, runs the swap, and animates to the new snapshot. Use it for history navigation and plain crossfades. The rules are the same as cross-document: one engine per element, GSAP intros after finished for touched elements, and ::view-transition { pointer-events: none }.
Cases to design for
- Navigation requested mid-intro.
- Two destinations clicked quickly.
- A fetch that fails or returns something that is not HTML. Fall back to
location.assign. - A slow fetch. The cover stays up; show a hint after a delay.
- Back or forward with no outro.
- A page that changes body classes, theme, or scroll lock.
- A page setup that should run on every visit versus once per session.