Skip to content

Commit 83c4084

Browse files
committed
fix: add CORS headers and root redirect to worker
1 parent 233dc21 commit 83c4084

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

.github/workflows/deploy.yaml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jobs:
2424
export ENV=production
2525
export PROJECT_NAME="polygon-token-list"
2626
elif [ "${{ github.ref_name }}" == "dev" ]; then
27-
export ENV=preview
27+
export ENV=staging
2828
export PROJECT_NAME="polygon-token-list-staging"
2929
else
30-
export ENV=preview
30+
export ENV=staging
3131
export PROJECT_NAME="polygon-token-list-staging"
3232
fi
3333
@@ -42,17 +42,9 @@ jobs:
4242
- uses: actions/setup-node@v5
4343
- run: npm ci
4444
- run: npm run build
45-
- name: Create Pages Project (if not exists)
45+
- name: Deploy to Cloudflare Workers
4646
uses: cloudflare/wrangler-action@v3
4747
with:
48-
command: pages project create ${{ needs.get-env.outputs.project-name }} --production-branch master
49-
apiToken: ${{ secrets.CF_WORKER_API_TOKEN }}
50-
accountId: ${{ secrets.CF_WORKER_ACCOUNT_ID }}
51-
continue-on-error: true
52-
53-
- name: Deploy to Cloudflare Pages
54-
uses: cloudflare/wrangler-action@v3
55-
with:
56-
command: pages deploy build --project-name ${{ needs.get-env.outputs.project-name }}
48+
environment: ${{ needs.get-env.outputs.env }}
5749
apiToken: ${{ secrets.CF_WORKER_API_TOKEN }}
5850
accountId: ${{ secrets.CF_WORKER_ACCOUNT_ID }}

src/worker.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

wrangler.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
name = "polygon-token-list"
2+
main = "src/worker.js"
23
compatibility_date = "2024-12-30"
3-
pages_build_output_dir = "build"
4+
placement = { mode = "smart" }
5+
preview_urls = false
6+
send_metrics = false
7+
workers_dev = false
48

5-
[env.preview]
9+
# Static assets with worker for OPTIONS handling
10+
[assets]
11+
directory = "build"
12+
13+
[env.staging]
614
routes = [
715
{ pattern = "api-polygon-tokens.staging.polygon.technology", custom_domain = true },
816
]

0 commit comments

Comments
 (0)