Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions test/production/options-request/app/app-page/dynamic/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function Page() {
return <div>foo page</div>
}

export const dynamic = 'force-dynamic'
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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' })
Expand Down
11 changes: 11 additions & 0 deletions test/production/options-request/pages/pages-page/dynamic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function Page() {
return <div>bar</div>
}

export async function getServerSideProps() {
return {
props: {
data: {},
},
}
}
Loading