Astro’s built-in image system can help us handle everything that Lighthouse Audit requires. It is not as seamless as the one I built, but let’s go through how you can use it first.
Using Astro’s Image Component
You have to import your image and pass the imported variable into the Image component. You cannot just pass the url directly.
---import { Image } from 'astro:assets'import myImage from '../assets/penguin.png'---
<Image src={myImage} alt="A very cool penguin!" />The output looks like this in development:
<img src="/_image/?href=some-long-string.......f=webp" data-image-component="true" alt="A very cool penguin!" loading="lazy" decoding="async" fetchpriority="auto" width="1588" height="1054"/>And it looks like this in production:
<img src="/_astro/penguin-3.ClS-zgCy_tIb6a.webp" alt="A very cool penguin!" loading="lazy" decoding="async" fetchpriority="auto" width="1588" height="1054"/>Two things to note here:
- The
srcstring changes between development and production - The
data-image-componentdisappears in production
You don’t have to worry about these two differences because Astro handles them. I just wanted to let you know about this in case you looked at the HTML and got a shock.
Fulfilling Lighthouse audits
This resolves 3 out of 6 of Google Lighthouse’s audit issues.
- Images must be efficiently encoded
- Images must use better formats like WebP and AVIF
- Images should be lazy-loaded if they’re below the fold
1 and 2 are handled by the image processing engine Astro uses behind the scenes. (Sharp is the default processor). 3 is handled with the loading="lazy" attribute.
The rest are not resolved just yet, but they can be handled with a few more tiny changes.
- Images must be properly sized
- Images above the fold should be prioritized
- The layouts must not shift when images are loaded
Ensuring layouts don’t shift
Astro’s Image component produces height and width attributes that ensure that the layouts do not shift. That is the first step. The second step is just to add some CSS.
/* Important properties for responsive images! */img { max-width: 100%; height: auto;}Images above the fold should be prioritized
Astro’s Image component takes in a priority prop that helps you prioritize images above the fold. It does the following:
- Set
loading="eager"— so the image is fetched immediately - Set
fetchpriority="high"— tells the browser to download the image along with other critical assets - Set
decoding="sync"— decodes the image immediately so the image can be painted
<Image src="..." alt="..." priority />This is all you need to prioritize images above the fold.
Sizing images properly
Let’s say a user loads your website on their mobile device, and their screen is only 480px wide. They don’t need a 1500px image. We can serve them a smaller image instead. This saves bandwidth and improves load times.
However, if their mobile device has a higher pixel density — as most retina screens do nowadays — the image needed depends on the pixel density. A device with 2x pixel density will need an image that’s twice the device’s width.
Ultimately, we want to serve the smallest image that still looks sharp. In Astro, we can do this with 2 properties:
widths— tells Astro’s image processor to generate the image at several sizes. It produces ansrcsetattribute.sizes— tells the browser how wide the image will display at each breakpoint, so it can pick the smallest image that still looks sharp. When omitted, sizes defaults to100vwwhich is good enough for most cases.
<!-- sizes when omitted defaults to 100vw --><Image src={myImg} alt="..." widths={[400, 800, 1200]} sizes="100vw"/>Code you can copy
The major difference for all images is whether they are above the fold or below the fold. For images above the fold, you need to prioritize them with the priority property.
---import { Image } from 'astro:assets'import myImage from '../assets/penguin.png'---
<Image src={myImage} alt="..." widths={[400, 800, 1200]} priority />For images below the fold, you don’t have to.
---import { Image } from 'astro:assets'import myImage from '../assets/penguin.png'---
<Image src={myImage} alt="..." widths={[400, 800, 1200]} />In both cases, make sure you have the following CSS to make images responsive while preventing layout shifts.
img { max-width: 100%; height: auto;}Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson