Skip to content

Commit 1fde845

Browse files
committed
Cache image promises in Suspense examples
1 parent e946aa2 commit 1fde845

2 files changed

Lines changed: 46 additions & 26 deletions

File tree

‎src/content/reference/react-dom/components/img.md‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ import {
140140
} from 'react';
141141
import { fetchImageSrc } from './image.js';
142142

143-
function Profile({ srcPromise }) {
144-
const src = use(srcPromise);
143+
function Profile({ cacheKey }) {
144+
const src = use(fetchImageSrc(cacheKey));
145145
return (
146146
<div className="card">
147147
<img src={src} alt="Jack Pope" width={80} height={80} />
@@ -160,29 +160,29 @@ function ProfilePlaceholder() {
160160
}
161161

162162
export default function App() {
163-
const [transitionSrcPromise, setTransitionSrcPromise] =
164-
useState(null);
165-
const [srcPromise, setSrcPromise] = useState(null);
163+
const [showWithTransition, setShowWithTransition] =
164+
useState(false);
165+
const [showWithoutTransition, setShowWithoutTransition] =
166+
useState(false);
166167
return (
167168
<>
168-
<button
169-
onClick={() => setTransitionSrcPromise(fetchImageSrc())}>
169+
<button onClick={() => setShowWithTransition(true)}>
170170
Show profile with View Transition
171171
</button>
172-
{transitionSrcPromise && (
172+
{showWithTransition && (
173173
<ViewTransition>
174174
<Suspense fallback={<ProfilePlaceholder />}>
175-
<Profile srcPromise={transitionSrcPromise} />
175+
<Profile cacheKey="with-transition" />
176176
</Suspense>
177177
</ViewTransition>
178178
)}
179179
<hr />
180-
<button onClick={() => setSrcPromise(fetchImageSrc())}>
180+
<button onClick={() => setShowWithoutTransition(true)}>
181181
Show profile without View Transition
182182
</button>
183-
{srcPromise && (
183+
{showWithoutTransition && (
184184
<Suspense fallback={<ProfilePlaceholder />}>
185-
<Profile srcPromise={srcPromise} />
185+
<Profile cacheKey="without-transition" />
186186
</Suspense>
187187
)}
188188
</>
@@ -191,7 +191,17 @@ export default function App() {
191191
```
192192

193193
```js src/image.js hidden
194-
export async function fetchImageSrc() {
194+
// Normally, the caching logic would be inside a framework.
195+
const cache = new Map();
196+
197+
export function fetchImageSrc(cacheKey) {
198+
if (!cache.has(cacheKey)) {
199+
cache.set(cacheKey, loadImageSrc());
200+
}
201+
return cache.get(cacheKey);
202+
}
203+
204+
async function loadImageSrc() {
195205
// Delay the response so the Suspense fallback is visible.
196206
await new Promise(resolve => setTimeout(resolve, 1000));
197207
// Add a unique parameter so the image isn't cached.

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,8 +3066,8 @@ import {
30663066
} from 'react';
30673067
import { fetchImageSrc } from './image.js';
30683068

3069-
function Profile({ srcPromise }) {
3070-
const src = use(srcPromise);
3069+
function Profile({ cacheKey }) {
3070+
const src = use(fetchImageSrc(cacheKey));
30713071
return (
30723072
<div className="card">
30733073
<img src={src} alt="Jack Pope" width={80} height={80} />
@@ -3086,29 +3086,29 @@ function ProfilePlaceholder() {
30863086
}
30873087

30883088
export default function App() {
3089-
const [transitionSrcPromise, setTransitionSrcPromise] =
3090-
useState(null);
3091-
const [srcPromise, setSrcPromise] = useState(null);
3089+
const [showWithTransition, setShowWithTransition] =
3090+
useState(false);
3091+
const [showWithoutTransition, setShowWithoutTransition] =
3092+
useState(false);
30923093
return (
30933094
<>
3094-
<button
3095-
onClick={() => setTransitionSrcPromise(fetchImageSrc())}>
3095+
<button onClick={() => setShowWithTransition(true)}>
30963096
Show profile with View Transition
30973097
</button>
3098-
{transitionSrcPromise && (
3098+
{showWithTransition && (
30993099
<ViewTransition>
31003100
<Suspense fallback={<ProfilePlaceholder />}>
3101-
<Profile srcPromise={transitionSrcPromise} />
3101+
<Profile cacheKey="with-transition" />
31023102
</Suspense>
31033103
</ViewTransition>
31043104
)}
31053105
<hr />
3106-
<button onClick={() => setSrcPromise(fetchImageSrc())}>
3106+
<button onClick={() => setShowWithoutTransition(true)}>
31073107
Show profile without View Transition
31083108
</button>
3109-
{srcPromise && (
3109+
{showWithoutTransition && (
31103110
<Suspense fallback={<ProfilePlaceholder />}>
3111-
<Profile srcPromise={srcPromise} />
3111+
<Profile cacheKey="without-transition" />
31123112
</Suspense>
31133113
)}
31143114
</>
@@ -3117,7 +3117,17 @@ export default function App() {
31173117
```
31183118
31193119
```js src/image.js hidden
3120-
export async function fetchImageSrc() {
3120+
// Normally, the caching logic would be inside a framework.
3121+
const cache = new Map();
3122+
3123+
export function fetchImageSrc(cacheKey) {
3124+
if (!cache.has(cacheKey)) {
3125+
cache.set(cacheKey, loadImageSrc());
3126+
}
3127+
return cache.get(cacheKey);
3128+
}
3129+
3130+
async function loadImageSrc() {
31213131
// Delay the response so the Suspense fallback is visible.
31223132
await new Promise(resolve => setTimeout(resolve, 1000));
31233133
// Add a unique parameter so the image isn't cached.

0 commit comments

Comments
 (0)