@@ -69,8 +69,7 @@ self.addEventListener("fetch", (event) => {
6969});
7070*/
7171
72-
73- const CACHE_NAME = "java-evolution-cache-v5" ; // Updated version
72+ const CACHE_NAME = "java-evolution-cache-v6" ; // Updated version
7473const STATIC_ASSETS = [
7574 "/JavaEvolution-Learning-Growing-Mastering/" ,
7675 "/JavaEvolution-Learning-Growing-Mastering/default.html" ,
@@ -80,44 +79,29 @@ const STATIC_ASSETS = [
8079 "/JavaEvolution-Learning-Growing-Mastering/assets/favicon.ico" ,
8180 "/JavaEvolution-Learning-Growing-Mastering/assets/apple-touch-icon.png" ,
8281 "/JavaEvolution-Learning-Growing-Mastering/assets/site.webmanifest" ,
83- "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" // Cache CDN resource
82+ "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
8483] ;
8584
86- // Simple offline fallback page
87- const OFFLINE_PAGE = `
88- <!DOCTYPE html>
89- <html lang="en">
90- <head>
91- <meta charset="UTF-8">
92- <meta name="viewport" content="width=device-width, initial-scale=1.0">
93- <title>Offline</title>
94- <style>
95- body { font-family: 'Segoe UI', sans-serif; text-align: center; padding: 2rem; background: #f9f9f9; color: #333; }
96- h1 { font-size: 1.5rem; }
97- p { font-size: 1rem; }
98- </style>
99- </head>
100- <body>
101- <h1>You're Offline</h1>
102- <p>Please check your internet connection and try again.</p>
103- </body>
104- </html>
105- ` ;
85+ // Debounce function to limit cache updates
86+ function debounce ( func , wait ) {
87+ let timeout ;
88+ return function executedFunction ( ...args ) {
89+ const later = ( ) => {
90+ clearTimeout ( timeout ) ;
91+ func ( ...args ) ;
92+ } ;
93+ clearTimeout ( timeout ) ;
94+ timeout = setTimeout ( later , wait ) ;
95+ } ;
96+ }
10697
10798// Install event
10899self . addEventListener ( "install" , ( event ) => {
109100 console . log ( "[Service Worker] Installing and caching static assets" ) ;
110101 self . skipWaiting ( ) ;
111102 event . waitUntil (
112103 caches . open ( CACHE_NAME ) . then ( ( cache ) => {
113- // Cache static assets
114- return cache . addAll ( STATIC_ASSETS ) . then ( ( ) => {
115- // Cache offline page
116- const offlineResponse = new Response ( OFFLINE_PAGE , {
117- headers : { 'Content-Type' : 'text/html' }
118- } ) ;
119- return cache . put ( '/offline.html' , offlineResponse ) ;
120- } ) ;
104+ return cache . addAll ( STATIC_ASSETS ) ;
121105 } )
122106 ) ;
123107} ) ;
@@ -146,17 +130,27 @@ self.addEventListener("fetch", (event) => {
146130 event . respondWith (
147131 fetch ( request )
148132 . then ( ( networkResponse ) => {
149- // Cache successful response
150- return caches . open ( CACHE_NAME ) . then ( ( cache ) => {
151- cache . put ( request , networkResponse . clone ( ) ) ;
152- return networkResponse ;
153- } ) ;
133+ // Cache successful response asynchronously
134+ debounce ( ( ) => {
135+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
136+ cache . put ( request , networkResponse . clone ( ) ) ;
137+ // Limit cache size
138+ cache . keys ( ) . then ( ( keys ) => {
139+ if ( keys . length > 20 ) {
140+ cache . delete ( keys [ 0 ] ) ;
141+ }
142+ } ) ;
143+ } ) ;
144+ } , 100 ) ( ) ;
145+ return networkResponse ;
154146 } )
155147 . catch ( ( ) => {
156- // Serve cached page or offline fallback
157- return caches . match ( request ) . then ( ( cachedResponse ) => {
158- return cachedResponse || caches . match ( '/offline.html' ) ;
159- } ) ;
148+ // Fallback to cached default.html
149+ return caches . match ( '/JavaEvolution-Learning-Growing-Mastering/default.html' ) ||
150+ new Response ( 'Network error: Please check your connection.' , {
151+ status : 503 ,
152+ statusText : 'Service Unavailable'
153+ } ) ;
160154 } )
161155 ) ;
162156 return ;
@@ -167,12 +161,14 @@ self.addEventListener("fetch", (event) => {
167161 caches . match ( request ) . then ( ( cachedResponse ) => {
168162 // Return cached response if available
169163 if ( cachedResponse ) {
170- // Refresh cache in background
171- fetch ( request ) . then ( ( networkResponse ) => {
172- caches . open ( CACHE_NAME ) . then ( ( cache ) => {
173- cache . put ( request , networkResponse ) ;
174- } ) ;
175- } ) . catch ( ( ) => { } ) ; // Silent fail
164+ // Background cache refresh
165+ debounce ( ( ) => {
166+ fetch ( request ) . then ( ( networkResponse ) => {
167+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
168+ cache . put ( request , networkResponse ) ;
169+ } ) ;
170+ } ) . catch ( ( ) => { } ) ; // Silent fail
171+ } , 100 ) ( ) ;
176172 return cachedResponse ;
177173 }
178174
@@ -185,25 +181,29 @@ self.addEventListener("fetch", (event) => {
185181 ( request . url . startsWith ( self . location . origin ) ||
186182 request . url . startsWith ( 'https://cdnjs.cloudflare.com' ) )
187183 ) {
188- return caches . open ( CACHE_NAME ) . then ( ( cache ) => {
189- cache . put ( request , networkResponse . clone ( ) ) ;
190- // Limit cache size
191- cache . keys ( ) . then ( ( keys ) => {
192- if ( keys . length > 50 ) {
193- cache . delete ( keys [ 0 ] ) ;
194- }
184+ debounce ( ( ) => {
185+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
186+ cache . put ( request , networkResponse . clone ( ) ) ;
187+ // Limit cache size
188+ cache . keys ( ) . then ( ( keys ) => {
189+ if ( keys . length > 20 ) {
190+ cache . delete ( keys [ 0 ] ) ;
191+ }
192+ } ) ;
195193 } ) ;
196- return networkResponse ;
197- } ) ;
194+ } , 100 ) ( ) ;
198195 }
199196 return networkResponse ;
200197 } )
201198 . catch ( ( ) => {
202- // Fallback for non-HTML resources
199+ // Fallback for Font Awesome
203200 if ( request . url . includes ( 'font-awesome' ) ) {
204201 return caches . match ( 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css' ) ;
205202 }
206- return caches . match ( '/offline.html' ) ;
203+ return new Response ( 'Resource unavailable offline.' , {
204+ status : 503 ,
205+ statusText : 'Service Unavailable'
206+ } ) ;
207207 } ) ;
208208 } )
209209 ) ;
0 commit comments