Image Caching
Image caching optimizes the delivery of images in your PWA by storing them locally in the browser. This feature uses the CacheFirst strategy, meaning images are served from the cache if available, with the network as a fallback.
Why Cache Images?
Images are often the largest assets on web pages. Caching them provides:
Faster page loads: Images load instantly from cache
Reduced bandwidth: Same images aren't downloaded multiple times
Offline access: Images remain available without network connection
Better user experience: No loading spinners or broken images
Default Configuration
By default, the bundle caches images with these settings:
Max entries: 60 images
Max age: 365 days (1 year)
Strategy: CacheFirst
Supported formats:
.ico,.png,.jpg,.jpeg,.gif,.svg,.webp,.bmp
Pattern matched:
/\.(ico|png|jpe?g|gif|svg|webp|bmp)$/Asset Cache vs Image Cache
Configuration Options
Enabling/Disabling
pwa:
serviceworker:
workbox:
image_cache:
enabled: true # Default: trueCustom Regex Pattern
Customize which image URLs are cached by modifying the regex pattern:
pwa:
serviceworker:
workbox:
image_cache:
enabled: true
regex: '/\.(png|jpe?g|svg|webp)$/' # Only these formatsUse cases:
Limit to specific formats (e.g., only PNG and JPG)
Match specific URL patterns (e.g.,
/\/images\/.*\.(png|jpg)$/)Exclude certain paths
Max Entries
Type: integer Default: 60
Maximum number of images to store in the cache. When the limit is reached, the least recently used images are removed.
pwa:
serviceworker:
workbox:
image_cache:
max_entries: 200 # Cache up to 200 imagesRecommendations:
Small apps: 30-60 images
Medium apps: 100-200 images
Large apps: 200-500 images
Image-heavy apps: 500+ images
Consider your users' storage and typical image count.
Max Age
Type: integer (seconds) or string (human-readable) Default: 31536000 (365 days)
How long images remain in the cache before being refreshed.
pwa:
serviceworker:
workbox:
image_cache:
max_age: 2592000 # 30 days in seconds
# or
max_age: 30 days # Human-readable format
# or
max_age: 6 months # Also supportedHuman-readable format examples:
1 hour,2 hours1 day,7 days,30 days1 week,2 weeks1 month,6 months1 year
Choosing the right max age:
Static branding images: 1 year or more
Product photos: 1-3 months
User avatars: 1-7 days
Frequently changing content: 1 day or less
Cache Name
Type: string Default: null (auto-generated)
Customize the cache storage name for organization and debugging.
pwa:
serviceworker:
workbox:
image_cache:
cache_name: 'product-images'Benefits of custom names:
Easier debugging in DevTools
Separate caches for different image types
Better cache management
Complete Example
pwa:
serviceworker:
enabled: true
src: "sw.js"
workbox:
image_cache:
enabled: true
cache_name: 'app-images'
max_entries: 150
max_age: 90 days
regex: '/\.(png|jpe?g|svg|webp)$/'Use Cases
E-commerce Site
Cache product images with moderate retention:
pwa:
serviceworker:
workbox:
image_cache:
enabled: true
cache_name: 'product-images'
max_entries: 300
max_age: 30 daysImage Gallery App
Prioritize capacity with shorter retention:
pwa:
serviceworker:
workbox:
image_cache:
enabled: true
cache_name: 'gallery-images'
max_entries: 500
max_age: 7 daysBlog with Static Images
Long retention for rarely changing images:
pwa:
serviceworker:
workbox:
image_cache:
enabled: true
max_entries: 100
max_age: 1 yearHow It Works
First request: Image is fetched from network and stored in cache
Subsequent requests: Image is served instantly from cache (CacheFirst strategy)
Cache miss: If image not in cache or expired, network request is made
Cache limit: When
max_entriesis reached, oldest images are removedExpiration: After
max_age, images are refreshed on next request
Debugging
To inspect the image cache in Chrome DevTools:
Open DevTools (F12)
Go to Application tab
Expand Cache Storage
Look for your cache (default or custom
cache_name)View cached images and their metadata
Best Practices
Balance capacity and storage: Don't cache excessive images on mobile devices
Use appropriate max_age: Match your content update frequency
Monitor cache size: Check DevTools to ensure cache isn't growing too large
Consider user storage: Mobile users have limited space
Use custom cache names: Makes debugging and management easier
The default configuration works well for most applications. Start with defaults and adjust based on your specific needs and user feedback.
Last updated
Was this helpful?