Performance: Preflight requests behavior
MEDIUM IMPACT
Preflight requests add extra network round-trips before the main request, impacting page load speed and interaction responsiveness.
const cors = require('cors'); const corsOptions = { origin: 'https://example.com', methods: ['GET', 'POST'], allowedHeaders: ['Content-Type'], optionsSuccessStatus: 204 }; app.use(cors(corsOptions));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
return res.status(204).json({});
}
next();
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Generic CORS middleware allowing all origins and methods | N/A | N/A | Adds network delay before DOM can render | [X] Bad |
| Specific CORS middleware with limited origins, methods, and headers | N/A | N/A | Minimal network delay, faster content rendering | [OK] Good |