đ External Image Optimization
This page demonstrates how to optimize external images in Astro using various techniques.
đ¸ External Image Examples
Method 1: Regular img tag with optimization attributes
Uses loading="lazy" and decoding="async" for performance
Method 2: Responsive image with srcset
Uses srcset for responsive images
Method 3: Picture element with multiple formats
Uses picture element for format fallbacks
đ ī¸ External Image Optimization Techniques
đ Lazy Loading
Use loading="lazy" to defer loading images until they're needed
<img loading="lazy" ... /> ⥠Async Decoding
Use decoding="async" to prevent blocking the main thread
<img decoding="async" ... /> đą Responsive Images
Use srcset and sizes for different screen sizes
<img srcset="small.jpg 300w, large.jpg 800w" /> đŧī¸ Modern Formats
Use <picture> element for WebP/AVIF with fallbacks
<picture>
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="...">
</picture> âī¸ Image CDN Services
For better external image optimization, consider using image CDN services:
Cloudinary
Automatic optimization, format conversion, and resizing
https://res.cloudinary.com/demo/image/fetch/w_400,f_auto,q_auto/https://example.com/image.jpg ImageKit
Real-time image optimization and transformation
https://ik.imagekit.io/demo/tr:w-400,f-auto,q-auto/https://example.com/image.jpg Cloudflare Images
Global CDN with automatic optimization
https://imagedelivery.net/account/image-id/w=400,f=auto,q=auto ⥠Performance Tips
- Preload critical images: Use
<link rel="preload" as="image">for above-the-fold images - Set dimensions: Always specify width and height to prevent layout shift
- Use appropriate formats: WebP for photos, SVG for icons and simple graphics
- Optimize quality: Use 80-85% quality for most images
- Consider aspect ratios: Use CSS aspect-ratio to maintain layout
- Implement blur placeholders: Show low-quality placeholders while loading
âšī¸ External vs Internal Images
| Aspect | Internal Images | External Images |
|---|---|---|
| Optimization | Automatic by Astro | Manual or CDN-based |
| Build Time | Processed during build | No build processing |
| Caching | Cached with site | Depends on external server |
| Control | Full control | Limited control |