The Symptom
Symptom
Root Cause Analysis
Root Cause
Because the frontend and backend were now on different domains, the browser engaged Cross-Origin Resource Sharing (CORS) protections. Before sending a `POST` or `PUT` request with custom headers (like `Authorization`), the browser sends a preflight `OPTIONS` request to ask the server for permission. Our backend responded with `Access-Control-Allow-Origin: *`, but we failed to include an `Access-Control-Max-Age` header. By default, Chromium caches preflight responses for only 5 seconds. This meant that every time a user navigated the app, the browser was firing duplicate OPTIONS requests, doubling network latency (due to round trips) and artificially inflating our server load.
// The fatal flaw: Express CORS middleware defaults
app.use(cors({
origin: 'https://app.devsolved.com',
methods: ['GET', 'POST', 'PUT']
// Missing maxAge!
}));
Discussions 0