max
max
CDCloudflare Developers
Created by max on 4/17/2025 in #workers-help
R2 seems to be ignoring the CORS policy
I have a bucket that i'm trying to upload to via signed URLs generated using aws-sdk Node package The signed URL always fails PUT request with Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Here's my CORS policy, which should work? What am I missing?
[
{
"AllowedOrigins": [
"*"
],
"AllowedMethods": [
"GET",
"PUT",
"POST",
"HEAD"
],
"AllowedHeaders": [
"range"
],
"ExposeHeaders": [
"Content-Type",
"Access-Control-Allow-Origin",
"ETag"
]
}
]
[
{
"AllowedOrigins": [
"*"
],
"AllowedMethods": [
"GET",
"PUT",
"POST",
"HEAD"
],
"AllowedHeaders": [
"range"
],
"ExposeHeaders": [
"Content-Type",
"Access-Control-Allow-Origin",
"ETag"
]
}
]
12 replies
CDCloudflare Developers
Created by max on 4/7/2025 in #workers-help
Worker deploys fine via Wrangler and Web UI but fails via API with SyntaxError
Hi everyone. I'm having a rather odd issue. I'm trying to deploy a worker (I'm using Workers for Platforms but it shouldn't matter) and while it's able to deploy perfectly fine using Wrangler and Web UI, when I deploy via the API, I'm getting a syntax error. Uncaught SyntaxError: Unexpected token 'export' It seems the API will not accept ES module syntax for some reason? Reproduces even with Cloudflare's Hello World example:
export default {
async fetch(request, env, ctx) {
return new Response("Hello World!");
},
};
export default {
async fetch(request, env, ctx) {
return new Response("Hello World!");
},
};
My API code:
// Create form data for Cloudflare API
const formData = new FormData()

// Add metadata
const metadata = {
compatibility_date: "2023-08-23",
main_module: "worker.js",
}
formData.append("metadata", JSON.stringify(metadata))

// Add worker code
formData.append("worker.js", new Blob([workerCode], { type: "application/javascript" }))

// Deploy to Cloudflare Workers
const scriptName = `${userId}-${userWorkerId}`
const cfApiUrl = `https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/workers/dispatch/namespaces/${CLOUDFLARE_DISPATCH_NAMESPACE}/scripts/${scriptName}`

console.log(`Deploying worker to Cloudflare API: PUT ${cfApiUrl}`)

const response = await fetch(cfApiUrl, {
method: "PUT",
headers: {
Authorization: `Bearer ${CLOUDFLARE_API_TOKEN}`,
// FormData sets Content-Type automatically
},
body: formData,
})
// Create form data for Cloudflare API
const formData = new FormData()

// Add metadata
const metadata = {
compatibility_date: "2023-08-23",
main_module: "worker.js",
}
formData.append("metadata", JSON.stringify(metadata))

// Add worker code
formData.append("worker.js", new Blob([workerCode], { type: "application/javascript" }))

// Deploy to Cloudflare Workers
const scriptName = `${userId}-${userWorkerId}`
const cfApiUrl = `https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/workers/dispatch/namespaces/${CLOUDFLARE_DISPATCH_NAMESPACE}/scripts/${scriptName}`

console.log(`Deploying worker to Cloudflare API: PUT ${cfApiUrl}`)

const response = await fetch(cfApiUrl, {
method: "PUT",
headers: {
Authorization: `Bearer ${CLOUDFLARE_API_TOKEN}`,
// FormData sets Content-Type automatically
},
body: formData,
})
The multipart stuff is 100% correct and the worker uploads since the error i'm getting shows the exact location of the word export in the code, so the issue is at the deployment stage inside Cloudflare's API Any tips to fix it would be much appreciated! Thanks!
8 replies