Sub topic 2
What the browser does: snapshots, update, animate
This chapter assumes you can write basic JavaScript with promises, and that you know what a CSS pseudo-element is. Here you will follow what the browser does between your call to document.startViewTransition() and the finished animation.
The four phases
A view transition is a sequence the browser runs for you. You supply only the DOM update.
- Capture the old state. The browser takes a snapshot of how the page looks right now.
- Update the DOM. Rendering is paused, and your callback runs. If it returns a promise, the browser waits for it.
- Capture the new state. Once the callback finishes, the browser records the updated page.
- Animate. The browser builds a tree of pseudo-elements from the two captures and animates between them.
Snapshots are not the live page
The old state is a static image. It does not change while the animation runs. The new state is a live view of the updated element, so a video or a CSS animation inside it keeps playing.
While your callback runs, the browser holds back rendering. The user sees the old frame and cannot interact with the page. Keep the callback short. If it waits on a slow network request, the page looks frozen during that time. Fetch your data first, then start the transition with a callback that only touches the DOM.
Watching the phases
The object returned by startViewTransition() has three promises. This example logs each phase. A later chapter covers them in detail.
function updateView() {
document.querySelector('#content').textContent =
new Date().toLocaleTimeString();
}
async function run() {
if (!document.startViewTransition) {
updateView();
return;
}
const transition = document.startViewTransition(() => {
console.log('2. update callback runs');
updateView();
});
console.log('1. old state is being captured');
await transition.updateCallbackDone;
console.log('3. DOM updated');
await transition.ready;
console.log('4. pseudo-elements exist, animation starts');
await transition.finished;
console.log('5. animation done, pseudo-elements removed');
}
run();The first log appears before the callback's log. The browser does not run your callback synchronously. It runs the callback after the old state has been captured.
Common errors
- Changing the DOM outside the callback. Changes made before the call or after it returns are not part of the transition. The old snapshot may already include them, so nothing animates.
- Forgetting to wait for async work. If the callback starts an update and returns before it finishes, the new capture shows an incomplete page. Return the promise.
- Slow callbacks. The page is frozen while the callback runs.
- Throwing in the callback. If the callback throws or its promise rejects, the browser skips the animation.
Exercise
A developer calls document.startViewTransition(() => { fetch('/item').then(r => r.text()).then(html => { box.innerHTML = html; }); }). The page animates, but the new content appears after the animation ends. What is wrong, and how do you fix it?
Show answer
The callback returns before the fetch finishes, so the browser captures the new state while the DOM is still unchanged. Return the promise chain, or make the callback async and await the fetch, so the browser waits until box.innerHTML is set. Better still, fetch first and run only the DOM update inside the callback, to avoid freezing the page.