Cross-document view transitions let the browser animate between two normal page loads with one line of CSS, @view-transition { navigation: auto; }, and no JavaScript. Astro’s <ClientRouter /> instead intercepts navigation, fetches the next page and swaps the DOM itself, which adds a small router script but gives you persisted islands, lifecycle events, fallbacks for older browsers and programmatic navigation. For most content sites, the native CSS approach is now the better default; use ClientRouter when you need state that survives navigation.
Key Takeaways
- Native cross-document view transitions need only CSS and work with full page loads, so every script and analytics tag behaves as usual.
- Browsers without support simply navigate without animation, which is a safe fallback.
<ClientRouter />addstransition:persist, lifecycle events,navigate()and an animated fallback for unsupported browsers.- With
ClientRouter, scripts don’t re-run on navigation; you move setup code to theastro:page-loadevent.transition:nameoutputs a plainview-transition-name, so it is useful in both approaches.
What native cross-document view transitions are
The View Transitions API started as a same-document feature for single-page apps. The cross-document version extends it to regular multi-page sites: when a visitor clicks a same-origin link and both pages opt in, the browser snapshots the old page, loads the new one, and animates between them. A full page load still happens; the browser just hides the jump.
Opting in is one CSS rule on both pages:
/* src/styles/global.css */
@view-transition {
navigation: auto;
}
That gives a cross-fade between pages. To animate a specific element, such as a card image that grows into a hero image, give it the same view-transition-name on both pages:
/* src/styles/global.css */
.post-hero {
view-transition-name: post-hero;
}
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 200ms;
}
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none;
}
}
Chromium-based browsers and Safari support cross-document view transitions. Check MDN’s view transitions page for the current status in other browsers. Where it isn’t supported, the CSS is ignored and navigation works normally.
What Astro’s ClientRouter does
<ClientRouter /> is Astro’s client-side router, imported from astro:transitions. Add it to the <head> of a shared layout and Astro takes over same-origin link clicks: it fetches the next page, runs the view transition with the browser API where available, and swaps the document contents without a full reload.
---
// src/layouts/Base.astro
import { ClientRouter } from 'astro:transitions';
---
<html lang="en">
<head>
<meta charset="utf-8" />
<ClientRouter />
</head>
<body>
<slot />
</body>
</html>
Because Astro controls the navigation, it can do things a browser-driven transition can’t:
transition:persistkeeps an element or island, with its state, across pages. A video, an audio player island or a search box keeps playing or keeps its text.- Lifecycle events:
astro:before-preparation,astro:after-preparation,astro:before-swap,astro:after-swapandastro:page-loadlet you hook into each stage. navigate()fromastro:transitions/clienttriggers a routed navigation from code.- Fallbacks: in browsers without the View Transitions API, the
fallbackprop (animateby default, orswapornone) still gives an animated or instant swap. - Prefetching is turned on by default for pages that use the router.
Cross-document view transitions vs ClientRouter at a glance
Native CSS (@view-transition) |
Astro <ClientRouter /> |
|
|---|---|---|
| JavaScript shipped | None | Router script on every page |
| Navigation | Full page load | Fetch and DOM swap |
| Setup | One CSS rule on both pages | One component in the layout |
| Unsupported browsers | Normal navigation, no animation | Animated fallback (configurable) |
| Persist state across pages | No | transition:persist |
| Scripts on navigation | Run on every page load as usual | Bundled scripts run once; use astro:page-load |
| Third-party scripts | Work unchanged | May need re-initialisation |
| Lifecycle hooks | pageswap, pagereveal browser events |
Five Astro events plus browser events |
| Programmatic navigation | location.href |
navigate() |
| Named element transitions | view-transition-name |
transition:name (or the CSS property) |
| Built-in animations | Write your own CSS | fade, slide, none, initial via transition:animate |
| Best for | Blogs, docs, marketing sites | App-like sites, persistent media, shared state |
The script problem with ClientRouter
This is the trade-off that catches most people. With a full page load, every <script> runs on every page. With ClientRouter, the page never fully reloads. Astro’s bundled module scripts run once, on first load, and are not re-executed after a swap. Code that binds event listeners to elements in the page stops working on the second page you visit.
The fix is to run setup on Astro’s page-load event, which fires on the first load and after every navigation:
---
// src/components/MobileNav.astro
---
<button class="menu-toggle" aria-expanded="false">Menu</button>
<nav class="menu" hidden><slot /></nav>
<script>
function setup() {
const button = document.querySelector<HTMLButtonElement>('.menu-toggle');
const menu = document.querySelector<HTMLElement>('.menu');
if (!button || !menu) return;
button.addEventListener('click', () => {
const open = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', String(!open));
menu.hidden = open;
});
}
document.addEventListener('astro:page-load', setup);
</script>
Third-party widgets, analytics tags and embeds that assume a fresh page can break in the same way and may need a manual page view or re-initialisation. Adding data-astro-rerun to an is:inline script forces it to run again after every navigation, which is another detail to learn. None of this applies to native cross-document transitions, because each navigation is a real page load. The island or script guide explains how Astro bundles component scripts.
Using Astro directives with native transitions
You can keep Astro’s transition:name directive without the router. It outputs a CSS rule that sets view-transition-name on the element, which is exactly what native cross-document transitions read.
---
// src/pages/blog/[slug].astro
const { post } = Astro.props;
---
<img
src={post.cover}
alt=""
transition:name={`cover-${post.slug}`}
/>
Put the same transition:name on the matching image in your blog index, and the image morphs between the two pages in supporting browsers. Built-in transition:animate animations for forward navigation also compile to plain ::view-transition-* CSS. Direction-aware back animations rely on an attribute the router sets, so they need ClientRouter.
Performance and SEO
Native transitions add no JavaScript, and every page is a normal document load, so caching, prefetching (via Astro’s prefetch option or Speculation Rules) and Core Web Vitals measurement all behave exactly as they would without transitions. The Astro prefetch tip shows how to make the next page load almost instant, which pairs well with a native cross-fade.
ClientRouter is small, but it is still JavaScript on every page, and it changes how navigations are measured. Field metrics such as INP and LCP are recorded differently for soft navigations, and some tools only report the first page load. Neither approach affects SEO directly: search engines crawl each URL as a normal HTML page in both cases.
Choose native transitions or ClientRouter
Choose native cross-document view transitions if you build a blog, documentation, portfolio or marketing site, want transitions with zero JavaScript, and are fine with no animation in browsers that don’t support them yet. This is the right default for most Astro sites in late 2026.
Choose <ClientRouter /> if something must survive navigation (a playing video, an audio player, a chat widget, form state), you need lifecycle hooks or navigate(), or animated transitions in every browser are a requirement.
Choose neither if your pages are fast and simple and motion adds nothing. Instant navigation with prefetching is often the best transition.
Komal themes use regular page loads by default, so you can add the @view-transition rule to the global stylesheet and get native transitions without touching any component scripts. See the styling docs for where each theme’s global CSS lives, or browse portfolio and agency themes, where page-to-page motion is most common. For the full ClientRouter API, read the Astro view transitions docs.
FAQ
What are cross-document view transitions?
They are browser-native animations between two separate page loads on the same origin. Both pages opt in with the CSS rule @view-transition { navigation: auto; }, and the browser animates from the old page to the new one without any JavaScript. Elements with matching view-transition-name values animate individually.
Do I still need Astro’s ClientRouter?
Only if you need what it adds: state that persists across pages with transition:persist, lifecycle events, navigate(), or animated fallbacks in browsers without view transition support. For simple page-to-page animations, the native CSS approach is enough.
Why do my scripts stop working with ClientRouter?
With ClientRouter, pages are swapped without a full reload, and Astro’s bundled scripts run only once. Listeners attached on the first page don’t exist on the next. Move setup code into a function and call it on the astro:page-load event, which fires after every navigation.
Which browsers support cross-document view transitions?
Chromium-based browsers such as Chrome and Edge, and Safari, support cross-document view transitions. Support in other browsers is in progress, so check MDN or caniuse for the current state. Browsers without support ignore the CSS and navigate normally.
Can I use transition:name without ClientRouter?
Yes. Astro compiles transition:name into a view-transition-name CSS rule, which native cross-document view transitions use. Forward transition:animate animations also compile to plain CSS, while direction-aware back animations depend on the router.