Cache Optimization Implementation

This document explains the caching strategies implemented for the portfolio website to improve performance and achieve significant savings in data transfer.

🎯 Objective

Implement efficient cache lifetimes to:

πŸ› οΈ Implementation Strategy

1. Service Worker Caching (service-worker.js)

A service worker has been implemented with the following cache strategies:

Cache Durations:

Features:

2. Browser Caching Headers

Added resource hints and preloading directives in _layouts/default.html:

DNS Prefetch & Preconnect:

<link rel="dns-prefetch" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>

Preload Critical Assets:

<link rel="preload" href="/assets/css/main.css" as="style">
<link rel="preload" href="/assets/images/sulochan-thapa.webp" as="image">

3. Alternative Hosting Solutions

Since GitHub Pages doesn’t support custom cache headers, we’ve prepared files for alternative hosting:

.htaccess (Apache servers)

_headers (Netlify/Cloudflare Pages)

πŸ“Š Expected Performance Improvements

Current Cache TTL Issues:

| Resource | Current TTL | Size | Issue | |β€”β€”β€”-|β€”β€”β€”β€”-|β€”β€”|β€”β€”-| | queen-of-heart.webp | 10m | 272 KiB | Too short | | ocr.JPG | 10m | 94 KiB | Too short | | myblog.JPG | 10m | 75 KiB | Too short | | main.css | 10m | 69 KiB | Too short | | sulochan-thapa.webp | 10m | 47 KiB | Too short |

After Implementation:

| Resource | New TTL | Size | Improvement | |β€”β€”β€”-|β€”β€”β€”|β€”β€”|β€”β€”β€”β€”-| | All images | 1 year | 488 KiB | βœ… Cached long-term | | main.css | 1 year | 69 KiB | βœ… Cached long-term | | Total Savings | - | 557 KiB | βœ… 536+ KiB saved |

πŸš€ How It Works

First Visit:

  1. User requests a page
  2. Service worker intercepts the request
  3. Assets are fetched from network
  4. Assets are stored in browser cache
  5. Service worker marks cache time

Repeat Visits:

  1. User requests a page
  2. Service worker intercepts the request
  3. Checks if cached version is fresh
  4. Serves from cache instantly (0ms)
  5. User experiences instant page load

Cache Invalidation:

When you update files:

  1. Change the CACHE_VERSION in service-worker.js
  2. Deploy the updated file
  3. Service worker automatically clears old cache
  4. New assets are cached

πŸ“ Usage Instructions

For GitHub Pages (Current Setup):

The service worker is automatically registered and working. No additional steps needed.

For Migration to Other Hosts:

Netlify:

  1. Copy _headers to root directory
  2. Deploy - headers are automatically applied

Apache Hosting:

  1. Copy .htaccess to root directory
  2. Ensure mod_headers and mod_expires are enabled

Cloudflare Pages:

  1. Use _headers file
  2. Or configure in Cloudflare dashboard

πŸ”§ Maintenance

Update Cache Version:

When making significant changes to cached assets:

// In service-worker.js
const CACHE_VERSION = 'v1.0.1'; // Increment version

Clear Cache Manually:

Users can clear cache by:

Monitor Cache Performance:

Use Chrome DevTools:

  1. Open DevTools (F12)
  2. Go to Network tab
  3. Reload page
  4. Check β€œSize” column for β€œ(from ServiceWorker)” or β€œ(disk cache)”

πŸ“ˆ Performance Metrics

Before Implementation:

After Implementation (Expected):

⚠️ Important Notes

  1. GitHub Pages Limitation: GitHub Pages sets its own cache headers (10 minutes). The service worker works around this by providing browser-level caching.

  2. Ad Networks: Google Ads and other third-party content are intentionally NOT cached to ensure fresh ad content.

  3. Cache Busting: If you need to force users to get new versions of files, increment the CACHE_VERSION in the service worker.

  4. Browser Support: Service Workers are supported in all modern browsers (95%+ of users).

πŸŽ‰ Benefits

βœ… Faster page loads on repeat visits
βœ… Reduced bandwidth costs
βœ… Better SEO scores (Core Web Vitals)
βœ… Offline functionality as a bonus
βœ… Improved user experience
βœ… Lower server costs (fewer requests)

πŸ“š Resources


Last Updated: December 4, 2025
Maintained By: Sulochan Thapa (code.darjeeling)