Sub topic 3
Which navigations get a transition
This chapter assumes you know that links trigger full page loads, that each URL returns its own document, and that you've seen @view-transition { navigation: auto; } from the previous chapter. It also assumes you understand the same-origin policy at a basic level.
Adding the opt-in rule does not animate every navigation. The browser checks each navigation and runs a transition only if the navigation qualifies. Otherwise it loads the page normally, with no error and no animation.
The eligibility checks
A navigation gets a transition only when all of these are true:
- The old and new documents are same-origin. They must share scheme, host, and port. Going from
https://example.com/tohttps://blog.example.com/is cross-origin, so there is no transition. - Both documents opt in. The page being left and the page being opened each need the
@view-transitionrule. - The navigation type is supported. The browser animates link clicks, form submissions, script navigations such as
location.href = '/next', and back/forward traversal. It does not animate reloads. It also skips navigations started from browser UI, such as typing in the address bar or choosing a bookmark. - Any redirects stay same-origin. A redirect chain that passes through another origin cancels the transition.
- The new page renders in time. If the new document takes too long to produce its first render (Chrome allows about four seconds), the browser skips the transition and shows the page.
Observing eligibility from script
The pageswap event fires on the old page just before it goes away. The pagereveal event fires on the new page before its first render. In each event, the viewTransition property is null when no transition will run for that navigation. This gives you a reliable test:
window.addEventListener('pageswap', (event) => {
console.log(
event.viewTransition ? 'Leaving with a transition' : 'Leaving without a transition'
);
});
window.addEventListener('pagereveal', (event) => {
console.log(
event.viewTransition ? 'Arriving with a transition' : 'Arriving without a transition'
);
});Paste this into both pages, then try each kind of navigation. Click a link, press Back, reload, and type a URL in the address bar. The console shows which ones qualify.
Common errors
- Only one page has the rule. Opting in on the list page but not the detail page produces no transition, and nothing warns you. Put the rule in a stylesheet that every page shares.
- Testing by reloading. A reload never animates, so you may think the feature is broken. Navigate by clicking a link instead.
- Testing across subdomains or ports.
localhost:3000tolocalhost:4000is cross-origin. Keep all your pages on one origin during development. - Expecting a fallback animation. When a navigation is ineligible, the page simply loads. Design so the site works fully without any transition.
Exercise
A user on https://shop.example/cart clicks a link to https://shop.example/checkout. Both pages contain the @view-transition rule, but the user sees no animation. Name two possible causes from this chapter.
Show answer
Any two of these: the checkout response redirected through another origin before landing on /checkout; the new page took too long to render, so the browser skipped the transition; the navigation was not a supported type (for example, the user reloaded or used the address bar instead of the link). Also check that the rule is actually served on both pages and not blocked by a stylesheet that fails to load.