Overview
The frontend middleware automatically applies caching based on three factors:- File type (HTML, JS, CSS, images, etc.)
- Content fingerprinting (hash in filename)
- Custom patterns (your configuration)
Default Caching Strategy
| Asset Type | Pattern | Cache Duration | Cache-Control |
|---|---|---|---|
| Hashed assets | *.abc123.js | 1 year | public, max-age=31536000, immutable |
| Unhashed assets | logo.png | 1 week | public, max-age=604800 |
| HTML files | *.html | None | no-cache, no-store, must-revalidate |
| Source maps | *.map | None | no-cache |
How Content Hashing Works
Build Tools Generate Hashes
When you build your frontend, tools like Vite add content hashes to filenames:Why This Matters
Old approach (without hashing):main.js. When you update it, users might still see the old cached version.
New approach (with hashing):
Asset Classification
Mizu automatically classifies assets:Hash Detection Pattern
Mizu looks for this pattern in filenames:main.a1b2c3.js✅vendor-xyz789.css✅chunk.ABC123DEF.js✅logo_12ab34cd.png✅
main.js❌ (no hash)version-1.2.3.js❌ (not a hex hash)user-123.png❌ (too short)
Custom Cache Configuration
Override Defaults
Pattern-Based Caching
Cache specific file types differently:- Check custom patterns first
- If no match, check if file has hash
- If hashed → use
HashedAssetsduration - If unhashed → use
UnhashedAssetsduration - If HTML → use
HTMLduration
Environment-Specific Caching
Cache-Control Directives
immutable
Hashed assets get theimmutable directive:
public: Can be cached by browsers and CDNsmax-age=31536000: Cache for 1 year (in seconds)immutable: File will never change (safe to cache forever)
no-cache vs no-store
HTML files get aggressive no-cache headers:no-cache: Revalidate before using cached copyno-store: Don’t cache at allmust-revalidate: Obey the rules strictlyPragma: no-cache: HTTP/1.0 compatibilityExpires: 0: Old-style expiration
CDN Caching
When using a CDN (CloudFlare, Fastly, etc.):- Files cached at edge locations
- Faster delivery to users
- Reduced origin server load
- Mizu’s cache headers work automatically
Best Practices
1. Always Use Asset Fingerprinting
Ensure your build tool fingerprints assets: Vite:2. Never Cache HTML Files
Always set HTML caching to 0:3. Use Longer Cache for Fonts
Fonts rarely change:4. Test Cache Headers
Check headers with curl:5. Monitor Cache Hit Rates
Use CDN analytics to track:- Cache hit rate (aim for >90%)
- Bandwidth saved
- Origin requests (should be minimal)
Debugging Cache Issues
Clear Browser Cache
Chrome:Verify Cache Headers
Common Issues
Problem: Users see old version after deployment Solution: Check that:- HTML files have
cache-control: no-cache - Asset filenames include content hash
- Build generated new hashes
- Filenames include hash pattern
- Server is returning cache headers
- HTTPS is enabled (required for some cache APIs)