Full answer
A cross-origin request is "simple" when it uses GET, HEAD or POST, has only a few safe headers, and for POST uses a content type of application/x-www-form-urlencoded, multipart/form-data or text/plain. These are the kinds of requests that an HTML form could already send before CORS existed, so the browser sends them straight away and then checks the response headers.
Anything else needs a preflight. Common triggers:
- A method other than GET, HEAD or POST, such as PUT, PATCH or DELETE.
Content-Type: application/json.- A custom header such as
AuthorizationorX-Request-Id.
The browser first sends OPTIONS with Origin, Access-Control-Request-Method and Access-Control-Request-Headers. The server answers, usually with status 204 and these headers:
Access-Control-Allow-Origin: the allowed origin.Access-Control-Allow-Methods: the methods the real request may use.Access-Control-Allow-Headers: the headers the real request may send.Access-Control-Max-Age: how many seconds the browser may remember the answer, which saves a round trip on later calls. Browsers cap this value.
If the preflight fails or does not list a method or header, the browser never sends the real request, and the page sees a network error. The server log then shows only OPTIONS.
The preflight request carries no cookies, so protect the OPTIONS route accordingly: it must not require authentication, or it will always fail. When credentials are used on the real request, the answer must also include Access-Control-Allow-Credentials: true and a specific origin.
In the test, the page sends a PUT with a JSON body and an Authorization header. The server log shows OPTIONS then PUT. When the server forgets to allow Authorization, the log shows OPTIONS only.
Preflights cost latency. Reduce them with Access-Control-Max-Age, by keeping the API on the same origin behind a proxy, or by avoiding custom headers where a simple request is enough.
import http from 'node:http';
const allowedOrigin = process.env.ALLOWED ?? 'https://app.example.com';
const seen = [];
http.createServer((req, res) => {
if (req.url === '/seen') return res.end(JSON.stringify(seen));
seen.push(req.method);
if (req.headers.origin === allowedOrigin) {
res.setHeader('Access-Control-Allow-Origin', allowedOrigin);
res.setHeader('Vary', 'Origin');
}
if (req.method === 'OPTIONS') {
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Max-Age', '600');
res.statusCode = 204;
return res.end();
}
res.end('saved');
}).listen(process.env.PORT, () => console.log('listening'));Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The page and the API ran on different localhost ports. The PUT with a JSON body and Authorization header gave OPTIONS then PUT. When Authorization was left out of Allow-Headers, only OPTIONS reached the server. A simple GET sent no preflight.
Key points
- Non-simple requests trigger an automatic OPTIONS preflight.
- Triggers: methods like PUT or DELETE, JSON content type, custom headers.
- The server must list the method and headers the real request will use.
- If the preflight fails, the real request is never sent.
- Access-Control-Max-Age caches the answer for a limited time.
Common mistakes
- Requiring authentication on the OPTIONS route, so the preflight can never pass.
- Allowing the method but forgetting a header such as Authorization.
- Returning the CORS headers only on the real response and not on the preflight.
- Thinking the preflight is sent by the developer's code.
Follow-ups
- How do you avoid a preflight for a simple search request?
- Why does a preflight not carry cookies?
- What changes when the real request uses credentials?
Tips
- List the three triggers and the four answer headers.
- Mention how to see the OPTIONS request in the Network tab.