Skip to content

Commit 0d41392

Browse files
committed
Enhance Suspense documentation with examples of stylesheet loading behavior and vanilla DOM comparison
1 parent 551cdf6 commit 0d41392

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

src/content/reference/react/Suspense.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,13 +2377,16 @@ The server HTML will include the loading indicator. It will be replaced by the `
23772377
23782378
A stylesheet rendered with [`<link rel="stylesheet">` and a `precedence` prop](/reference/react-dom/components/link#special-rendering-behavior) blocks the Suspense boundary until the stylesheet loads, up to a timeout, so the content doesn't appear unstyled.
23792379
2380-
In the example below, the `Card` component renders a stylesheet with `precedence`. Press "Show card": React shows the fallback until the stylesheet has loaded, and then reveals the card with its styles applied:
2380+
In the example below, the `Card` component renders a stylesheet with `precedence`. Press "Show card": React shows the fallback until the stylesheet has loaded, and then reveals the card with its styles applied.
2381+
2382+
For comparison, the second button performs the same update with plain DOM in a separate document, without React. Nothing waits for the stylesheet, so the card's text appears in a fallback font first and then switches:
23812383
23822384
<Sandpack>
23832385
23842386
```js
23852387
import { Suspense, useState, startTransition } from 'react';
23862388
import { freshStylesheetUrl } from './styles.js';
2389+
import VanillaCard from './VanillaCard.js';
23872390

23882391
function Card({ href }) {
23892392
return (
@@ -2411,6 +2414,43 @@ export default function App() {
24112414
<Card href={href} />
24122415
</Suspense>
24132416
)}
2417+
<hr />
2418+
<VanillaCard />
2419+
</>
2420+
);
2421+
}
2422+
```
2423+
2424+
```js src/VanillaCard.js
2425+
import { useRef } from 'react';
2426+
import { freshStylesheetUrl } from './styles.js';
2427+
2428+
export default function VanillaCard() {
2429+
const ref = useRef(null);
2430+
function show() {
2431+
const doc = ref.current.contentWindow.document;
2432+
doc.open();
2433+
doc.write(`
2434+
<link rel="stylesheet" href="${freshStylesheetUrl()}">
2435+
<style>
2436+
body { margin: 0; }
2437+
.fancy-card {
2438+
padding: 20px;
2439+
border-radius: 8px;
2440+
color: white;
2441+
font-family: 'Caveat', sans-serif;
2442+
font-size: 24px;
2443+
background: linear-gradient(135deg, #087ea4, #2b3491);
2444+
}
2445+
</style>
2446+
<div class="fancy-card">This card uses a font from the stylesheet.</div>
2447+
`);
2448+
doc.close();
2449+
}
2450+
return (
2451+
<>
2452+
<button onClick={show}>Show card (vanilla DOM)</button>
2453+
<iframe ref={ref} title="Vanilla card" className="vanilla-frame" />
24142454
</>
24152455
);
24162456
}
@@ -2421,7 +2461,7 @@ export default function App() {
24212461
// and every run shows the loading state.
24222462
export function freshStylesheetUrl() {
24232463
return (
2424-
'https://fonts.googleapis.com/css2?family=Caveat&display=block' +
2464+
'https://fonts.googleapis.com/css2?family=Caveat&display=swap' +
24252465
'&t=' +
24262466
Date.now()
24272467
);
@@ -2430,11 +2470,14 @@ export function freshStylesheetUrl() {
24302470
24312471
```css
24322472
#root {
2433-
min-height: 140px;
2473+
min-height: 300px;
24342474
}
24352475
button {
24362476
margin-right: 8px;
24372477
}
2478+
hr {
2479+
margin: 16px 0;
2480+
}
24382481
.fancy-card {
24392482
margin-top: 1em;
24402483
padding: 20px;
@@ -2444,6 +2487,13 @@ button {
24442487
font-size: 24px;
24452488
background: linear-gradient(135deg, #087ea4, #2b3491);
24462489
}
2490+
.vanilla-frame {
2491+
display: block;
2492+
margin-top: 1em;
2493+
border: none;
2494+
width: 100%;
2495+
height: 90px;
2496+
}
24472497
```
24482498
24492499
</Sandpack>

0 commit comments

Comments
 (0)