Repository navigation
feat: respond with 405 for routes matched only by other methods - #4811
Conversation
When no route matches the request method but the path has method-specific routes, respond with `405 Method Not Allowed` and an `Allow` header instead of `404`.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing: 2
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
commit: |
Probe the existing route matcher with each method of the method-specific routes instead of compiling a second matcher, so bundle size no longer grows with the number of routes. Return the 405 error instead of throwing it, and name the allowed methods in its message.
closes #1727
When a request path has method-specific routes (e.g.
hello.post.ts) but none for the request method, Nitro now responds with405 Method Not Allowedand anAllowheader instead of404:{ "error": true, "status": 405, "statusText": "Method Not Allowed", "message": "Method GET is not allowed for /api/hello (allowed: POST)" }~findRoutelooks the path up again with the existingfindRoute, once for each method used by method-specific routes (a list emitted at build time). A method-agnostic route matching the path would have matched the original request, so any hit is a route of that method.405handler, so CORS preflight keeps working.[...].ts, renderer, server entry) still take precedence. Making method routes win over them would break setups like a/contactpage withserver/routes/contact.post.ts. This means Nuxt apps (which always have the renderer catch-all) are not affected.The shared fixture has a server entry (
/**), so the405can't be observed there; the existingit.todostays and coverage is in unit tests. Verified manually in dev and prod builds, includingbaseURLand a CORS preflight.Note:
HEADon aGET-only route now returns405(was404). Nitro's compiled router has noHEAD->GETfallback; that's a separate gap.🤖 Generated with AI assistant