Unverified Commit 0a697cf0 authored by Sam Ko's avatar Sam Ko Committed by GitHub

fix(route-handlers): make sure preflight has CORS headers (#63264)

## Why?

When we just have a POST route handler, it seems CORs is not working
properly. This is because we break out of the loop here
https://github.com/vercel/next.js/blob/768a92b15bd72002df0f9f893d78e094a8e3165f/packages/next/src/server/future/route-modules/app-route/helpers/auto-implement-methods.ts#L38
when a `GET` isn't present before we add the correct headers and status
to the preflight `OPTION`.

- Closes https://github.com/vercel/next.js/issues/57999

Closes NEXT-2813

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 768a92b1
...@@ -33,16 +33,13 @@ export function autoImplementMethods( ...@@ -33,16 +33,13 @@ export function autoImplementMethods(
// we'll automatically implement it by calling the GET method (if it // we'll automatically implement it by calling the GET method (if it
// exists). // exists).
if (method === 'HEAD') { if (method === 'HEAD') {
// If the userland module doesn't implement the GET method, then if (handlers.GET) {
// we're done. // Implement the HEAD method by calling the GET method.
if (!handlers.GET) break methods.HEAD = handlers.GET
// Implement the HEAD method by calling the GET method.
methods.HEAD = handlers.GET
// Mark it as implemented.
implemented.add('HEAD')
// Mark it as implemented.
implemented.add('HEAD')
}
continue continue
} }
......
...@@ -557,9 +557,7 @@ createNextDescribe( ...@@ -557,9 +557,7 @@ createNextDescribe(
expect(res.status).toEqual(204) expect(res.status).toEqual(204)
expect(await res.text()).toBeEmpty() expect(await res.text()).toBeEmpty()
expect(res.headers.get('allow')).toEqual( expect(res.headers.get('allow')).toEqual('OPTIONS, POST')
'DELETE, GET, HEAD, OPTIONS, POST'
)
}) })
}) })
......
// This route exports GET, POST, and DELETE. The test verifies that this route // This route exports POST. The test verifies that this route
// will handle the OPTIONS request. // will handle the OPTIONS request.
export { GET, POST, DELETE } from '../../../handlers/hello' export { POST } from '../../../handlers/hello'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment