Astro > Managing images

Managing images

One of the major advantages of using DatoCMS instead of any other content management systems is its responsiveImage query, which will return pre-computed image attributes that will help you setting up responsive images in your frontend without any additional manipulation.

To make it even easier to offer responsive, progressive, lazy-loaded images on your projects, we offer a package called @datocms/astro that exposes an <Image /> component and pairs perfectly with the responsiveImage query:

To take advantage of it, install the @datocms/astro package:

yarn add @datocms/astro

Before using the image component, it is necessary to obtain the necessary data by running a GraphQL query using responsiveImage.

Then, inside your Astro component or page, feed content coming from a responsiveImage query directly into the <Image /> component:

---
// src/pages/index.astro
import { Image } from '@datocms/astro';
const query = `
query HomeQuery {
blogPost {
title
cover {
responsiveImage(imgixParams: { fit: crop, w: 300, h: 300, auto: format }) {
# always required
src
srcSet
width
height
# not required, but strongly suggested!
alt
title
# LQIP (base64-encoded)
base64
# you can omit 'sizes' if you explicitly pass the 'sizes' prop to the image component
sizes
}
}
}
`;
const data = await executeQuery(query);
---
<article>
<h1>{data.blogPost.title}</h1>
<Image data={data.blogPost.cover.responsiveImage} />
</article>

The image component creates zero JS footprint, produces a single <picture /> element, and implements lazy-loading using the native loading="lazy" attribute. You can refer to the package README to learn more.