@@ -4,6 +4,9 @@ if (unoConfig.environmentVariables["UNO_BOOTSTRAP_DEBUGGER_ENABLED"] !== "True")
4
4
console . debug ( "[ServiceWorker] Initializing" ) ;
5
5
let uno_enable_tracing = unoConfig . uno_enable_tracing ;
6
6
7
+ // Get the number of fetch retries from environment variables or default to 1
8
+ const fetchRetries = parseInt ( unoConfig . environmentVariables [ "UNO_BOOTSTRAP_FETCH_RETRIES" ] || "1" ) ;
9
+
7
10
self . addEventListener ( 'install' , function ( e ) {
8
11
console . debug ( '[ServiceWorker] Installing offline worker' ) ;
9
12
e . waitUntil (
@@ -114,10 +117,36 @@ if (unoConfig.environmentVariables["UNO_BOOTSTRAP_DEBUGGER_ENABLED"] !== "True")
114
117
return cachedResponse ;
115
118
}
116
119
120
+ // Add retry mechanism - attempt to fetch again if retries are configured
121
+ if ( fetchRetries > 0 ) {
122
+ console . debug ( `[ServiceWorker] Resource not in cache, attempting ${ fetchRetries } network retries for: ${ requestClone . url } ` ) ;
123
+
124
+ // Try multiple fetch attempts with exponential backoff
125
+ for ( let retryCount = 0 ; retryCount < fetchRetries ; retryCount ++ ) {
126
+ try {
127
+ // Exponential backoff between retries (500ms, 1s, 2s, etc.)
128
+ const retryDelay = Math . pow ( 2 , retryCount ) * 500 ;
129
+ await new Promise ( resolve => setTimeout ( resolve , retryDelay ) ) ;
130
+
131
+ if ( uno_enable_tracing ) {
132
+ console . debug ( `[ServiceWorker] Retry attempt ${ retryCount + 1 } /${ fetchRetries } for: ${ requestClone . url } ` ) ;
133
+ }
134
+
135
+ // Need a fresh request clone for each retry
136
+ return await fetch ( event . request . clone ( ) ) ;
137
+ } catch ( retryErr ) {
138
+ if ( uno_enable_tracing ) {
139
+ console . debug ( `[ServiceWorker] Retry ${ retryCount + 1 } failed: ${ retryErr . message } ` ) ;
140
+ }
141
+ // Continue to next retry attempt
142
+ }
143
+ }
144
+ }
145
+
117
146
// Graceful error handling with a proper HTTP response
118
147
// Rather than letting the fetch fail with a generic error,
119
148
// we return a controlled 503 Service Unavailable response
120
- console . error ( `[ServiceWorker] Resource not available in cache or network: ${ requestClone . url } ` ) ;
149
+ console . error ( `[ServiceWorker] Resource not available in cache or network after ${ fetchRetries } retries : ${ requestClone . url } ` ) ;
121
150
return new Response ( 'Network error occurred, and resource was not found in cache.' , {
122
151
status : 503 ,
123
152
statusText : 'Service Unavailable' ,
0 commit comments