Astro Image Optimization for Web
Astro Image Optimization for Web
Section titled “Astro Image Optimization for Web”Astro provides built-in image optimization capabilities through the @astrojs/image integration and the Image component. This document explores how images can be auto-optimized for the web in Astro and Starlight-based sites.
Current State: Static Images
Section titled “Current State: Static Images”Currently, the site uses markdown images with static file references:
These images are served as static files without optimization:
- No format conversion (WebP/AVIF)
- No responsive
srcsetgeneration - No automatic resizing
Astro Image Optimization Features
Section titled “Astro Image Optimization Features”1. @astrojs/image Integration
Section titled “1. @astrojs/image Integration”Astro provides the @astrojs/image package for image optimization:
pnpm add -D @astrojs/imageThis integration provides:
- Format conversion: Automatically converts images to WebP, AVIF, JPEG, PNG
- Responsive images: Generates
srcsetattributes for different screen sizes - Lazy loading: Native
loading="lazy"attribute - Aspect ratio preservation: Maintains image proportions
2. Image Component Usage
Section titled “2. Image Component Usage”Instead of markdown images, use the Image component:
---import { Image, getImage } from 'astro:assets';import heroImage from '../images/hero.png';
const optimizedImage = await getImage({ src: heroImage, width: 1200, quality: 75, formats: ['webp', 'avif', 'jpeg']});---
<Image src={optimizedImage} alt="Hero image" loading="lazy"/>3. Starlight Image Support
Section titled “3. Starlight Image Support”Starlight (the documentation theme used by this site) has built-in image handling:
- Markdown images: Starlight automatically optimizes images in markdown files when using the
@astrojs/imageintegration - Featured images: Blog posts and pages can have featured images that are optimized
- Social images: Open Graph images are automatically generated and optimized
Enabling Image Optimization
Section titled “Enabling Image Optimization”Step 1: Install @astrojs/image
Section titled “Step 1: Install @astrojs/image”cd sitepnpm add -D @astrojs/imageStep 2: Configure in astro.config.mjs
Section titled “Step 2: Configure in astro.config.mjs”import { defineConfig } from 'astro/config';import starlight from '@astrojs/starlight';import starlightBlog from 'starlight-blog';import { image } from '@astrojs/image';
export default defineConfig({ site: 'https://michael.wegener.engineering', trailingSlash: 'never', integrations: [ starlight({ // ... starlight config }), image(), // Add image optimization integration ],});Step 3: Update Markdown Images
Section titled “Step 3: Update Markdown Images”With @astrojs/image configured, markdown images are automatically optimized:
Astro will:
- Detect the image format
- Generate WebP and AVIF versions
- Create a
srcsetwith multiple sizes - Serve the optimal format based on browser support
Image Optimization Benefits
Section titled “Image Optimization Benefits”1. Format Optimization
Section titled “1. Format Optimization”- WebP: 25-35% smaller than PNG/JPEG with similar quality
- AVIF: 50% smaller than JPEG with similar quality (modern browsers)
- Fallback: JPEG/PNG for older browsers
2. Responsive Images
Section titled “2. Responsive Images”- Multiple
srcsetsizes for different screen resolutions sizesattribute for responsive sizing- Automatic
srcsetgeneration based on image dimensions
3. Performance Metrics
Section titled “3. Performance Metrics”- LCP (Largest Contentful Paint): Faster image loading
- CLS (Cumulative Layout Shift): Preserved aspect ratios prevent layout shifts
- FID (First Input Delay): Reduced main thread blocking
Current Site Image Assets
Section titled “Current Site Image Assets”The site has images in:
site/assets/images/image-generation/- Blog post images (PNG format)site/assets/images/team/- Team member photos (AVIF, JPEG, WEBP formats)site/assets/images/home/- Home page images (AVIF, JPEG, WEBP formats)
Note: The team and home images already use modern formats (AVIF, WEBP) with fallbacks to JPEG/PNG, which is the recommended approach for static image assets.
Recommendations
Section titled “Recommendations”- For markdown images: Use the
@astrojs/imageintegration to enable automatic optimization - For static assets: Continue using modern formats (AVIF, WebP) with JPEG/PNG fallbacks
- For blog featured images: Use the
Imagecomponent or ensure markdown images are optimized - For social/OG images: Ensure images are under 1200x630px and use WebP format