Matt 5ec24af338
All checks were successful
Build and Push Docker Images / docker (push) Successful in 44s
Fix service worker cache preventing code updates and change fallback URL
- Bumped service worker cache version to v2 to force cache invalidation
- Added activate event to explicitly clear old caches
- Enhanced service worker logging with update checking
- Changed API fallback URL from localhost:3001 to https://puffinoffset.com/api

This fixes the issue where new code deployments were not being used due to
service worker caching the old JavaScript with the timing bug.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-30 12:50:15 +01:00

66 lines
1.5 KiB
JavaScript

const CACHE_NAME = 'puffin-calculator-v2'; // Bumped to clear old cached code
const urlsToCache = [
'/',
'/mobile-app',
'/static/js/bundle.js',
'/static/css/main.css',
'/puffinOffset.webp',
'/manifest.json'
];
// Install event - cache resources
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
return cache.addAll(urlsToCache);
})
.catch((error) => {
console.log('Cache install failed:', error);
})
);
});
// Activate event - clear old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Clearing old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
// Fetch event - serve from cache when offline
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// Return cached version or fetch from network
return response || fetch(event.request);
}
)
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});