Astro Image Optimization & Lazy Loading
Learn how to properly implement optimized image loading in Astro applications using native lazy loading, modern formats, and responsive design patterns.
Demo Details
Duration: 10-15 minutes
Category: Frontend Development
Complexity: Intermediate
Technologies
Astro Native Lazy Loading WebP AVIF Responsive Images
Watch Demo
Try Interactive
Launch DemoKey Features
- • Native Lazy Loading
- • WebP/AVIF Support
- • Responsive Images
- + 1 more features
Astro Image Optimization: Best Practices Guide
This comprehensive demo showcases the correct way to handle images in Astro applications, demonstrating performance optimization techniques and modern web standards for image loading.
Demo Overview
Learn how to implement optimized image loading that provides:
- Lightning-fast performance with native lazy loading
- Modern format support (WebP, AVIF) with automatic fallbacks
- Responsive design with proper sizing for all devices
- SEO optimization with proper alt text and structured data
- Accessibility compliance following WCAG guidelines
Code Examples
1. Basic Astro Image Usage
---
// Import the Image component and your assets
import { Image } from 'astro:assets';
import heroImage from '@/assets/hero-image.jpg';
---
<!-- Basic optimized image -->
<Image
src={heroImage}
alt="Description of the image"
width={800}
height={600}
format="webp"
quality={85}
/>2. Lazy Loading with LazyImage Component
---
// Import our custom LazyImage wrapper
import LazyImage from '@/components/LazyImage.astro';
import productImage from '@/assets/product-shot.png';
---
<!-- Lazy loaded image with native browser lazy loading -->
<LazyImage
src={productImage}
alt="Product showcase image"
width={600}
height={400}
loading="lazy"
fetchpriority="low"
format="webp"
quality={80}
/>3. Hero Images (Eager Loading)
---
import LazyImage from '@/components/LazyImage.astro';
import heroBackground from '@/assets/hero-bg.jpg';
---
<!-- Above-the-fold images should load eagerly -->
<LazyImage
src={heroBackground}
alt="Company headquarters building"
width={1920}
height={1080}
loading="eager"
fetchpriority="high"
format="webp"
quality={90}
class="hero-image"
/>4. Responsive Images with Multiple Sizes
---
import LazyImage from '@/components/LazyImage.astro';
import responsiveImage from '@/assets/responsive-example.jpg';
---
<!-- Responsive image with different sizes for different viewports -->
<LazyImage
src={responsiveImage}
alt="Responsive design example"
widths={[400, 800, 1200, 1600]}
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
format="webp"
quality={85}
loading="lazy"
class="responsive-image"
/>5. Art Direction with Different Images
---
import { Picture } from 'astro:assets';
import mobileImage from '@/assets/mobile-version.jpg';
import desktopImage from '@/assets/desktop-version.jpg';
---
<!-- Different images for different screen sizes -->
<Picture
src={desktopImage}
alt="Service overview"
widths={[400, 800, 1200]}
formats={['webp', 'jpg']}
loading="lazy"
class="art-direction-image"
>
<!-- Mobile-specific image -->
<source
media="(max-width: 768px)"
srcset={mobileImage}
/>
</Picture>Performance Best Practices
Loading Strategy Guidelines
- Above-the-fold images: Use
loading="eager"andfetchpriority="high" - Below-the-fold images: Use
loading="lazy"andfetchpriority="low" - Critical images: Use
decoding="sync"for important content - Non-critical images: Use
decoding="async"(default)
Format Selection
---
// Automatic format selection based on browser support
const optimizedImage = {
format: 'webp', // First choice: WebP for most browsers
quality: 85, // Good balance of quality vs file size
// Astro automatically provides JPEG/PNG fallbacks
};
---Sizing Guidelines
---
// Define widths based on your layout breakpoints
const responsiveSizes = {
widths: [320, 640, 768, 1024, 1280, 1920],
sizes: "(max-width: 320px) 280px, (max-width: 768px) 640px, (max-width: 1024px) 768px, 1024px"
};
---Common Patterns
1. Card Images
<!-- Product/service cards -->
<LazyImage
src={cardImage}
alt="Service description"
width={300}
height={200}
format="webp"
quality={80}
loading="lazy"
class="card-image"
/>2. Avatar Images
<!-- User avatars - small, round -->
<LazyImage
src={avatarImage}
alt={`${userName}'s profile picture`}
width={64}
height={64}
format="webp"
quality={90}
loading="lazy"
class="avatar"
/>3. Gallery Images
<!-- Image gallery with lightbox -->
<LazyImage
src={galleryImage}
alt="Gallery image description"
width={400}
height={300}
format="webp"
quality={85}
loading="lazy"
fetchpriority="low"
class="gallery-thumbnail"
/>SEO & Accessibility
Proper Alt Text
<!-- Good: Descriptive alt text -->
<LazyImage
src={teamPhoto}
alt="Cloud Intelligence team meeting in San Francisco office, 12 people around conference table"
width={800}
height={600}
/>
<!-- Bad: Generic alt text -->
<LazyImage
src={teamPhoto}
alt="team photo"
width={800}
height={600}
/>Structured Data
---
// Add structured data for images
const imageStructuredData = {
"@context": "https://schema.org",
"@type": "ImageObject",
"url": "/hero-image.webp",
"width": 1920,
"height": 1080,
"caption": "Modern office space with cloud computing infrastructure"
};
---Performance Metrics
This implementation typically achieves:
- Largest Contentful Paint (LCP): < 2.5s
- Cumulative Layout Shift (CLS): < 0.1
- File Size Reduction: 60-80% with WebP format
- Loading Performance: 90+ Lighthouse score
Try the Interactive Demo
The live demo includes:
- Format comparison: See WebP vs JPEG file sizes
- Lazy loading visualization: Watch images load as you scroll
- Responsive behavior: Resize your browser to see different image sizes
- Performance metrics: Real-time loading performance data
- Accessibility testing: Screen reader and keyboard navigation
Implementation Checklist
- Use
LazyImagecomponent for all images - Set appropriate
loadingattribute (eagerfor above-fold,lazyfor below-fold) - Choose correct
fetchpriority(highfor critical,lowfor non-critical) - Specify
widthandheightto prevent layout shift - Use WebP format with quality 80-90
- Write descriptive
alttext - Set responsive
sizesfor different viewports - Test on mobile devices and slow connections
Want to optimize your image loading? Contact our team to audit your current implementation and improve your site’s performance.