Skip to main content
Proper caching is crucial for frontend performance. Mizu implements intelligent caching based on asset types, content fingerprinting, and best practices.

Overview

The frontend middleware automatically applies caching based on three factors:
  1. File type (HTML, JS, CSS, images, etc.)
  2. Content fingerprinting (hash in filename)
  3. Custom patterns (your configuration)

Default Caching Strategy

How Content Hashing Works

Build Tools Generate Hashes

When you build your frontend, tools like Vite add content hashes to filenames:
The hash is based on file contents. If the file changes, the hash changes:

Why This Matters

Old approach (without hashing):
Problem: Browsers cache main.js. When you update it, users might still see the old cached version. New approach (with hashing):
Benefit: When you update the file, the filename changes:
Browsers see a new filename and fetch the new file. The old file stays cached harmlessly.

Asset Classification

Mizu automatically classifies assets:

Hash Detection Pattern

Mizu looks for this pattern in filenames:
Examples that match:
  • main.a1b2c3.js βœ…
  • vendor-xyz789.css βœ…
  • chunk.ABC123DEF.js βœ…
  • logo_12ab34cd.png βœ…
Examples that don’t match:
  • 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:
Pattern matching order:
  1. Check custom patterns first
  2. If no match, check if file has hash
  3. If hashed β†’ use HashedAssets duration
  4. If unhashed β†’ use UnhashedAssets duration
  5. If HTML β†’ use HTML duration

Environment-Specific Caching

Cache-Control Directives

immutable

Hashed assets get the immutable directive:
What it means:
  • public: Can be cached by browsers and CDNs
  • max-age=31536000: Cache for 1 year (in seconds)
  • immutable: File will never change (safe to cache forever)
Modern browsers skip revalidation for immutable resources.

no-cache vs no-store

HTML files get aggressive no-cache headers:
What each means:
  • no-cache: Revalidate before using cached copy
  • no-store: Don’t cache at all
  • must-revalidate: Obey the rules strictly
  • Pragma: no-cache: HTTP/1.0 compatibility
  • Expires: 0: Old-style expiration

CDN Caching

When using a CDN (CloudFlare, Fastly, etc.):
CDN benefits:
  • 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:
Angular:

2. Never Cache HTML Files

Always set HTML caching to 0:
Why: HTML files reference the hashed assets. If HTML is cached, users might request old asset filenames that no longer exist.

3. Use Longer Cache for Fonts

Fonts rarely change:

4. Test Cache Headers

Check headers with curl:
Should show:

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:
Programmatically:

Verify Cache Headers

Common Issues

Problem: Users see old version after deployment Solution: Check that:
  1. HTML files have cache-control: no-cache
  2. Asset filenames include content hash
  3. Build generated new hashes
Problem: Assets not caching Solution: Check that:
  1. Filenames include hash pattern
  2. Server is returning cache headers
  3. HTTPS is enabled (required for some cache APIs)

Next Steps

Production Mode

Production optimization guide

Security

Security headers and best practices

Manifest

Build manifest integration

Static Hosting

Deploy to CDN