Caching Strategy for Repeat Visits
Caching Strategy for Repeat Visits
This document explains how the site is optimized for instant repeat visits through aggressive caching strategies.
π Caching Layers Implemented
1. Service Worker (Primary Caching)
File: sw.js
Strategy: Cache-First with Network Fallback
- Static Assets (CSS, JS, images): Cached forever, served instantly
- HTML Pages: Cached, but can be updated from network
- Images: Separate cache for better management
How it works:
- First visit: Assets downloaded and cached
- Repeat visits: Assets served instantly from cache (0ms load time!)
- Background updates: Service worker checks for updates periodically
Cache Versions:
- Update
CACHE_NAME in sw.js to force cache refresh
- Old caches automatically cleaned up on activation
2. Browser HTTP Caching
File: _headers (for CDN/Cloudflare)
Strategy: Very aggressive caching - most content is static
- Images/CSS/JS: 1 year cache (
max-age=31536000) - immutable, never changes
- HTML Pages: 24 hours cache (
max-age=86400) - most content is static
- Writings Page: 6 hours cache (
max-age=21600) - updates a few times a year
- Favicon: 1 month cache (
max-age=2592000) - rarely changes
Note: GitHub Pages doesnβt support custom headers natively. If you use:
- Cloudflare: Add these headers in Cloudflare dashboard
- Netlify: Create
_headers file (already created)
- Other CDN: Configure headers in CDN settings
3. Browser Native Caching
Automatic: Browser handles this based on HTTP headers
- Browser automatically caches based on
Cache-Control headers
- Works even without service worker
- First line of defense for repeat visits
First Visit:
- Normal load time: ~2-3 seconds
- Assets downloaded and cached
Repeat Visits:
- With Service Worker: < 100ms (instant!)
- Without Service Worker: < 500ms (from browser cache)
- Offline: Site still works (service worker serves cached content)
π§ How to Update Cache
When You Update Content:
- Update Service Worker Version:
// In sw.js, change version number
const CACHE_NAME = 'qyco-v1.0.1'; // Increment version
- Deploy Changes:
- Push to GitHub
- Service worker automatically updates
- Old cache cleaned up
- New content cached
Force Cache Refresh:
- Users: Hard refresh (Ctrl+Shift+R / Cmd+Shift+R)
- Or: Clear browser cache
- Or: Update service worker version
π― What Gets Cached
- Homepage (
/)
- Main CSS (
/style.css)
- Hero image (
/assets/tom5t-adobe-license.png)
- Favicon
Cached on First Request:
- All HTML pages
- All images (photos, book covers, etc.)
- All CSS/JS files
- Navigation pages
NOT Cached:
- Google Analytics (bypassed)
- Google Fonts (bypassed - uses browser cache)
- External resources
π Testing Caching
Test Service Worker:
- Open DevTools β Application tab β Service Workers
- Check βUpdate on reloadβ
- Reload page - should see service worker active
- Go offline (DevTools β Network β Offline)
- Reload page - should still work!
Test Cache:
- First visit: Check Network tab, see all requests
- Second visit: Most requests should show β(from ServiceWorker)β or β(from disk cache)β
- Load time should be < 100ms
- DevTools β Network tab
- Click on any asset
- Check βResponse Headersβ for
Cache-Control
π Expected Results
| Resource Type |
First Visit |
Repeat Visit (Cached) |
| HTML Pages |
~500ms |
< 50ms β‘ |
| CSS/JS |
~200ms |
< 10ms β‘ |
| Images |
~1-2s |
< 20ms β‘ |
| Total |
~2-3s |
< 100ms β‘ |
Offline Support:
- β
Site works offline
- β
Images load from cache
- β
Navigation works
- β
All pages accessible
π οΈ Advanced: Customize Caching
Cache Specific Pages Longer:
Edit sw.js:
// Add to STATIC_ASSETS array
const STATIC_ASSETS = [
'/',
'/photos/',
'/books/',
// ... add pages you want cached immediately
];
Change Cache Duration:
Edit _headers file:
# Change max-age value (in seconds)
/assets/*
Cache-Control: public, max-age=31536000 # 1 year
Exclude from Cache:
Edit sw.js fetch handler:
// Skip certain URLs
if (url.pathname.includes('/admin/')) {
return; // Don't cache admin pages
}
π¨ Important Notes
- Service Worker Scope:
- Must be served from root (
/sw.js)
- Works for entire domain
- HTTPS required (GitHub Pages provides this)
- Cache Size Limits:
- Browsers limit cache size (~50-100MB)
- Service worker automatically manages this
- Old caches cleaned up automatically
- Updates:
- Service worker checks for updates every minute
- Users get new version on next visit
- Old cache cleaned up automatically
- Browser Support:
- β
Chrome/Edge: Full support
- β
Firefox: Full support
- β
Safari: Full support (iOS 11.3+)
- β οΈ Older browsers: Falls back to HTTP caching
π Maintenance
Regular Tasks:
- Update cache version when making major changes
- Monitor cache size (DevTools β Application β Storage)
- Test offline functionality periodically
When to Update Cache Version:
- Major design changes
- New asset structure
- Critical bug fixes
- New features added
π Result
Repeat visits are now INSTANT!
- First visit: Normal speed (~2-3s)
- Repeat visits: < 100ms (20-30x faster!)
- Offline: Site still works
- Better user experience
- Reduced server load
- Lower bandwidth costs
Your site now feels like a native app! π