Skip to content

Commit 5a1aeda

Browse files
clairernovotnyjeromelaban
authored andcommitted
feat(service-worker): Add fetch retry mechanism and environment variable support
1 parent 261a601 commit 5a1aeda

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/features-environment-variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The bootstrapper provides a set of environment variables that reflect the config
2626
- `UNO_BOOTSTRAP_MONO_RUNTIME_MODE`, which specifies the runtime mode configuration (see above for valid values)
2727
- `UNO_BOOTSTRAP_LINKER_ENABLED`, which is set to `True` if the linker was enabled, otherwise `False`
2828
- `UNO_BOOTSTRAP_DEBUGGER_ENABLED`, which is set to `True` if the debugging support was enabled, otherwise `False`
29+
- `UNO_BOOTSTRAP_FETCH_RETRIES`, which specifies how many ties `fetch` should retry on a failed request. Defaults to `1`
2930
- `UNO_BOOTSTRAP_MONO_RUNTIME_CONFIGURATION`, which provides the mono runtime configuration, which can be can either be `release` or `debug`.
3031
- `UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES`, which provides a list of comma separated feature enabled in the runtime (e.g. `threads`)
3132
- `UNO_BOOTSTRAP_MONO_PROFILED_AOT`, which specifies if the package was built using a PG-AOT profile.

src/Uno.Wasm.Bootstrap/Embedded/service-worker.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ if (unoConfig.environmentVariables["UNO_BOOTSTRAP_DEBUGGER_ENABLED"] !== "True")
44
console.debug("[ServiceWorker] Initializing");
55
let uno_enable_tracing = unoConfig.uno_enable_tracing;
66

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+
710
self.addEventListener('install', function (e) {
811
console.debug('[ServiceWorker] Installing offline worker');
912
e.waitUntil(
@@ -114,10 +117,36 @@ if (unoConfig.environmentVariables["UNO_BOOTSTRAP_DEBUGGER_ENABLED"] !== "True")
114117
return cachedResponse;
115118
}
116119

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+
117146
// Graceful error handling with a proper HTTP response
118147
// Rather than letting the fetch fail with a generic error,
119148
// 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}`);
121150
return new Response('Network error occurred, and resource was not found in cache.', {
122151
status: 503,
123152
statusText: 'Service Unavailable',

src/Uno.Wasm.Bootstrap/ShellTask.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public partial class ShellTask_v0 : Task
125125

126126
public bool GenerateAOTProfile { get; set; }
127127

128+
public string FetchRetries { get; set; } = "1";
129+
128130
public bool EnableThreads { get; set; }
129131

130132
public ITaskItem[]? ReferencePath { get; set; }
@@ -590,6 +592,7 @@ private void GenerateConfig()
590592
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_PROFILED_AOT", isProfiledAOT.ToString());
591593
AddEnvironmentVariable("UNO_BOOTSTRAP_LINKER_ENABLED", (PublishTrimmed && RunILLink).ToString());
592594
AddEnvironmentVariable("UNO_BOOTSTRAP_DEBUGGER_ENABLED", (!Optimize).ToString());
595+
AddEnvironmentVariable("UNO_BOOTSTRAP_FETCH_RETRIES", FetchRetries);
593596
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_CONFIGURATION", "Release");
594597
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES", BuildRuntimeFeatures());
595598
AddEnvironmentVariable("UNO_BOOTSTRAP_APP_BASE", PackageAssetsFolder);

0 commit comments

Comments
 (0)