Skip to content

Commit

Permalink
fix: do not add mode: cors when cloudflare workers runtime (#1605)
Browse files Browse the repository at this point in the history
In cloudflare workers the request fails with: `The 'mode' field on
'RequestInitializerDict' is not implemented.`.

Detecting cloudflare environment as advised by cloudflare:
https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
  • Loading branch information
alanshaw authored Dec 18, 2024
1 parent 12e2e69 commit 7902527
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/upload-client/src/blob/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import retry, { AbortError } from 'p-retry'
import { servicePrincipal, connection } from '../service.js'
import { REQUEST_RETRIES } from '../constants.js'
import { poll } from '../receipts.js'
import { isCloudflareWorkers } from '../runtime.js'

/**
* @param {string} url
Expand Down Expand Up @@ -228,7 +229,7 @@ export async function add(
try {
const res = await fetchWithUploadProgress(address.url, {
method: 'PUT',
mode: 'cors',
...(!isCloudflareWorkers && { mode: 'cors' }),
body: bytes,
headers: address.headers,
signal: options.signal,
Expand Down
4 changes: 4 additions & 0 deletions packages/upload-client/src/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* c8 ignore next 3 */
export const isCloudflareWorkers =
typeof navigator !== 'undefined' &&
navigator?.userAgent === 'Cloudflare-Workers'

0 comments on commit 7902527

Please sign in to comment.