Fix CORS Policy Blocks & Failed Preflight (OPTIONS) Errors
Why browsers block cross-origin requests and preflight OPTIONS checks, and how to configure clean CORS headers in one layer.
By the end of this tutorial you will know:
- Why
curlsucceeds while your browser throws a CORS error - Why
Access-Control-Allow-Origin: *breaks when cookies orcredentials: "include"are used - How to handle
OPTIONSpreflight requests without auth middleware blocking them
Prerequisites: Basic familiarity with fetch() and HTTP headers.
1. The big idea
CORS (Cross-Origin Resource Sharing) is a browser security rule, not a server crash. Whenever your frontend on http://localhost:3000 calls an API on https://api.example.com, the browser checks whether the server explicitly allows that origin.
For requests with custom headers (like Authorization) or methods like PUT/DELETE, the browser first sends an automatic preflight OPTIONS request before the real request. If your API requires a login token on OPTIONS or omits the CORS headers, the preflight fails and the real request never fires.
2. The three most common CORS mistakes
- Wildcard
*+ Credentials: ReturningAccess-Control-Allow-Origin: *alongsideAccess-Control-Allow-Credentials: true. Browsers strictly forbid this. - Auth middleware blocking
OPTIONS: Browser preflights never carry yourAuthorizationheader, so returning401 UnauthorizedonOPTIONSkills the request. - Duplicate headers: Setting CORS in both Nginx and your Go/Node app produces
Access-Control-Allow-Origin: http://localhost:3000, http://localhost:3000, which browsers reject.
3. The fix: configure CORS in one layer
Here is a clean, minimal Next.js Route Handler pattern (or generate one for Go, FastAPI, or Nginx with our CORS Helper):
const ALLOWED_ORIGIN = "http://localhost:3000";
const corsHeaders = {
"Access-Control-Allow-Origin": ALLOWED_ORIGIN,
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Credentials": "true",
};
export async function OPTIONS() {
return new Response(null, { status: 204, headers: corsHeaders });
}
export async function GET() {
return Response.json({ ok: true }, { headers: corsHeaders });
}Open your browser DevTools Network tab and check the OPTIONS request. You should see 204 No Content with a single Access-Control-Allow-Origin matching your frontend origin.
4. Common pitfalls
| Error Message | Cause | Fix |
|---|---|---|
wildcard '*' cannot be used when credentials mode is 'include' | Allow-Origin: * + Allow-Credentials: true | Echo or whitelist the exact origin URL |
Response to preflight request doesn't pass access control check | OPTIONS returned 401 or 404 | Return 204 on OPTIONS before auth middleware |
header contains multiple values | Both Nginx and app set CORS | Remove CORS headers from one layer |
5. What you learned
You now know how browser preflight OPTIONS requests work and how to configure explicit, single-layer CORS headers safely.
Video walkthrough
YouTubeClick play to load video (YouTube)