Writing
Fullstack English

CORS Errors, Debugged Properly for Once

The browser is not blocking your request - it is blocking your response. Preflights, credentials, and the three headers that actually matter.

  • Published
  • 1 min read
On this page3 sections
  1. The mental model
  2. The three failure modes
  3. Debug ritual

Every fullstack developer has pasted a CORS error into a search bar at 11 PM. The fix is usually two headers - the pain is not knowing which two.

The mental model

CORS is the server giving the browser permission to show a cross-origin response to JavaScript. The request usually reaches your server fine. The browser then checks the response headers and, absent permission, hides it from your code.

The three failure modes

  1. Simple request, missing header. Response needs Access-Control-Allow-Origin: https://app.example.com (or * if no credentials).
  2. Preflight. Any request with Authorization, Content-Type: application/json, or non-GET/POST triggers an OPTIONS first. Your route/middleware must answer it with Allow-Origin, Allow-Methods, Allow-Headers - and answer fast, without auth.
  3. Credentials. fetch(..., { credentials: 'include' }) requires Access-Control-Allow-Credentials: true and a specific origin. * is forbidden by spec here - this combination is 80% of the mysterious cases.
typescript
// Hono
app.use('*', cors({
  origin: ['https://app.example.com', 'http://localhost:5173'],
  credentials: true,
}))

Debug ritual

DevTools > Network > the failing request. Is there an OPTIONS above it? What status? Which Access-Control-* headers came back? Answer those three questions and you will never paste a CORS error into a search bar again. At 11 PM, anyway.