1- const  CACHE_NAME  =  'kevins-oracle-v1.0.0' ; 
1+ // Service Worker with proper update handling 
2+ const  CACHE_VERSION  =  'v1.0.1' ;  // This will be auto-updated by GitHub Actions 
3+ const  CACHE_NAME  =  `kevins-oracle-${ CACHE_VERSION }  ; 
4+ 
25const  urlsToCache  =  [ 
36  './' , 
47  './index.html' , 
58  './manifest.json' , 
69  './sw.js' , 
10+   './generators.js' , 
711  './icons/icon-72x72.svg' , 
812  './icons/icon-96x96.svg' , 
913  './icons/icon-128x128.svg' , 
@@ -14,43 +18,84 @@ const urlsToCache = [
1418  './icons/icon-512x512.svg' 
1519] ; 
1620
17- // Install event - cache essential files 
21+ // Install event - cache essential files and skip waiting  
1822self . addEventListener ( 'install' ,  event  =>  { 
1923  console . log ( 'Service Worker installing...' ) ; 
24+   
25+   // Skip waiting to activate immediately 
26+   self . skipWaiting ( ) ; 
27+   
2028  event . waitUntil ( 
2129    caches . open ( CACHE_NAME ) 
2230      . then ( cache  =>  { 
23-         console . log ( 'Opened cache'  ) ; 
31+         console . log ( 'Opened cache:'  ,   CACHE_NAME ) ; 
2432        return  cache . addAll ( urlsToCache ) ; 
2533      } ) 
26-       . then ( ( )  =>  self . skipWaiting ( ) ) 
2734  ) ; 
2835} ) ; 
2936
30- // Activate event - clean up old caches 
37+ // Activate event - clean up old caches and claim clients  
3138self . addEventListener ( 'activate' ,  event  =>  { 
3239  console . log ( 'Service Worker activating...' ) ; 
40+   
3341  event . waitUntil ( 
3442    caches . keys ( ) . then ( cacheNames  =>  { 
3543      return  Promise . all ( 
3644        cacheNames . map ( cacheName  =>  { 
45+           // Delete all caches that don't match current version 
3746          if  ( cacheName  !==  CACHE_NAME )  { 
3847            console . log ( 'Deleting old cache:' ,  cacheName ) ; 
3948            return  caches . delete ( cacheName ) ; 
4049          } 
4150        } ) 
4251      ) ; 
43-     } ) . then ( ( )  =>  self . clients . claim ( ) ) 
52+     } ) . then ( ( )  =>  { 
53+       // Claim all clients to ensure they use the updated service worker 
54+       return  self . clients . claim ( ) ; 
55+     } ) 
4456  ) ; 
4557} ) ; 
4658
4759// Fetch event - serve from cache, fall back to network 
4860self . addEventListener ( 'fetch' ,  event  =>  { 
61+   // Skip non-GET requests and browser extensions 
62+   if  ( event . request . method  !==  'GET' )  return ; 
63+   
4964  event . respondWith ( 
5065    caches . match ( event . request ) 
5166      . then ( response  =>  { 
5267        // Return cached version or fetch from network 
53-         return  response  ||  fetch ( event . request ) ; 
68+         if  ( response )  { 
69+           return  response ; 
70+         } 
71+         
72+         // Clone the request because it can only be used once 
73+         const  fetchRequest  =  event . request . clone ( ) ; 
74+         
75+         return  fetch ( fetchRequest ) . then ( response  =>  { 
76+           // Check if we received a valid response 
77+           if  ( ! response  ||  response . status  !==  200  ||  response . type  !==  'basic' )  { 
78+             return  response ; 
79+           } 
80+           
81+           // Clone the response because it can only be used once 
82+           const  responseToCache  =  response . clone ( ) ; 
83+           
84+           // Cache the new response for future requests 
85+           caches . open ( CACHE_NAME ) 
86+             . then ( cache  =>  { 
87+               cache . put ( event . request ,  responseToCache ) ; 
88+             } ) ; 
89+           
90+           return  response ; 
91+         } ) ; 
5492      } ) 
5593  ) ; 
94+ } ) ; 
95+ 
96+ // Listen for messages from the client 
97+ self . addEventListener ( 'message' ,  event  =>  { 
98+   if  ( event . data  &&  event . data . type  ===  'SKIP_WAITING' )  { 
99+     self . skipWaiting ( ) ; 
100+   } 
56101} ) ; 
0 commit comments