Skip to content

Commit dbf37e1

Browse files
docs: Add guidance regarding using getImage on the client (#13354)
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
1 parent afad7a0 commit dbf37e1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/content/docs/en/guides/images.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,24 @@ const allBlogPosts = await getCollection("blog");
633633

634634
The `getImage()` function is intended for generating images destined to be used somewhere else than directly in HTML, for example in an [API Route](/en/guides/endpoints/#server-endpoints-api-routes). When you need options that the `<Picture>` and `<Image>` components do not currently support, you can use the `getImage()` function to create your own custom `<Image />` component.
635635

636+
`getImage()` can only be used on the server. If you need to use the resulting image URL on the client (e.g. in a client-side script or framework component), call `getImage()` inside the frontmatter and pass the resulting `src` to the client:
637+
638+
```astro title="src/components/ClientImage.astro"
639+
---
640+
import { getImage } from "astro:assets";
641+
import myBackground from "../background.png";
642+
643+
const optimizedBackground = await getImage({ src: myBackground, format: "avif" });
644+
---
645+
646+
<div id="background" data-src={optimizedBackground.src}></div>
647+
648+
<script>
649+
const src = document.getElementById("background").dataset.src;
650+
// use src client-side as needed
651+
</script>
652+
```
653+
636654
<ReadMore>See more in the [`getImage()` reference](/en/reference/modules/astro-assets/#getimage).</ReadMore>
637655

638656
<RecipeLinks slugs={["en/recipes/build-custom-img-component" ]}/>

src/content/docs/en/guides/upgrade-to/v6.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,36 @@ If you were previously relying on the fact that the image service would automati
14301430
14311431
<ReadMore>Learn more about [the `format` image property](/en/reference/modules/astro-assets/#format)</ReadMore>
14321432
1433+
### Changed: `getImage()` throws when called on the client
1434+
1435+
<SourcePR number="15800" title="feat: disallow getImage on the client" />
1436+
1437+
In Astro 5.x, calling `getImage()` from `astro:assets` on the client would silently fail or produce incorrect results.
1438+
1439+
Astro 6.0 throws a runtime error when `getImage()` is called on the client.
1440+
1441+
#### What should I do?
1442+
1443+
Call `getImage()` on the server and pass the resulting `src` to the client instead:
1444+
1445+
```astro title="src/components/ClientImage.astro" {5, 8, 11}
1446+
---
1447+
import { getImage } from "astro:assets";
1448+
import myBackground from "../background.png";
1449+
1450+
const optimizedBackground = await getImage({ src: myBackground, format: "avif" });
1451+
---
1452+
1453+
<div id="background" data-src={optimizedBackground.src}></div>
1454+
1455+
<script>
1456+
const src = document.getElementById("background").dataset.src;
1457+
// use src client-side as needed
1458+
</script>
1459+
```
1460+
1461+
<ReadMore>See [generating images with `getImage()`](/en/guides/images/#generating-images-with-getimage) for a full example.</ReadMore>
1462+
14331463
### Changed: Markdown heading ID generation
14341464
14351465
<SourcePR number="14494" title="feat!: stabilize experimental.headingIdCompat"/>

src/content/docs/en/reference/modules/astro-assets.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ Variable weight font files will be preloaded if any weight within its range is r
638638
</p>
639639

640640
:::caution
641-
`getImage()` relies on server-only APIs and breaks the build when used on the client.
641+
`getImage()` relies on server-only APIs and will throw an error on the client.
642+
643+
If you need the resulting image URL client-side, you can [pass the `src` from a server-rendered `getImage()` call to the client](/en/guides/images/#generating-images-with-getimage).
642644
:::
643645

644646
The `getImage()` function is intended for generating images destined to be used somewhere else than directly in HTML, for example in an [API Route](/en/guides/endpoints/#server-endpoints-api-routes). It also allows you to create your own custom `<Image />` component.

0 commit comments

Comments
 (0)