Global middleware don't see OPTIONS requests
Environment
Used version [email protected]
Reproduction
- Go to https://stackblitz.com/edit/github-hwbudnxq
- Click on "GET /api/hello" button
- See the logs in the terminal
- Click on "POST /api/hello" button
- See the logs in the terminal
- Click on "OPTIONS /api/hello" button
- No logs in the terminal
Describe the bug
I not able to catch the OPTIONS requests to add headers for cors for pre-flight requests.
May be I'm missing some web standard web but I'm not able to add CORS headers on OPTIONS requests.
Either I use the routeRules config, the server.ts or the global middlewares in /middleware folder.
I see no logs from OPTIONS requests. Like something is catching the request before any middleware.
May be I do it wrong and I should not try to add CORS this way but for me it's seems like a bug to dont be able to use middleware for OPTIONS requests.
If this make sense in a web standard way, please educate me 😅
Additional context
I'm a noob with CORS on server side 😅
Logs
This is because Vite intercepts OPTIONS requests by default during dev (not after build), before Nitro can handle the request.
If you disable Vite's default behavior by setting server.cors: false, you can handle the OPTIONS request in Nitro's handler.
I think something might need to be done in nitro/vite to make the behavior consistent between dev and build. 🤔
@kricsleo thanks for sharing findings. i think nitro should default cors to false to match closer to prod behavior, feel free to drop a PR.