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

Add API config to allow disabling response size warning #34700

Merged
merged 15 commits into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/next/server/api-utils/node.ts
Expand Up @@ -172,6 +172,7 @@ export async function apiResolver(
}
const config: PageConfig = resolverModule.config || {}
const bodyParser = config.api?.bodyParser !== false
const bodyLimit = config.api?.bodyLimit ?? true
const externalResolver = config.api?.externalResolver || false

// Parsing of cookies
Expand Down Expand Up @@ -209,9 +210,9 @@ export async function apiResolver(
contentLength += Buffer.byteLength(args[0] || '')
}

if (contentLength >= 4 * 1024 * 1024) {
if (bodyLimit && contentLength >= 4 * 1024 * 1024) {
console.warn(
`API response for ${req.url} exceeds 4MB. This will cause the request to fail in a future version. https://nextjs.org/docs/messages/api-routes-body-size-limit`
`API response for ${req.url} exceeds 4MB. API Routes are meant to respond quickly. https://nextjs.org/docs/messages/api-routes-body-size-limit`
)
}

Expand Down
5 changes: 5 additions & 0 deletions packages/next/types/index.d.ts
Expand Up @@ -63,6 +63,11 @@ export type NextPage<P = {}, IP = P> = NextComponentType<NextPageContext, IP, P>
export type PageConfig = {
amp?: boolean | 'hybrid'
api?: {
/**
* Flag to disable warning "API response for ${req.url} exceeds 4MB.",
* due to deploying app outside Vercel, like AWS.
*/
bodyLimit?: boolean
crice88 marked this conversation as resolved.
Show resolved Hide resolved
/**
* The byte limit of the body. This is the number of bytes or any string
* format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`.
Expand Down
@@ -0,0 +1,10 @@
export const config = {
api: {
bodyLimit: false,
},
}

export default (req, res) => {
let body = '.'.repeat(5 * 1024 * 1024)
res.send(body)
}
12 changes: 10 additions & 2 deletions test/integration/api-support/test/index.test.js
Expand Up @@ -451,13 +451,21 @@ function runTests(dev = false) {
let res = await fetchViaHTTP(appPort, '/api/large-response')
expect(res.ok).toBeTruthy()
expect(stderr).toContain(
'API response for /api/large-response exceeds 4MB. This will cause the request to fail in a future version.'
'API response for /api/large-response exceeds 4MB. API Routes are meant to respond quickly.'
)

res = await fetchViaHTTP(appPort, '/api/large-chunked-response')
expect(res.ok).toBeTruthy()
expect(stderr).toContain(
'API response for /api/large-chunked-response exceeds 4MB. This will cause the request to fail in a future version.'
'API response for /api/large-chunked-response exceeds 4MB. API Routes are meant to respond quickly.'
)
})

it('should not warn if response body is larger than 4MB with config', async () => {
let res = await fetchViaHTTP(appPort, '/api/large-response-with-config')
expect(res.ok).toBeTruthy()
expect(stderr).not.toContain(
'API response for /api/large-response-with-config exceeds 4MB. API Routes are meant to respond quickly.'
)
})

Expand Down