Sub topic 14
Performance and Common Pitfalls
This chapter assumes you have enabled cross-document transitions with @view-transition, styled the ::view-transition-* pseudo-elements, and used pageswap and pagereveal to name elements per navigation. It covers what makes a transition slow or janky, and why transitions sometimes silently don't run.
Where the cost comes from
A cross-document transition costs more than a plain navigation in three places.
- Snapshots. The browser captures the old page as an image. The new page is a live representation that is composited during the animation. Every element with a
view-transition-namegets its own snapshot pair and its own group, in addition to therootsnapshot. Each snapshot uses graphics memory, so memory use grows with the number of named elements and with their pixel area. - Delay. The old page stays on screen until the new page is ready to render. A slow server response, a large render-blocking stylesheet, or a long script delays the whole transition.
- Animation time. The old page remains visible while the animation runs. While the pseudo-elements are on screen, the page underneath generally doesn't respond to clicks. A one-second slide makes every navigation feel one second slower.
The default animations only change opacity and transform. These can run on the compositor, which is the part of the browser that moves and blends layers without redoing layout. If you write custom keyframes, keep to those two properties for the same reason. The one exception is size and position changes on shared elements, which the browser animates for you on the group.
Pitfall: duplicate transition names
A view-transition-name must be unique within a document at the moment the browser captures it. If two elements share a name, the transition is skipped and the navigation completes with no animation. You get no visible error.
This usually happens on list pages. Putting view-transition-name: post-image in a stylesheet rule that matches every card thumbnail gives all of them the same name. The fix is the pattern from the earlier chapter. Set the name only on the one element involved in the current navigation, from pageswap and pagereveal, and remove it afterwards.
Two details are easy to miss:
- On the page you leave, the name only needs to exist until the old snapshot is taken. On the page you arrive at, set it before the first rendered frame, which is why
pagerevealis the right place. - A name left behind on the list page can collide the next time you navigate. Resetting to
noneafterevent.viewTransition.finishedprevents this.
You can audit a page from the DevTools console:
const counts = new Map();
for (const el of document.querySelectorAll('*')) {
const name = getComputedStyle(el).viewTransitionName;
if (name === 'none') continue;
counts.set(name, (counts.get(name) ?? 0) + 1);
}
console.table([...counts].map(([name, count]) => ({ name, count })));
// Any count above 1 means the transition will be skipped.Pitfall: naming too much
Names are cheap to write and expensive to render. Giving every card, button, and heading a name produces dozens of groups, each with two snapshots, and each is animated on every navigation. A good rule is to name only elements that appear on both pages and visibly change position or size, or that must stay still while the rest animates (like a sticky header).
Large named elements cost the most. A full-width hero image at high pixel density has a snapshot of that full size. If the shared element is only a thumbnail morphing into a hero, consider whether the morph is worth that memory on low-end phones.
Pitfall: waiting for the new page
The transition starts when the new page is ready to render. A render-blocking resource, such as a large stylesheet in <head> or a synchronous script, delays the animation. Browsers also have a time limit, and if the new page takes too long they abandon the transition and show the page directly.
Two strategies help:
- Keep the critical CSS small and inline what the first screen needs.
- Use
rel="expect"when the opposite problem occurs: the page renders too early, before the element you want to animate to exists.
<head>
<link rel="expect" href="#main" blocking="render">
</head>
<body>
<header>…</header>
<main id="main">…</main>
</body>Blocking is a trade-off. It improves visual stability but delays the first render. Point it at an element that appears early in the document. If it points at something near the end of a long page, the browser waits for the parser to reach it. If the id never appears, the block ends only when the browser's own time limit fires. Also remember this hint isn't supported everywhere, so the page must still work without it.
Pitfall: slow or long animations
Transition animations delay the visible result of a click. Keep them short. Roughly 150 to 300 ms suits page-level motion, and anything above about 400 ms tends to feel like lag on a site people navigate often.
Don't animate expensive properties such as filter: blur() on large snapshots, since they are re-rendered each frame. Prefer opacity and transform.
/* Short, compositor-friendly page animation */
::view-transition-old(root) {
animation: 150ms ease-in both fade-out;
}
::view-transition-new(root) {
animation: 200ms ease-out both fade-in;
}
@keyframes fade-out {
to { opacity: 0; }
}
@keyframes fade-in {
from { opacity: 0; }
}Pitfall: shared images that stretch
By default, the old and new snapshots of a named element are stretched to fit the group, and the group animates between the two sizes. If the thumbnail is square and the hero is wide, the image distorts mid-flight. Make the snapshot images fill the group and crop instead:
::view-transition-old(post-image),
::view-transition-new(post-image) {
height: 100%;
object-fit: cover;
}Check the result in slow motion (see below). Whether cropping or stretching looks better depends on the content.
Pitfall: stacking and fixed elements
Named elements are lifted out of the normal page and painted in the transition's own layer above everything else, in document order. A sticky header that is not named can end up under an animating named image. Name the header too, and set its group animation to zero so it doesn't move:
.site-header {
view-transition-name: site-header;
}
::view-transition-group(site-header) {
animation-duration: 0s;
}Without a name, a sticky header is part of the root snapshot and fades with the rest of the page, which looks like flicker.
Pitfall: motion for everyone
Large motion can cause discomfort for some users. Always include the prefers-reduced-motion rule from the accessibility chapter. Navigation itself continues to work, and only the animation is removed. Test it by enabling the "reduce motion" setting in your operating system or by emulating the media feature in DevTools.
Pitfall: unsupported browsers and scope
Only same-origin navigations qualify. A link to another domain, or a redirect that ends on another origin, gets no transition. Browsers without support ignore @view-transition and the events never fire, so the site works as a normal multi-page site. Don't write code that assumes event.viewTransition or event.activation exists. The handlers in this tutorial check for them and return early.
Diagnosing a transition that doesn't run
| Symptom | Likely cause | Check |
|---|---|---|
| No animation at all | One page lacks `@view-transition` | Confirm the rule on both the source and destination pages |
| No animation, only on some links | Cross-origin link or redirect | Compare origins and follow the redirect chain |
| Works first time, fails after going back | Stale `view-transition-name` on the list page | Reset names after `finished`, and run the console audit above |
| Animation starts late | Render-blocking CSS or slow response | Performance panel, network waterfall |
| Whole page flashes | Sticky element not named, or `rel="expect"` blocking too long | Name the element; move the expect target earlier |
| Image looks squashed | Different aspect ratios between pages | Add `object-fit: cover` to the old and new pseudo-elements |
| Janky on phones | Too many or too large named elements | Remove names, shorten animations |
To see what is happening, open the Animations panel in Chrome DevTools, slow playback to 10%, and inspect the ::view-transition tree in the Elements panel while the animation is paused. Check the Performance panel for long tasks before the first paint of the new page.
Exercise
A list page has 40 cards. Each thumbnail has the CSS rule view-transition-name: post-image. Clicking any card navigates to the post page, but no animation plays. Why, and how do you fix it?
Show answer
All 40 thumbnails share the same name, and a view-transition-name must be unique in the document at capture time. The browser skips the transition. Remove the static rule. In a pageswap handler, set the name only on the clicked card's thumbnail, and in a pagereveal handler do the same when coming back, resetting it to none after event.viewTransition.finished.
Exercise
Your transition feels slow after you add a one-second slide and a blur on the root snapshot. Name two changes that would improve it.
Show answer
Shorten the animation to roughly 150 to 300 ms, and remove the blur so that only opacity and transform are animated. Both reduce the time the user waits and the per-frame rendering work. Reducing the number or size of named elements would also help.