Skip to content

Commit d56099a

Browse files
committed
Clarify image loading behavior during <ViewTransition> updates in Suspense documentation
1 parent d63cade commit d56099a

1 file changed

Lines changed: 67 additions & 22 deletions

File tree

‎src/content/reference/react/Suspense.md‎

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ A Suspense boundary waits for its content to be ready before revealing it. Any o
4747
- Loading a stylesheet rendered with [`<link rel="stylesheet">` and a `precedence` prop.](/reference/react-dom/components/link#special-rendering-behavior) React blocks the boundary until the stylesheet loads, up to a timeout. [See an example below.](#waiting-for-a-stylesheet-to-load)
4848
- Waiting for a large boundary's HTML to arrive during streaming server rendering. Sending HTML takes time, so a boundary with enough content activates even when nothing in it suspends. React reveals the content as the HTML arrives.
4949
- Loading fonts. This doesn't happen by default, but a [`<ViewTransition>`](/reference/react/ViewTransition) update waits for new fonts to load, up to a timeout, so text doesn't flash with a fallback font. [See an example below.](#waiting-for-a-font-to-load)
50-
- Loading images. This doesn't happen by default, but a [`<ViewTransition>`](/reference/react/ViewTransition) update waits for its images to load, up to a timeout. Adding an `onLoad` handler opts a specific image out. [See an example below.](#waiting-for-an-image-to-load)
50+
- Loading images. This doesn't happen by default, but during a [`<ViewTransition>`](/reference/react/ViewTransition) update, an image's `src` blocks the boundary until the image loads, up to a timeout. Adding an `onLoad` handler opts a specific image out. [See an example below.](#waiting-for-an-image-to-load)
5151
- <ExperimentalBadge /> Performing CPU-bound render work inside a `<Suspense>` boundary marked with the `defer` prop.
5252

5353
<Note>
@@ -2261,16 +2261,33 @@ During a Transition, React avoids hiding already revealed content. However, when
22612261
22622262
With a different `key`, React treats the profiles as different components and resets the Suspense boundary during navigation. Suspense-integrated routers do this automatically.
22632263
2264-
In the example below, the `key` resets the boundary when you switch profiles, so the fallback shows instead of the previous user's bio. Try removing the `key`: the previous bio stays visible while the next one loads:
2264+
In the example below, opening the profile page loads the first profile. Switching tabs navigates to a different profile, and the `key` resets the boundary, so the fallback shows instead of the previous user's bio. Try removing the `key`: the previous bio stays visible while the next one loads:
22652265
22662266
<Sandpack>
22672267
2268-
```js
2268+
```js src/App.js hidden
2269+
import { useState } from 'react';
2270+
import ProfilePage from './ProfilePage.js';
2271+
2272+
export default function App() {
2273+
const [show, setShow] = useState(false);
2274+
if (show) {
2275+
return <ProfilePage />;
2276+
}
2277+
return (
2278+
<button onClick={() => setShow(true)}>
2279+
Open profile page
2280+
</button>
2281+
);
2282+
}
2283+
```
2284+
2285+
```js src/ProfilePage.js active
22692286
import { Suspense, useState, startTransition } from 'react';
22702287
import Bio from './Bio.js';
22712288
import { fetchBio } from './data.js';
22722289

2273-
export default function App() {
2290+
export default function ProfilePage() {
22742291
const [user, setUser] = useState(() => ({
22752292
id: 'alice',
22762293
bioPromise: fetchBio('alice'),
@@ -2668,37 +2685,50 @@ Where you place the `<ViewTransition>` relative to the boundary determines wheth
26682685
26692686
<CanaryBadge /> When a [`<ViewTransition>`](/reference/react/ViewTransition) animates a boundary's reveal, React also waits for new fonts the content introduces, up to a timeout, so the text doesn't flash with a fallback font. This doesn't happen by default outside a `<ViewTransition>`.
26702687
2671-
In the example below, the quote uses a new font. The browser only starts downloading a font when text first uses it, so rendering the quote starts the download. React holds the animated reveal until the font has loaded:
2688+
In the example below, the `Quote` component suspends while its data loads. Rendering the quote starts its font download, so React keeps the fallback visible until the font has loaded, and the quote appears already in its font:
26722689
26732690
<Sandpack>
26742691
26752692
```js
2676-
import { ViewTransition, Suspense, useState, startTransition } from 'react';
2693+
import { ViewTransition, Suspense, use, useState, startTransition } from 'react';
2694+
import { fetchQuote } from './data.js';
2695+
2696+
const fontUrl =
2697+
'https://raw.githubusercontent.com/google/fonts/main/ofl/caveat/Caveat%5Bwght%5D.ttf';
26772698

2678-
function Quote() {
2699+
function Quote({ fontSrc }) {
2700+
const quote = use(fetchQuote());
26792701
return (
2680-
<p className="quote fancy">
2681-
The best way to predict the future is to invent it.
2682-
</p>
2702+
<>
2703+
<style href="fancy-font" precedence="default">
2704+
{`@font-face {
2705+
font-family: 'Fancy';
2706+
src: url(${fontSrc}) format('truetype');
2707+
}`}
2708+
</style>
2709+
<p className="quote fancy">{quote}</p>
2710+
</>
26832711
);
26842712
}
26852713

26862714
export default function App() {
2687-
const [show, setShow] = useState(false);
2715+
const [fontSrc, setFontSrc] = useState(null);
26882716
return (
26892717
<>
26902718
<button
26912719
onClick={() => {
26922720
startTransition(() => {
2693-
setShow(true);
2721+
// Add a unique parameter so the font isn't cached,
2722+
// and every run shows the font being waited on.
2723+
setFontSrc(fontUrl + '?t=' + Date.now());
26942724
});
26952725
}}>
26962726
Show quote
26972727
</button>
2698-
{show && (
2728+
{fontSrc && (
26992729
<ViewTransition>
27002730
<Suspense fallback={<p className="quote">⌛ Loading...</p>}>
2701-
<Quote />
2731+
<Quote fontSrc={fontSrc} />
27022732
</Suspense>
27032733
</ViewTransition>
27042734
)}
@@ -2707,14 +2737,29 @@ export default function App() {
27072737
}
27082738
```
27092739
2710-
```css
2711-
/* The browser doesn't download the font until
2712-
text first renders with this font family. */
2713-
@font-face {
2714-
font-family: 'Fancy';
2715-
src: url(https://fonts.gstatic.com/s/caveat/v23/WnznHAc5bAfYB2QRah7pcpNvOx-pjfJ9eIipYT5Kmgq3s84t.woff2)
2716-
format('woff2');
2740+
```js src/data.js hidden
2741+
// Note: the way you would do data fetching depends on
2742+
// the framework that you use together with Suspense.
2743+
// Normally, the caching logic would be inside a framework.
2744+
2745+
let cache = null;
2746+
2747+
export function fetchQuote() {
2748+
if (!cache) {
2749+
cache = new Promise((resolve) => {
2750+
// Add a fake delay to make waiting noticeable.
2751+
setTimeout(() => {
2752+
resolve(
2753+
'The best way to predict the future is to invent it.'
2754+
);
2755+
}, 1500);
2756+
});
2757+
}
2758+
return cache;
27172759
}
2760+
```
2761+
2762+
```css
27182763
#root {
27192764
min-height: 100px;
27202765
}
@@ -2745,7 +2790,7 @@ export default function App() {
27452790
27462791
<CanaryBadge /> Images work the same way: when a [`<ViewTransition>`](/reference/react/ViewTransition) animates a boundary's reveal, React waits for visible images in the content to load before revealing the content, so the animation doesn't finish on a half-loaded image. This doesn't happen by default outside a `<ViewTransition>`. Adding an `onLoad` handler opts a specific image out, even inside a `<ViewTransition>`.
27472792
2748-
In the example below, React holds the animated reveal until the portrait has loaded:
2793+
In the example below, the boundary shows its fallback until the portrait has loaded:
27492794
27502795
<Sandpack>
27512796

0 commit comments

Comments
 (0)