#lazy loading images inside the .mdx file
7 messages · Page 1 of 1 (latest)
Astro provides automatic support for images in Markdown, Markdoc, and MDX files. When you reference images using a relative image path in your Markdown content or frontmatter, Astro will automatically optimize them for you. The resulting <img> tag will have the loading="lazy" attribute, which enables lazy loading.
Here's an example of how you can reference an image in your Markdown content:
!**A starry night sky** (../../assets/stars.png)
This will be transformed into the following HTML:
<img src="/_astro/stars.XXXXXX.webp" width="300" height="300" loading="lazy" decoding="async" alt="A starry night sky"/>
As you can see, the loading="lazy" attribute is automatically added to the <img> tag, enabling lazy loading for the image.
If you need more control over the image attributes, you can use the <Image /> component directly in MDX. However, in most cases, the default settings provided by Astro should be sufficient.
Please note that the loading="lazy" attribute is added by default to the <img> tag generated by Astro. If you want to disable this, you would need to create a custom image service or use a workaround as discussed in this Github issue (https://github.com/withastro/astro/issues/6611).
For more information, you can refer to the Astro documentation on images (https://docs.astro.build/en/guides/images#additional-properties).
Please note that the <Image /> component optimizes your image and infers width and height (of local images) based on the original aspect ratio to avoid CLS. You can use the HTML <img> element when you cannot use the <Image /> component, for example, for unsupported image formats, when you do not want your image optimized by Astro, or to access and change the src attribute dynamically client-side. You can find more details in the Astro documentation (https://docs.astro.build/en/guides/images#choosing-image--vs-img).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@slim palm kapa.ai is still learning and improving, please let us know how it did by reacting below
would it still work if the images are inside the public/assets folder @azure island
Let me check the knowledge base for you. One moment! :books:
Yes, images in the public/ folder can be used with the Astro <Image /> component, but they will not be processed or optimized. This means that the loading="lazy" attribute will not be automatically added to the <img> tag for these images.
Here's an example of how you can use an image from the public/ folder:
<Image src="/images/bird-in-public-folder.jpg" alt="A bird." width="50" height="50" />
This will be transformed into the following HTML:
<img src="/images/bird-in-public-folder.jpg" alt="A bird." width="50" height="50" />
As you can see, the loading="lazy" attribute is not added to the <img> tag.
If you want to add the loading="lazy" attribute to these images, you would need to use the HTML <img> tag directly and add the attribute manually:
<img src="/images/bird-in-public-folder.jpg" alt="A bird." width="50" height="50" loading="lazy" />