08/10/2024, Sat
Searching for Images
Astro comes with a default image component that you can use to
load your images from the /src/assets folder.
From the actual documentation page,
---
// import the Image component and the image
import { Image } from 'astro:assets';
import myImage from "../assets/my_image.png"; // Image is 1600x900
---
<!-- `alt` is mandatory on the Image component -->
<Image src={myImage} alt="A description of my image." />
Using Astro's Image
component is useful in that it helps you perform optimizations to your images on the final build. It is able to output to webp format as well as set the width and height for them to improve site layout performance.
However, one of the downside of using the default component is that you have to load the images one by one. Astro also has a suggestion for dynamic images using import.meta.glob
, but it still is not as dynamic as compared to that of Astro's glob
method.