1- const cacheName = 'java-evolution-cache-v1' ;
2- const assetsToCache = [
3- '/' ,
4- '/JavaEvolution-Learning-Growing-Mastering/index.html' ,
5- '/JavaEvolution-Learning-Growing-Mastering/assets/style.css' ,
6- '/JavaEvolution-Learning-Growing-Mastering/assets/script.js' ,
7- // Add more files as needed
1+ const CACHE_NAME = "java-evolution-cache-v2" ;
2+ const urlsToCache = [
3+ "/JavaEvolution-Learning-Growing-Mastering/" ,
4+ "/JavaEvolution-Learning-Growing-Mastering/index.html" ,
5+ "/JavaEvolution-Learning-Growing-Mastering/assets/style.css" ,
6+ "/JavaEvolution-Learning-Growing-Mastering/assets/script.js" ,
7+ "/JavaEvolution-Learning-Growing-Mastering/assets/favicon-96x96.png" ,
8+ "/JavaEvolution-Learning-Growing-Mastering/assets/apple-touch-icon.png" ,
9+ // Add more assets like images, fonts, etc.
810] ;
911
10- self . addEventListener ( "install" , ( event ) => {
11- event . waitUntil (
12- caches . open ( cacheName ) . then ( ( cache ) => cache . addAll ( assetsToCache ) )
12+ // Install event: caching essential files
13+ self . addEventListener ( "install" , ( e ) => {
14+ e . waitUntil (
15+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
16+ return cache . addAll ( urlsToCache ) ;
17+ } )
1318 ) ;
19+ console . log ( "✅ Service Worker Installed & Cached" ) ;
1420} ) ;
1521
16- self . addEventListener ( "fetch" , ( event ) => {
17- event . respondWith (
18- caches . match ( event . request ) . then ( ( response ) => response || fetch ( event . request ) )
22+ // Activate event: cleanup old caches
23+ self . addEventListener ( "activate" , ( e ) => {
24+ e . waitUntil (
25+ caches . keys ( ) . then ( ( cacheNames ) =>
26+ Promise . all (
27+ cacheNames
28+ . filter ( ( name ) => name !== CACHE_NAME )
29+ . map ( ( name ) => caches . delete ( name ) )
30+ )
31+ )
1932 ) ;
33+ console . log ( "⚙️ Old caches cleaned up" ) ;
2034} ) ;
2135
22-
23- /*
24- self.addEventListener("install", function (e) {
25- console.log("Service Worker Installed");
26- });
27-
28- self.addEventListener("fetch", function (e) {
29- // Just let requests pass through for now
30- });*/
36+ // Fetch event: serve cached files when offline
37+ self . addEventListener ( "fetch" , ( e ) => {
38+ e . respondWith (
39+ caches . match ( e . request ) . then ( ( response ) => {
40+ return response || fetch ( e . request ) ;
41+ } )
42+ ) ;
43+ } ) ;
0 commit comments