Unverified Commit 4bb686de authored by Jude Gao's avatar Jude Gao Committed by GitHub

Fix: respond 405 status code on OPTIONS request to SSG page (#76767)

### What?

The behavior of `OPTIONS` requests differs depending on whether the page
is an SSG page or not:

- For **SSG pages**, we return a `405 Method Not Allowed` status code
([#34346](https://github.com/vercel/next.js/pull/34346)).
- Otherwise, when an `OPTIONS` request reaches `doRender`, it returns a
`400 Bad Request`
([#65295](https://github.com/vercel/next.js/pull/65295)).

However, in the first case, handling the error page triggers a
`doRender` call, which inadvertently resets the status code to `400`. We
want to ensure it remains `405`.

To fix this, I propose **skipping the error page rendering for `OPTIONS`
requests on SSG pages** and directly returning `"Method Not Allowed"`.
This also addresses several related issues (see "Why").

### Why?

When an `OPTIONS` request is made to an SSG page:

1. **Rendering an error page is wasteful.** A `405` response doesn’t
need a UI, so skipping the rendering improves efficiency.
2. **The status code is reset incorrectly.** As described in ["What"],
the logic in
[`base-server.ts#L2682`](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/base-server.ts#L2682)
changes the status code to `400` when rendering an error page.
3. **It triggers a fatal error** (`Error: invariant: cache entry
required but not generated`) for projects deployed on Vercel (see:
[NEXT-4000](https://github.com/vercel/next.js/issues/4000)).

### How?

Instead of rendering an error page, we return a **hardcoded HTML
response** with a `"Method Not Allowed"` body.

Closes [NEXT-4000](https://github.com/vercel/next.js/issues/4000).
parent 15a0dee8
......@@ -2247,7 +2247,7 @@ export default abstract class Server<
) {
res.statusCode = 405
res.setHeader('Allow', ['GET', 'HEAD'])
await this.renderError(null, req, res, pathname)
res.body('Method Not Allowed').send()
return null
}
......
export default function Page() {
return <div>foo page</div>
}
export const dynamic = 'force-dynamic'
......@@ -5,8 +5,8 @@ describe('options-request', () => {
files: __dirname,
})
it.each(['/app-page', '/pages-page'])(
'should return a 400 status code when invoking %s with an OPTIONS request',
it.each(['/app-page/dynamic', '/pages-page/dynamic'])(
'should return a 400 status code when invoking %s with an OPTIONS request (dynamic rendering)',
async (path) => {
const res = await next.fetch(path, { method: 'OPTIONS' })
expect(res.status).toBe(400)
......@@ -15,6 +15,15 @@ describe('options-request', () => {
}
)
it.each(['/app-page/static', '/pages-page/static'])(
'should return a 405 status code when invoking %s with an OPTIONS request (static rendering)',
async (path) => {
const res = await next.fetch(path, { method: 'OPTIONS' })
expect(res.status).toBe(405)
expect(await res.text()).toBe('Method Not Allowed')
}
)
// In app router, OPTIONS is auto-implemented if not provided
it('should respond with a 204 No Content when invoking an app route handler with an OPTIONS request', async () => {
const res = await next.fetch('/app-route', { method: 'OPTIONS' })
......
export default function Page() {
return <div>bar</div>
}
export async function getServerSideProps() {
return {
props: {
data: {},
},
}
}
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