Image Optimization Guide
This guide explains how to optimize images in the Tax Zero VitePress project for better performance and faster loading times.
Quick Start
Install Dependencies
bash
npm installOptimize All Images
bash
npm run optimize:imagesBuild with Optimized Images
bash
npm run optimize:images:prodOptimization Features
1. Automated Compression
- JPEG: Quality 80, progressive loading
- PNG: Maximum compression (level 9)
- WebP: Quality 80, high effort compression
- AVIF: Quality 80, high effort compression
2. Responsive Image Generation
Automatically creates multiple width variants:
- 400w for mobile
- 600w for tablets
- 800w for desktop
- 1200w for large screens
3. Build-time Optimization
Images are automatically optimized during the build process using:
vite-plugin-imageminfor lossless compressionsharpfor high-quality image processing
Manual Image Optimization
Before Adding New Images
- Use modern formats (WebP/AVIF) when possible
- Compress images using tools like TinyPNG or Squoosh
- Create appropriate size variants
- Use responsive images in your markup
Recommended Image Sizes
- Hero images: 1200px width max
- Card images: 400-600px width
- Icons/logos: Use SVG format
- Thumbnails: 200-300px width
Format Guidelines
- Photographs: Use WebP or AVIF with JPEG fallback
- Graphics/icons: Use SVG for scalability
- Logos: SVG or PNG with transparency
- Backgrounds: WebP or JPEG depending on complexity
Using Optimized Images
Responsive Image Markup
vue
<picture>
<source srcset="/images/hero-1200w.webp" media="(min-width: 1200px)">
<source srcset="/images/hero-800w.webp" media="(min-width: 800px)">
<source srcset="/images/hero-600w.webp" media="(min-width: 600px)">
<img src="/images/hero-400w.webp" alt="Hero image" loading="lazy">
</picture>Lazy Loading
Add loading="lazy" to images that are below the fold:
vue
<img src="/images/content-image.jpg" alt="Content" loading="lazy">Image Dimensions
Always specify width and height to prevent layout shift:
vue
<img
src="/images/example.jpg"
alt="Example"
width="800"
height="400"
loading="lazy"
>Performance Monitoring
Check Image Sizes
bash
# List all images with sizes
find docs/public/images -type f -exec ls -lh {} \;
# Check optimization results
npm run optimize:imagesWeb Vitals
The project includes web-vitals package to monitor:
- Largest Contentful Paint (LCP)
- Cumulative Layout Shift (CLS)
- First Input Delay (FID)
Troubleshooting
Common Issues
- Build fails: Ensure all dependencies are installed
- Large file sizes: Check if images are properly compressed
- Slow loading: Verify lazy loading is implemented
- Layout shift: Add width/height attributes
Manual Optimization Tools
- Squoosh - Web-based image compression
- TinyPNG - PNG compression
- ImageOptim - Mac app for compression
Best Practices
- Use modern formats (WebP, AVIF) with fallbacks
- Implement lazy loading for below-the-fold images
- Specify dimensions to prevent layout shift
- Compress before upload using the optimization script
- Use responsive images for different screen sizes
- Monitor performance using Web Vitals
- Test on real devices and network conditions
File Organization
docs/public/images/
├── hero-1200w.webp # Large screens
├── hero-800w.webp # Desktop
├── hero-600w.webp # Tablets
├── hero-400w.webp # Mobile
├── logo.svg # Vector logo
└── icons/ # Icon directory
├── feature1.svg
└── feature2.svg