Sub topic 3
Which navigations get a transition
This chapter assumes you can write @view-transition and know that a multi-page site loads a new document for each URL. It covers which navigations the browser animates, which it skips, and how to check what happened on a page you control.
Two documents, one decision
A cross-document view transition involves two documents: the page being left (the old page) and the page being opened (the new page). The browser decides whether to animate by checking both. If any check fails, the navigation proceeds as an ordinary page load with no animation and no error on screen.
This is deliberate. A transition is an enhancement, so the browser never delays or breaks a navigation to get one. When in doubt, it skips.
The checks fall into four groups:
- Was the navigation started in a way that is eligible?
- Are the two documents same-origin, including any redirects?
- Did both documents opt in?
- Did the new page become ready in time, and did the transition set up without errors?
How the navigation starts
Only navigations that the page itself initiates, or that history traversal initiates, are eligible.
| Navigation | Eligible? | Notes |
|---|---|---|
| Clicking a link | Yes | The common case. |
| Submitting a form | Yes | The transition runs when the response document arrives. |
| Script navigation, such as `location.href = "/next"` | Yes | Treated like a link click. |
| Back and forward buttons | Yes | Both history entries must opt in. |
| Typing a URL in the address bar | No | Started by browser UI, not by the page. |
| Opening a bookmark | No | Same reason. |
| Reload | No | Browser UI action, and the page is not changing. |
| Link opened in a new tab or window | No | There is no old page in that tab to snapshot. |
Back and forward are the case that surprises people. The traversal is animated using the same rules as a forward navigation. The page you return to must also contain the @view-transition rule, or it is skipped. If you add the rule to your site today, entries from yesterday's cached copies of old pages may still lack it until they reload.
The same-origin rule
Both documents must have the same origin, meaning the same scheme, host, and port. Moving from https://example.com/ to https://example.com/about qualifies. These do not:
https://example.comtohttps://blog.example.com(different host)https://example.comtohttp://example.com(different scheme)https://example.comtohttps://example.com:8443(different port)- Any move to another site entirely
Redirects count too. If /old redirects within your origin to /new, the transition can still run. If the chain passes through a different origin, such as a tracking or sign-in domain, the navigation is not eligible, even if it finally lands back on your site. This matters for links that go through a click-tracking redirect. Moving that redirect to a path on your own origin restores eligibility.
Only the top-level document takes part. A navigation inside an <iframe> does not trigger a transition, and an iframe inside a transitioning page is not animated separately.
Both pages must opt in
The old page needs the rule so the browser knows to snapshot it. The new page needs it so the browser knows to animate to it.
@view-transition {
navigation: auto;
}The usual way to guarantee this is to put the rule in a stylesheet that every page loads. Two details cause most "it only works one way" bugs:
- A page that links the shared stylesheet from a
<link>in<head>is read before first render, so the rule is seen in time. A rule added later by JavaScript, or in a stylesheet that loads late, may be too late for the new page. - If only the list page opts in, going from list to detail does nothing, and so does going back.
Timing and the skip conditions
Even when a navigation passes all the checks, the transition can still be skipped:
- The new page is too slow. Chrome gives the new page a short window, about four seconds, to reach its first render. After that the transition is skipped and the page shows normally.
- The page is hidden. If the tab is in the background when the transition would start, there is nothing to show.
- Duplicate names. Two elements with the same
view-transition-nameon one page make the transition fail, and the browser falls back to a plain navigation. You will meet this in the shared-element chapters. - Script cancels it. Your own code can call
skipTransition(), covered below.
A skipped transition is silent for visitors. For you, it shows up as a null or a finished-with-error transition object.
Observing the decision from JavaScript
Two events report what happened. pageswap fires on the old page just before it is replaced, and pagereveal fires on the new page just before its first frame. Both expose a viewTransition property. It is null when the navigation is not getting a transition.
Add this small logger to every page while you test:
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition) {
console.log('pageswap: no transition for this navigation');
return;
}
const to = event.activation ? event.activation.entry.url : 'unknown';
console.log('pageswap: transition starting, going to', to);
});
window.addEventListener('pagereveal', (event) => {
if (!event.viewTransition) {
console.log('pagereveal: no transition on arrival');
return;
}
console.log('pagereveal: transition running');
event.viewTransition.finished
.then(() => console.log('pagereveal: transition finished'))
.catch((error) => console.log('pagereveal: skipped or failed', error.name));
});Console output does not carry across a navigation by default. In Chrome DevTools, turn on "Preserve log" in the Console panel so that the pageswap line from the old page stays visible after the new page loads.
Reading the output tells you where a check failed:
pageswapreports no transition: the old page did not opt in, or the navigation type or origin is ineligible.pageswapreports a transition butpagerevealdoes not: the new page did not opt in, or a cross-origin redirect intervened.pagerevealreports a transition that ends in an error: the page was too slow, or names were duplicated.
Opting out of individual navigations
Because the rule is global, you will sometimes want specific navigations to be plain. Call skipTransition() from either event.
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition || !event.activation) return;
const to = new URL(event.activation.entry.url);
// Skip the animation when leaving for the checkout flow.
if (to.pathname.startsWith('/checkout')) {
event.viewTransition.skipTransition();
}
});The page still navigates normally. Only the animation is cancelled. Also, skipTransition() on an already-finished transition does nothing, so it is safe to call defensively.
Checking support before you rely on it
The events and the rule are not available everywhere. Use feature detection if your scripts depend on them:
const supportsCrossDocument =
'CSSViewTransitionRule' in window && 'PageRevealEvent' in window;
if (supportsCrossDocument) {
window.addEventListener('pageswap', (event) => {
// Safe to use event.viewTransition here.
});
}In a browser without support, the CSS rule is ignored, and every navigation takes the "plain navigation" path from the diagram. Nothing needs to change in your markup.
Common mistakes
- Testing by pressing reload or typing the URL. Neither is eligible.
- Adding the rule to only one page template.
- Routing links through a cross-origin redirect for analytics.
- Expecting a transition when opening a link in a new tab.
- Assuming that back works because forward works. The page you return to must opt in as well.
Exercise
A visitor clicks a link on https://shop.example/ to https://shop.example/cart. Both pages include the @view-transition rule. The link first goes through https://track.example/out?to=/cart, which redirects back to /cart. No transition runs. Why, and how would you fix it?
Show answer
The redirect passes through a different origin (track.example), so the navigation is not eligible for a cross-document transition even though it ends on the same origin as it started. The fix is to link directly to /cart, or to route the tracking redirect through a path on shop.example so the whole chain stays same-origin. You can confirm the diagnosis with the pageswap and pagereveal logger: pageswap may report a transition, but pagereveal will report none on arrival.
Exercise
Name three kinds of navigation that never get a cross-document transition regardless of CSS, and one that does get one only if both history entries opt in.
Show answer
Never eligible: typing a URL in the address bar, opening a bookmark, and reloading (also acceptable: opening a link in a new tab, or navigating across origins). Eligible only if both entries opt in: going back or forward in history, because the page being returned to must also include the @view-transition rule.