Sub topic 12
Waiting for critical content before animating
This chapter assumes you can already enable cross-document transitions with @view-transition { navigation: auto; } and that you know the old and new pages are captured as snapshots. Here you will learn why a transition can animate to a half-loaded page, and how to prevent that.
The problem: animating to an unfinished page
A cross-document transition needs a first frame of the new page to use as the "new" snapshot. By default the browser may render that first frame as soon as it has something to paint. On a slow connection, that can be a page whose header has arrived but whose main content has not.
The result is a smooth animation that ends on an empty or partly built page. Then the content pops in. The transition is working, but it animates the wrong thing.
Blocking render until an element exists
Add a link rel="expect" hint with blocking="render" to the <head> of the destination page. The href points to the id of the element that must be parsed before the first render.
<head>
<link rel="expect" href="#main" blocking="render">
</head>
<body>
<header>…</header>
<main id="main">…</main>
</body>The browser holds the first render until the element with id main has been parsed. The transition then captures a page that already contains the content.
Some points about how this behaves:
- The element must exist in the HTML that the server sends. If the target is added later by JavaScript, the browser has nothing to wait for until that script runs.
- Put the hint in the document head, because render blocking is decided before the body is painted.
- Choose a stable target. If the id is missing on some pages, the hint has nothing to wait for on those pages.
- Browsers that do not support the hint ignore it. The page still loads and transitions, just without the guarantee.
Keep the wait short
Blocking render delays what the user sees. If the target element sits at the very end of a large document, the user stares at the old page for longer, or at a blank screen if the old page is already gone.
Browsers also cap how long they will keep rendering blocked. After a timeout, the page renders anyway and the transition may be skipped. You cannot rely on blocking to hide a slow server.
Practical ways to keep the wait short:
- Place the expected element early in the markup, directly after the header.
- Keep critical CSS inline or in a small stylesheet, so styling is not what delays the first paint.
- Avoid blocking on content that is generated by a slow backend call.
Blocking versus the transition itself
Render blocking and the transition solve different problems. The table separates them.
| Concern | Tool |
|---|---|
| The new page must contain its main content before the snapshot | `link rel="expect"` with `blocking="render"` |
| The animation should look a particular way | `::view-transition-old()` and `::view-transition-new()` |
| Different animation per direction | Transition types |
Use the hint only when you have seen a transition end on an incomplete page. Pages that load quickly from cache often do not need it.
Checking that it works
Throttle the network in DevTools to a slow profile and navigate between pages. Without the hint, you may see the content arrive after the animation. With it, the animation should finish on the full layout, at the cost of a slightly later start.
Exercise
A page's transition ends on a layout with an empty content area, and the content appears a moment later. Which element and attribute do you add to the destination page's head, and why must the target element be in the server-sent HTML?
Show answer
Add <link rel="expect" href="#main" blocking="render">, where #main is the id of the content container. The browser waits to render until that element has been parsed, so the new snapshot includes the content. If JavaScript adds the element later, it is not in the parsed HTML, so there is nothing for the browser to wait on.
Exercise
Why is it a bad idea to point the hint at an element near the end of a very large document?
Show answer
Rendering is held until that element is parsed, so users wait longer before seeing anything new. Browsers also stop blocking after a timeout, so a very late target may not help at all.