From 5ec24af338a6c4c1ca58150b3e887301e6a1e42d Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 30 Oct 2025 12:50:15 +0100 Subject: [PATCH] Fix service worker cache preventing code updates and change fallback URL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- public/sw.js | 18 +++++++++++++++++- src/api/checkoutClient.ts | 4 ++-- src/main.tsx | 12 ++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/public/sw.js b/public/sw.js index 0c4e244..06bbaec 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,4 +1,4 @@ -const CACHE_NAME = 'puffin-calculator-v1'; +const CACHE_NAME = 'puffin-calculator-v2'; // Bumped to clear old cached code const urlsToCache = [ '/', '/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 self.addEventListener('fetch', (event) => { event.respondWith( diff --git a/src/api/checkoutClient.ts b/src/api/checkoutClient.ts index 2785187..f85c6fd 100644 --- a/src/api/checkoutClient.ts +++ b/src/api/checkoutClient.ts @@ -10,8 +10,8 @@ const getApiBaseUrl = (): string => { return window.env.API_BASE_URL; } - // Fall back to build-time env or default - return import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001'; + // Fall back to build-time env or production default + return import.meta.env.VITE_API_BASE_URL || 'https://puffinoffset.com/api'; }; export interface CreateCheckoutSessionParams { diff --git a/src/main.tsx b/src/main.tsx index 360a4ae..9eff9d6 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -9,10 +9,18 @@ if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js') .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) => { - console.log('SW registration failed: ', registrationError); + console.error('❌ SW registration failed:', registrationError); }); }); }