Fix service worker cache preventing code updates and change fallback URL
All checks were successful
Build and Push Docker Images / docker (push) Successful in 44s

- 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>
This commit is contained in:
Matt 2025-10-30 12:50:15 +01:00
parent 8d9f65868a
commit 5ec24af338
3 changed files with 29 additions and 5 deletions

View File

@ -1,4 +1,4 @@
const CACHE_NAME = 'puffin-calculator-v1'; const CACHE_NAME = 'puffin-calculator-v2'; // Bumped to clear old cached code
const urlsToCache = [ const urlsToCache = [
'/', '/',
'/mobile-app', '/mobile-app',
@ -21,6 +21,22 @@ self.addEventListener('install', (event) => {
); );
}); });
// 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 // Fetch event - serve from cache when offline
self.addEventListener('fetch', (event) => { self.addEventListener('fetch', (event) => {
event.respondWith( event.respondWith(

View File

@ -10,8 +10,8 @@ const getApiBaseUrl = (): string => {
return window.env.API_BASE_URL; return window.env.API_BASE_URL;
} }
// Fall back to build-time env or default // Fall back to build-time env or production default
return import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001'; return import.meta.env.VITE_API_BASE_URL || 'https://puffinoffset.com/api';
}; };
export interface CreateCheckoutSessionParams { export interface CreateCheckoutSessionParams {

View File

@ -9,10 +9,18 @@ if ('serviceWorker' in navigator) {
window.addEventListener('load', () => { window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js') navigator.serviceWorker.register('/sw.js')
.then((registration) => { .then((registration) => {
console.log('SW registered: ', registration); console.log('✅ Service Worker registered:', registration);
// Check for updates immediately
registration.update();
// Log when a new service worker is waiting
if (registration.waiting) {
console.log('⚠️ New service worker waiting. Reload page to activate.');
}
}) })
.catch((registrationError) => { .catch((registrationError) => {
console.log('SW registration failed: ', registrationError); console.error('❌ SW registration failed:', registrationError);
}); });
}); });
} }