Files
Jude GaoandGitHub 4bb686dee9 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).
2025-03-03 17:59:22 -05:00

50 lines
1.8 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
describe('options-request', () => {
const { next } = nextTestSetup({
files: __dirname,
})
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)
// There should be no response body
expect(await res.text()).toBe('')
}
)
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' })
expect(res.status).toBe(204)
// There should be no response body
expect(await res.text()).toBe('')
})
// In pages router, unless the handler explicitly handles OPTIONS, it will handle the request normally
it('should respond with a 200 + response body when invoking a pages API route with an OPTIONS request', async () => {
const res = await next.fetch('/api/pages-api-handler', {
method: 'OPTIONS',
})
expect(res.status).toBe(200)
// There should be no response body
expect((await res.json()).message).toBe('Hello from Next.js!')
})
it('should 404 for an OPTIONS request to a non-existent route', async () => {
const res = await next.fetch('/non-existent-route', { method: 'OPTIONS' })
expect(res.status).toBe(404)
})
})