Zum Inhalt springen

Astro Image Optimization Guide

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

This guide explains how to implement image optimization in Astro 7.x with Starlight for blog posts and pages.

For Astro’s image optimization pipeline to work, images must be stored in the src/ directory, not the public/ directory.

Correct location: src/assets/images/... Incorrect location: public/assets/images/...

Images in the public/ directory are served as-is and are not optimized by Astro’s image pipeline.

To use the <Image /> component from astro:assets, the file must be an .mdx file, not a .md file.

  • .md files are processed as markdown and do not support JSX components like <Image />
  • .mdx files support JSX components and can use the <Image /> component from astro:assets

The <Image /> component from astro:assets provides:

  • Automatic image optimization (WebP/AVIF formats)
  • Responsive image generation with srcset attributes
  • CLS prevention with automatic width and height attributes
  • Lazy loading support

Astro 7.x uses sharp for image optimization. Install it as a dev dependency:

Terminal window
pnpm add -D sharp

Move images from public/assets/... to src/assets/images/...:

site/src/assets/images/image-generation/
├── 1-nature-landscape.png
├── 2-scifi-cyberpunk.png
├── 3-portrait-realistic.png
├── 4-abstract-artistic.png
└── 5-food-culinary.png

Step 3: Create MDX File with <Image /> Component

Section titled “Step 3: Create MDX File with <Image /> Component”

Create or convert blog posts to .mdx format:

---
title: "Blog Post Title"
date: 2026-07-06
---
import { Image } from 'astro:assets';
import natureImage from '../../../assets/images/image-generation/1-nature-landscape.png';
# Blog Post Content
<Image src={natureImage} alt="Nature Landscape" loading="lazy" />

Run the build command:

Terminal window
pnpm build

Verify optimized images are generated in dist/_astro/:

dist/_astro/
├── 1-nature-landscape.51SGon9e_2putX.webp
├── 2-scifi-cyberpunk.JjhCW2vR_Z1taUln.webp
├── 3-portrait-realistic.BWz-0tfI_m5CB4.webp
├── 4-abstract-artistic.Cgf-1GlJ_1ATgsX.webp
└── 5-food-culinary.BWr7H010_Z20apFE.webp

Symptom: Images are served as original PNG/JPG files, not WebP/AVIF.

Solution: Ensure images are in src/assets/..., not public/assets/....

Symptom: Build fails with “Module not found” or JSX parsing errors.

Solution: Ensure the file is .mdx, not .md. Markdown files do not support JSX components.

Symptom: Build fails with “MissingSharp: Could not find Sharp.”

Solution: Install sharp as a dev dependency: pnpm add -D sharp

Original PNG files (559-677kB) are optimized to WebP format (26-50kB), achieving 85-95% size reduction.

Original Optimized WebP Reduction
1-nature-landscape.png (580kB) 1-nature-landscape.51SGon9e_2putX.webp (34kB) 94%
2-scifi-cyberpunk.png (631kB) 2-scifi-cyberpunk.JjhCW2vR_Z1taUln.webp (49kB) 92%
3-portrait-realistic.png (588kB) 3-portrait-realistic.BWz-0tfI_m5CB4.webp (26kB) 96%
4-abstract-artistic.png (677kB) 4-abstract-artistic.Cgf-1GlJ_1ATgsX.webp (50kB) 93%
5-food-culinary.png (559kB) 5-food-culinary.BWr7H010_Z20apFE.webp (30kB) 95%