Overview
There are several approaches to image optimization in SvelteKit:Vite assets
Built-in asset handling with imports
@sveltejs/enhanced-img
Automatic optimization and responsive images
CDN
Dynamic optimization for CMS images
Vite’s built-in handling
Vite automatically processes imported assets for improved performance:- Adds hashes to filenames for caching
- Inlines small assets (below
assetsInlineLimit) - Works with CSS
url()function - Handles images, video, audio, and more
Vite asset handling is great for basic needs but doesn’t create responsive images or convert formats.
@sveltejs/enhanced-img
The enhanced image plugin provides automatic optimization:What it does
Format optimization
Format optimization
Automatically generates modern formats like
.avif and .webp with fallbacksResponsive images
Responsive images
Creates multiple sizes and generates
srcset attributes for different devicesIntrinsic dimensions
Intrinsic dimensions
Sets
width and height automatically to prevent layout shiftPrivacy protection
Privacy protection
Strips EXIF data from images
Basic usage
Use<enhanced:img> instead of <img>:
<img> wrapped by a <picture> with multiple formats and sizes.
Dynamic image selection
Import images with the?enhanced query parameter:
Responsive images with sizes
For large images, specifysizes to serve smaller versions on smaller devices:
Custom widths
Custom widths
Specify exact widths with the
w query parameter:Per-image transforms
Apply transforms via query parameters:blur- Gaussian blur radiusquality- Compression quality (0-100)flatten- Flatten alpha channelrotate- Rotation angleflip- Flip horizontallyflop- Flip vertically- And many more
Intrinsic dimensions
width and height are inferred automatically, but you can override them with CSS:
Build caching
The first build takes longer due to image processing. Results are cached in
./node_modules/.cache/imagetools for fast subsequent builds.Loading images from a CDN
For images from a CMS or database, use a CDN with dynamic optimization:@unpic/svelte
CDN-agnostic component supporting multiple providers
Cloudinary
Cloudinary’s official Svelte SDK
CDN benefits and tradeoffs
Benefits
Benefits
- Images optimized at request time
- More flexibility with sizes
- No build-time processing
- Dynamic image sources (CMS, user uploads)
Tradeoffs
Tradeoffs
- Potential usage costs
- First request may be slow (lazy generation)
- Requires proper caching strategy
- Additional dependency
Example with @unpic/svelte
Best practices
Choose the right solution
- Static assets in repo: Use Vite or
@sveltejs/enhanced-img - CMS images: Use CDN with
@unpic/svelte - Meta tags: Use Vite’s import handling
Provide high-quality sources
Images should be 2x the display size for HiDPI screens. Optimization tools will scale down but can’t add detail.
Use sizes attribute
For images larger than ~400px, specify
sizes so smaller devices load appropriate versions:Common pitfalls
Performance metrics
Proper image optimization improves:Fast-loading hero images improve LCP scores
Setting
width and height prevents layout shiftSmaller files and modern formats reduce data transfer
Learn more
Web.dev: Optimize Cumulative Layout Shift