diff --git a/packages/next/server/api-utils.ts b/packages/next/server/api-utils.ts index 510d3019662c..42b37536fc38 100644 --- a/packages/next/server/api-utils.ts +++ b/packages/next/server/api-utils.ts @@ -69,12 +69,12 @@ export async function apiResolver( const writeData = apiRes.write const endResponse = apiRes.end apiRes.write = (...args: any[2]) => { - contentLength += Buffer.byteLength(args[0]) + contentLength += Buffer.byteLength(args[0] || '') return writeData.apply(apiRes, args) } apiRes.end = (...args: any[2]) => { if (args.length && typeof args[0] !== 'function') { - contentLength += Buffer.byteLength(args[0]) + contentLength += Buffer.byteLength(args[0] || '') } if (contentLength >= 4 * 1024 * 1024) { diff --git a/test/integration/api-support/pages/api/nullable-payload.js b/test/integration/api-support/pages/api/nullable-payload.js new file mode 100644 index 000000000000..7e9dd82f2186 --- /dev/null +++ b/test/integration/api-support/pages/api/nullable-payload.js @@ -0,0 +1,4 @@ +export default (req, res) => { + res.write('') + res.end() +} diff --git a/test/integration/api-support/test/index.test.js b/test/integration/api-support/test/index.test.js index 266c6eb46065..21a5874de3ad 100644 --- a/test/integration/api-support/test/index.test.js +++ b/test/integration/api-support/test/index.test.js @@ -442,6 +442,11 @@ function runTests(dev = false) { expect(data).toBe('hi') }) + it('should work with nullable payload', async () => { + const data = await renderViaHTTP(appPort, '/api/nullable-payload') + expect(data).toBe('') + }) + it('should warn if response body is larger than 4MB', async () => { let res = await fetchViaHTTP(appPort, '/api/large-response') expect(res.ok).toBeTruthy()