Skip to content

Commit bc84a7d

Browse files
committed
fix: Update cors issue by adding worker.js
1 parent ac74b6c commit bc84a7d

File tree

12 files changed

+106
-199
lines changed

12 files changed

+106
-199
lines changed

.github/workflows/deploy.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- master
77
- dev
8+
- fix-cloudflare-deployment-scripts-update
89

910
concurrency:
1011
group: ${{ github.workflow }}-${{ github.ref }}
@@ -15,18 +16,23 @@ jobs:
1516
runs-on: ubuntu-latest
1617
outputs:
1718
env: ${{ steps.set-env.outputs.env }}
19+
project-name: ${{ steps.set-env.outputs.project-name }}
1820
steps:
1921
- id: set-env
20-
run: |
22+
run: |
2123
if [ "${{ github.ref_name }}" == "master" ]; then
2224
export ENV=production
23-
fi
24-
25-
if [ "${{ github.ref_name }}" == "dev" ]; then
25+
export PROJECT_NAME="polygon-token-list"
26+
elif [ "${{ github.ref_name }}" == "dev" ]; then
27+
export ENV=staging
28+
export PROJECT_NAME="polygon-token-list-staging"
29+
else
2630
export ENV=staging
31+
export PROJECT_NAME="polygon-token-list-staging"
2732
fi
2833
2934
echo "env=$ENV" >> "$GITHUB_OUTPUT"
35+
echo "project-name=$PROJECT_NAME" >> "$GITHUB_OUTPUT"
3036
3137
build-and-deploy:
3238
needs: [get-env]
@@ -36,7 +42,7 @@ jobs:
3642
- uses: actions/setup-node@v5
3743
- run: npm ci
3844
- run: npm run build
39-
- name: Deploy bundle
45+
- name: Deploy to Cloudflare Workers
4046
uses: cloudflare/wrangler-action@v3
4147
with:
4248
environment: ${{ needs.get-env.outputs.env }}

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ node_modules/
22
build/
33
.DS_STORE
44
.wrangler/
5-
src/wrangler_main.js

functions/_middleware.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export async function onRequest(context) {
2+
if (context.request.method === 'OPTIONS') {
3+
return new Response(null, {
4+
status: 204,
5+
headers: {
6+
'Access-Control-Allow-Origin': '*',
7+
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
8+
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
9+
'Access-Control-Max-Age': '86400',
10+
},
11+
});
12+
}
13+
14+
// Let static assets handle all other requests
15+
return context.next();
16+
}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"scripts": {
77
"test": "mocha 'tests/tokenAddition'",
88
"test-all": "mocha 'tests'",
9-
"build": "rimraf build && mkdir -p build/tokenlists && node src/write.js && node src/generateWorker.js",
9+
"build": "rimraf build && mkdir -p build/tokenlists build/_functions && node src/write.js && cp src/_headers build/_headers && cp src/_redirects build/_redirects && cp functions/_middleware.js build/_functions/_middleware.js",
10+
"deploy": "npm run build && npx wrangler pages deploy build",
11+
"deploy:staging": "npm run build && npx wrangler pages deploy build --env staging",
12+
"deploy:production": "npm run build && npx wrangler pages deploy build --env production",
1013
"lint": "eslint \"./src/*\"",
1114
"lint:fix": "eslint --fix \"./src/*\"",
1215
"lint-tests:fix": "eslint --fix \"./tests/*\""
@@ -33,6 +36,7 @@
3336
"rimraf": "^3.0.2"
3437
},
3538
"dependencies": {
39+
"@cloudflare/kv-asset-handler": "^0.4.0",
3640
"wrangler": "^4.34.0"
3741
}
3842
}

src/_headers

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
Access-Control-Allow-Origin: *
3+
Access-Control-Allow-Methods: GET, HEAD, POST, OPTIONS
4+
Access-Control-Allow-Headers: Content-Type, X-Requested-With
5+
Access-Control-Max-Age: 86400

src/_redirects

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Serve listRegistry.json for explicit requests
2+
/listRegistry.json /listRegistry.json 200
3+
4+
# Serve token lists from tokenlists directory
5+
/tokenlists/* /tokenlists/:splat 200

src/generateWorker.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/worker.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export default {
2+
async fetch(request, env, ctx) {
3+
const url = new URL(request.url);
4+
5+
// Handle OPTIONS requests first
6+
if (request.method === 'OPTIONS') {
7+
return new Response(null, {
8+
status: 204,
9+
headers: {
10+
'Access-Control-Allow-Origin': '*',
11+
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
12+
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
13+
'Access-Control-Max-Age': '86400',
14+
},
15+
});
16+
}
17+
18+
// Handle root redirect to listRegistry.json
19+
if (url.pathname === '/') {
20+
// Redirect to listRegistry.json
21+
return Response.redirect(new URL('/listRegistry.json', url.origin), 302);
22+
}
23+
24+
// For all other requests, let Cloudflare serve static assets
25+
// and add CORS headers to the response
26+
let response;
27+
28+
try {
29+
if (env.ASSETS) {
30+
response = await env.ASSETS.fetch(request);
31+
} else {
32+
// Fallback for local development
33+
response = await fetch(request);
34+
}
35+
} catch (error) {
36+
console.error('Error fetching asset:', error);
37+
response = await fetch(request);
38+
}
39+
40+
// Clone the response to add CORS headers
41+
const newResponse = new Response(response.body, {
42+
status: response.status,
43+
statusText: response.statusText,
44+
headers: {
45+
...response.headers,
46+
'Access-Control-Allow-Origin': '*',
47+
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
48+
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
49+
'Access-Control-Max-Age': '86400',
50+
},
51+
});
52+
53+
return newResponse;
54+
},
55+
};

tests/defaultTest.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)