Performance: Image optimization with astro:assets
This affects page load speed by reducing image file sizes and improving rendering time for images.
--- import { getImage } from 'astro:assets'; const optimizedImage = await getImage({ src: '/images/photo.jpg', width: 800, format: 'webp' }); --- <img src={optimizedImage.src} alt="Photo" loading="lazy" decoding="async" />
<img src="/images/photo.jpg" alt="Photo">
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unoptimized full-size images | Minimal | Triggers 1 reflow per image load | High paint cost due to large images | [X] Bad |
| astro:assets optimized images with lazy loading | Minimal | Single reflow after image loads | Low paint cost with smaller images | [OK] Good |
Image component from astro:assets in an Astro project?astro:assetsastro:assets package provides tools to optimize images automatically.Image component's functionImage component serves images in optimized sizes and formats without manual work.Image auto-optimizes images [OK]Image uploads imagesImage component from astro:assets in an Astro file?Image component is a named export from astro:assets, so it requires curly braces.import { Image } from 'astro:assets'; uses named import and correct package name.---
import { Image } from 'astro:assets';
const photo = '../assets/photo.jpg';
---
Image behavior with src and widthImage component optimizes the image and respects the width prop to resize it.Image from astro:assets:
---
import { Image } from 'astro:assets';
const logo = '/images/logo.png';
---
src path formatImage component expects a relative path or imported asset, not an absolute URL path starting with '/'.alt attribute is present, and the import is correct; width/height are optional but recommended.src path should be relative, not absolute -> Option Asrc = B [OK]astro:assets. Which of the following code snippets correctly implements this feature?ImageImage component must be imported as a named import from astro:assets.import { Image } from 'astro:assets';
const pic = '../assets/pic.jpg';
<Image src={pic} alt="Scenery" placeholder="blur" width={600} height={400} />
uses correct import, relative path, and placeholder="blur" which is the proper way.placeholder="blur" and named import -> Option DPass the test and this pattern moves unknown