SvelteKit > 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/svelte that exposes an <Image /> component and pairs perfectly with the responsiveImage query:

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

yarn add @datocms/svelte

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

// src/routes/+page.server.ts
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
}
}
}
`;
export const load = () => {
return executeQuery(query);
};

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

<script>
// src/routes/+page.svelte
import { Image } from '@datocms/svelte';
export let data;
</script>
<Image data={data.blogPost.cover.responsiveImage} />

The @datocms/svelte package also offer a <NakedImage /> component which generates minimum JS footprint, outputs a single <picture /> element and implements lazy-loading using the native loading="lazy" attribute. You can refer to the package README to learn more.