Capítulo 413 de 456

experimental.proxyClientMaxBodySize

Core Idea

Limits how much of a request body Next.js buffers in memory when proxy clones the body for multiple reads (both in the proxy and the underlying route handler), preventing excessive memory usage.

Key Concepts

  • proxyClientMaxBodySize: experimental config under experimental, default 10MB. Accepts a human-readable string ('1mb') or a number of bytes.
  • String format units: b, kb, mb, gb.
  • Overflow behavior: exceeding the limit does NOT fail the request, only the first N bytes are buffered and a warning is logged; the request continues with the partial body.

Code Examples

import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  experimental: {
    proxyClientMaxBodySize: '1mb',
  },
}

export default nextConfig
export async function proxy(request: NextRequest) {
  const body = await request.text()
  console.log('Body size:', body.length)
  return NextResponse.next()
}
  • O que demonstra: define limite de 1MB e lê o body já bufferado tanto no proxy quanto depois no route handler (app/api/upload/route.ts), reaproveitando o mesmo buffer.

Anti-patterns

  • Assumir que exceder o limite falha a request: não falha, apenas trunca silenciosamente (exceto pelo warning no log), o que pode causar bugs sutis de dados incompletos.

Key Takeaways

  1. Só se aplica quando proxy é usado no app.
  2. Limite é por-request, não agregado entre requests concorrentes.
  3. Para uploads grandes, aumente o limite explicitamente ou trate o body parcial na lógica da aplicação.

Connects To

  • proxy.js file convention: onde o body clonado é efetivamente lido.
  • pageExtensions: proxy.js também é afetado por essa config caso a extensão de página seja customizada.