@@ -15,17 +15,65 @@ export default {
1515 } ) ;
1616 }
1717
18- // For all requests, let Cloudflare handle static assets and just add CORS headers
19- // The worker will only add CORS headers, not handle the actual content
20- return new Response ( 'Worker is running but static assets should be handled by Cloudflare' , {
21- status : 200 ,
18+ // Handle root redirect to listRegistry.json
19+ if ( url . pathname === '/' ) {
20+ const redirectUrl = new URL ( '/listRegistry.json' , url . origin ) ;
21+ let response ;
22+
23+ try {
24+ if ( env . ASSETS ) {
25+ response = await env . ASSETS . fetch ( new Request ( redirectUrl , request ) ) ;
26+ } else {
27+ // Fallback for local development - fetch from local server
28+ response = await fetch ( redirectUrl ) ;
29+ }
30+ } catch ( error ) {
31+ console . error ( 'Error fetching listRegistry.json:' , error ) ;
32+ response = await fetch ( redirectUrl ) ;
33+ }
34+
35+ return new Response ( response . body , {
36+ status : response . status ,
37+ statusText : response . statusText ,
38+ headers : {
39+ ...response . headers ,
40+ 'Access-Control-Allow-Origin' : '*' ,
41+ 'Access-Control-Allow-Methods' : 'GET, HEAD, POST, OPTIONS' ,
42+ 'Access-Control-Allow-Headers' : 'Content-Type, X-Requested-With' ,
43+ 'Access-Control-Max-Age' : '86400' ,
44+ } ,
45+ } ) ;
46+ }
47+
48+ // For all other requests, let Cloudflare serve static assets
49+ // and add CORS headers to the response
50+ let response ;
51+
52+ try {
53+ if ( env . ASSETS ) {
54+ response = await env . ASSETS . fetch ( request ) ;
55+ } else {
56+ // Fallback for local development
57+ response = await fetch ( request ) ;
58+ }
59+ } catch ( error ) {
60+ console . error ( 'Error fetching asset:' , error ) ;
61+ response = await fetch ( request ) ;
62+ }
63+
64+ // Clone the response to add CORS headers
65+ const newResponse = new Response ( response . body , {
66+ status : response . status ,
67+ statusText : response . statusText ,
2268 headers : {
23- 'Content-Type' : 'text/plain' ,
69+ ... response . headers ,
2470 'Access-Control-Allow-Origin' : '*' ,
2571 'Access-Control-Allow-Methods' : 'GET, HEAD, POST, OPTIONS' ,
2672 'Access-Control-Allow-Headers' : 'Content-Type, X-Requested-With' ,
2773 'Access-Control-Max-Age' : '86400' ,
2874 } ,
2975 } ) ;
76+
77+ return newResponse ;
3078 } ,
3179} ;
0 commit comments