Skip to content

Commit 9bc3aee

Browse files
committed
fix: improve worker error handling for asset fetching
1 parent 567fab3 commit 9bc3aee

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/worker.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ export default {
1919
// Handle root redirect to listRegistry.json
2020
if (url.pathname === '/') {
2121
const redirectUrl = new URL('/listRegistry.json', url.origin);
22-
const response = await env.ASSETS?.fetch(new Request(redirectUrl, request)) ||
23-
await fetch(redirectUrl);
22+
let response;
23+
24+
try {
25+
// Try to fetch from assets first
26+
if (env.ASSETS) {
27+
response = await env.ASSETS.fetch(new Request(redirectUrl, request));
28+
} else {
29+
response = await fetch(redirectUrl);
30+
}
31+
} catch (error) {
32+
console.error('Error fetching listRegistry.json:', error);
33+
// Fallback to direct fetch
34+
response = await fetch(redirectUrl);
35+
}
2436

2537
return new Response(response.body, {
2638
status: response.status,
@@ -36,7 +48,17 @@ export default {
3648
}
3749

3850
// For all other requests, fetch from static assets and add CORS headers
39-
const response = await env.ASSETS?.fetch(request) || await fetch(request);
51+
let response;
52+
try {
53+
if (env.ASSETS) {
54+
response = await env.ASSETS.fetch(request);
55+
} else {
56+
response = await fetch(request);
57+
}
58+
} catch (error) {
59+
console.error('Error fetching asset:', error);
60+
response = await fetch(request);
61+
}
4062

4163
// Clone the response to add CORS headers
4264
const newResponse = new Response(response.body, {

0 commit comments

Comments
 (0)