Sub topic 12
Waiting for critical content before animating
This chapter assumes you can already enable cross-document transitions with @view-transition { navigation: auto }. It also assumes you know the basic pseudo-element tree and that a navigation loads a new HTML document from the network. Here you will learn why a transition can animate toward an incomplete page, and how to tell the browser to wait.
The problem: animating to a half-built page
A cross-document transition has two sides. The browser snapshots the old page, loads the new document, and then needs a first frame of the new page to use as the "new" snapshot. The animation runs between those two images.
The browser does not automatically wait for your main content. It treats the new page as ready for its first render once the usual render-blocking resources have loaded. Those are stylesheets in the <head> and any script or style marked blocking="render". HTML is parsed in a stream, so the first render can happen when only the top of the <body> has arrived.
Suppose the new page's header is parsed, but <main> is still downloading. The new snapshot is then a header above an empty area. The transition animates to that empty state, and the content pops in after the animation. This is most visible when:
- A shared element, such as a hero image container, lives inside
<main>. - The server streams the response slowly, for example because of a database query.
- The page's
<head>is large, so the body starts late and arrives in chunks.
The result is a transition that technically ran but looks broken.
How the browser decides when to render
Normally the browser may paint as soon as it has a reasonable first frame. For view transitions, that first frame is captured as the new snapshot. Render blocking changes the rule by adding a condition: do not produce the first frame until a named piece of content exists.
While the browser waits, the old page stays on screen. The user sees the page they clicked from, not a blank or half-built page. That is usually the better experience, but it has a cost, covered below.
Declaring what to wait for
The mechanism is a <link> element with rel="expect". Its href is a fragment pointing at an element's id, and blocking="render" makes the wait mandatory.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Post: Cross-document transitions</title>
<link rel="expect" href="#main" blocking="render">
<style>
@view-transition {
navigation: auto;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
}
.site-header {
padding: 1rem;
background: #1f2933;
color: white;
view-transition-name: site-header;
}
main {
padding: 1rem;
}
</style>
</head>
<body>
<header class="site-header">My blog</header>
<main id="main">
<h1>Cross-document transitions</h1>
<p>The browser will not render this page until this main element is parsed.</p>
</main>
</body>
</html>The href must match the id of an element that appears in the same document. Put the link in the <head>, because the browser has to see it before it first renders. A link that appears late in the body cannot hold back a render that has already happened.
The usual choice is the largest container that holds the page's primary content. If your transition morphs a hero image, wait for the element that contains that image. Waiting for something far below the fold only delays the page for no benefit.
What "parsed" means
The browser waits until the element exists in the DOM, meaning the parser has reached and finished creating it. It does not wait for images inside it to download. If you need the hero image decoded for the morph, that is a separate concern (see the pitfalls below).
Because the check is on the parsed DOM, the element should come from the server's HTML. If JavaScript inserts #main after load, the browser waits for something a script has not yet created. It will not wait forever, as the next section explains, but you will have given up the benefit.
Limits on the wait
Render blocking is not an unconditional hold. The browser applies safety limits so a slow or broken page cannot freeze navigation:
- If parsing finishes and no element with that id exists, the block is released. A typo in the
hrefdoes not hang the page. - Browsers apply a time limit on how long they hold the old page for a cross-document transition. If the new page is not ready in time, the transition is skipped and the new page is shown normally. The exact limit is browser-defined, so do not design around a specific number.
- Browsers that do not understand
rel="expect"ignore the link. They render as they always did, which may mean the transition animates to a partial page. The page still works.
| Situation | Without `rel="expect"` | With `rel="expect"` |
|---|---|---|
| Fast server, small page | Works, both snapshots complete | Works, no visible difference |
| Slow streamed response | New snapshot may be missing main content | Old page stays until main is parsed |
| `href` points to a missing id | Not applicable | Block released when parsing ends |
| Browser without support | Default behavior | Link ignored, default behavior |
| Very slow page | Transition may animate to a partial page | Transition may be skipped after the limit |
Combining it with other render-blocking tools
rel="expect" is one of several ways to hold the first render. All of them delay the new snapshot, so they add together.
- Stylesheets in the
<head>block rendering by default. The transition CSS (your@keyframesand pseudo-element rules) must be in one of these or in a<style>block, or the first frame will use default styles. - Scripts and styles with
blocking="render"hold the render until they load. Use this sparingly. <link rel="expect">holds the render until a named element is parsed.
A frequent mistake is loading transition styles from a stylesheet marked media="print" with a swap trick, or from a deferred script. The new page then renders without the rules for the new snapshot. Keep transition CSS in the normal render-blocking path.
Choosing what to block on
Treat the blocking target as a deliberate performance decision.
- Keep it early in the document. The earlier
#mainappears in the HTML, the shorter the wait. - Keep the head small. Everything before
#mainin the stream delays it. - Avoid blocking on content that depends on a slow backend call. If the product list takes two seconds to build, either stream the shell first and block only on a part that is ready, or accept that the transition may be skipped.
- Do not block on elements you do not need. A page with several expected elements waits for all of them, and you pay for each.
Because blocking delays the first paint of the new page, it affects metrics such as First Contentful Paint and Largest Contentful Paint. Measure before and after. For fast pages the cost is negligible. For slow pages you are trading a smoother animation for a later paint, and a skipped transition is a reasonable outcome.
Common errors
- Placing the link in the body. The browser may already have rendered. Put it in the
<head>. - Mismatched id.
href="#main"needsid="main", with no#in the id itself. - Blocking on a JavaScript-created element. The wait does not end until parsing completes, so you lose the benefit.
- Adding it only to one page. Each page that can be a destination needs its own expectation. The rule applies to the page being loaded, so the destination's markup is what counts.
- Expecting it to wait for images. It waits for the element, not for its resources.
Verifying that it works
- Open the destination page in a Chromium-based browser that supports the feature, and throttle the network to a slow profile.
- Click a link from the source page and watch what stays visible during the load. With blocking active, the old page remains until the new content is parsed.
- Remove the
rel="expect"link and repeat. You should see the transition run toward a page with an empty content area. - Temporarily change
href="#main"to a missing id to confirm the page still loads.
Exercise
A list page links to post pages. On slow connections, the transition to a post animates to a page with a header but an empty body, and the post text appears after the animation. The post page's <main id="content"> holds the article. What should you add, and where? Name one thing you should not do.
Show answer
Add <link rel="expect" href="#content" blocking="render"> inside the <head> of the post page, so the browser holds the first render until <main id="content"> is parsed. The href must match the id exactly. You should not insert #content with JavaScript after load, because the browser waits on parsing and a script-created element removes the benefit. Also avoid pointing the link at an element far down the page, since that only delays the first paint.