Skip to main content
The cors() middleware handles everything your browser needs to make cross-origin requests work: it sets Access-Control-* response headers on every matching request and responds to OPTIONS preflight requests with a 204 No Content before your route handlers ever run. Because it has access to req.env, you can resolve the list of allowed origins from a Cloudflare environment binding at request time — no code changes required when you rotate or expand your origin list.

Basic usage

Allow all origins with zero configuration:
This sets Access-Control-Allow-Origin: * on every response and handles OPTIONS preflight automatically.

CorsOptions reference

string | string[] | (req) => string | string[] | Promise<string | string[]>
Allowed origin(s). Pass '*' (or omit entirely) to allow all origins. Pass an array of origin strings for an allowlist. Pass a function to resolve origins dynamically at request time — the function receives the full BlazeRequest so you can read from req.env. If the incoming Origin header is not in the allowlist, Blaze skips CORS headers entirely and calls next().
string[]
default:"['GET','HEAD','PUT','PATCH','POST','DELETE']"
HTTP methods to include in the Access-Control-Allow-Methods preflight header.
string[]
default:"[]"
Request headers to include in Access-Control-Allow-Headers. When this is empty and the client sends an Access-Control-Request-Headers preflight header, Blaze reflects the requested headers back automatically.
string[]
default:"[]"
Response headers to expose to the browser via Access-Control-Expose-Headers.
boolean
default:"false"
Set Access-Control-Allow-Credentials: true. When true, the wildcard '*' is automatically replaced with the actual request Origin — see the note below.
number
Access-Control-Max-Age in seconds. Controls how long browsers cache the preflight response. When omitted, the header is not set.

Dynamic origins from env

Store your allowed origins as a comma-separated Cloudflare variable and resolve them at request time:
The resolver is async-compatible, so you can also fetch the origin list from KV if you need dynamic updates without redeployment:

Credentials and cookies

When your frontend sends requests with cookies or an Authorization header, set credentials: true and specify an explicit origin (not '*'). Browsers reject credentialed responses that use the wildcard origin.
When credentials: true, Blaze automatically replaces the '*' wildcard with the actual request Origin header value. It also adds a Vary: Origin response header so caches don’t serve one client’s credentialed response to another.
With this configuration, Blaze produces these headers on a credentialed response:
And on a preflight OPTIONS response:

Scoped CORS

Apply cors() only to specific path prefixes rather than globally. This is useful when your Worker serves both an API (which needs CORS) and server-rendered HTML pages (which don’t).
You can also stack cors() with different configurations on different sub-routers: