Qasar Younis

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

How it works:

  1. First visit: Assets downloaded and cached
  2. Repeat visits: Assets served instantly from cache (0ms load time!)
  3. Background updates: Service worker checks for updates periodically

Cache Versions:

2. Browser HTTP Caching

File: _headers (for CDN/Cloudflare)

Strategy: Very aggressive caching - most content is static

Note: GitHub Pages doesn’t support custom headers natively. If you use:

3. Browser Native Caching

Automatic: Browser handles this based on HTTP headers

πŸ“Š Performance Impact

First Visit:

Repeat Visits:

πŸ”§ How to Update Cache

When You Update Content:

  1. Update Service Worker Version:
    // In sw.js, change version number
    const CACHE_NAME = 'qyco-v1.0.1'; // Increment version
    
  2. Deploy Changes:
    • Push to GitHub
    • Service worker automatically updates
    • Old cache cleaned up
    • New content cached

Force Cache Refresh:

🎯 What Gets Cached

Immediately Cached (on install):

Cached on First Request:

NOT Cached:

πŸ” Testing Caching

Test Service Worker:

  1. Open DevTools β†’ Application tab β†’ Service Workers
  2. Check β€œUpdate on reload”
  3. Reload page - should see service worker active
  4. Go offline (DevTools β†’ Network β†’ Offline)
  5. Reload page - should still work!

Test Cache:

  1. First visit: Check Network tab, see all requests
  2. Second visit: Most requests should show β€œ(from ServiceWorker)” or β€œ(from disk cache)”
  3. Load time should be < 100ms

Verify Cache Headers:

  1. DevTools β†’ Network tab
  2. Click on any asset
  3. Check β€œResponse Headers” for Cache-Control

πŸ“ˆ Expected Results

Repeat Visit Performance:

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:

πŸ› οΈ 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

  1. Service Worker Scope:
    • Must be served from root (/sw.js)
    • Works for entire domain
    • HTTPS required (GitHub Pages provides this)
  2. Cache Size Limits:
    • Browsers limit cache size (~50-100MB)
    • Service worker automatically manages this
    • Old caches cleaned up automatically
  3. Updates:
    • Service worker checks for updates every minute
    • Users get new version on next visit
    • Old cache cleaned up automatically
  4. 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:

When to Update Cache Version:

πŸŽ‰ Result

Repeat visits are now INSTANT!

Your site now feels like a native app! πŸš€