proxyClientMaxBodySize
This feature is currently experimental and subject to change, it's not recommended for production. Try it out and share your feedback onGitHub.
When proxy is used, Next.js automatically clones the request body and buffers it in memory to enable multiple reads - both in proxy and the underlying route handler. To prevent excessive memory usage, this configuration option sets a size limit on the buffered body.
By default, the maximum body size is10MB. If a request body exceeds this limit, the body will only be buffered up to the limit, and a warning will be logged indicating which route exceeded the limit.
Options
String format (recommended)
Specify the size using a human-readable string format:
next.config.ts
importtype { NextConfig }from'next'constnextConfig:NextConfig= { experimental: { proxyClientMaxBodySize:'1mb', },}exportdefault nextConfigSupported units:b,kb,mb,gb
Number format
Alternatively, specify the size in bytes as a number:
next.config.ts
importtype { NextConfig }from'next'constnextConfig:NextConfig= { experimental: { proxyClientMaxBodySize:1048576,// 1MB in bytes },}exportdefault nextConfigBehavior
When a request body exceeds the configured limit:
- Next.js will buffer only the first N bytes (up to the limit)
- A warning will be logged to the console indicating the route that exceeded the limit
- The request will continue processing normally, but only the partial body will be available
- The request willnot fail or return an error to the client
If your application needs to process the full request body, you should either:
- Increase the
proxyClientMaxBodySizelimit - Handle the partial body gracefully in your application logic
Example
proxy.ts
import { NextRequest, NextResponse }from'next/server'exportasyncfunctionproxy(request:NextRequest) {// Next.js automatically buffers the body with the configured size limit// You can read the body in proxy...constbody=awaitrequest.text()// If the body exceeded the limit, only partial data will be availableconsole.log('Body size:',body.length)returnNextResponse.next()}app/api/upload/route.ts
import { NextRequest, NextResponse }from'next/server'exportasyncfunctionPOST(request:NextRequest) {// ...and the body is still available in your route handlerconstbody=awaitrequest.text()console.log('Body in route handler:',body.length)returnNextResponse.json({ received:body.length })}Good to know
- This setting only applies when proxy is used in your application
- The default limit of 10MB is designed to balance memory usage and typical use cases
- The limit applies per-request, not globally across all concurrent requests
- For applications handling large file uploads, consider increasing the limit accordingly
Was this helpful?