当使用代理时,Next.js 会自动克隆请求体并将其缓冲在内存中,以便代理和底层路由处理程序都能进行多次读取。为了防止内存使用量过大,此配置选项设置了缓冲体的大小限制。
默认情况下,最大请求体大小为 10MB。如果请求体超过此限制,则只会缓冲达到该限制的部分,并且会记录一条警告,指示哪个路由超出了限制。
使用人类可读的字符串格式指定大小:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
proxyClientMaxBodySize: '1mb',
},
}
export default nextConfig/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
proxyClientMaxBodySize: '1mb',
},
}
module.exports = nextConfig支持的单位:b、kb、mb、gb
或者,以字节数的形式指定大小:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
proxyClientMaxBodySize: 1048576, // 1MB in bytes
},
}
export default nextConfig/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
proxyClientMaxBodySize: 1048576, // 1MB in bytes
},
}
module.exports = nextConfig当请求体超出配置的限制时:
如果您的应用程序需要处理完整的请求体,您应该:
proxyClientMaxBodySize 限制import { NextRequest, NextResponse } from 'next/server'
export async function proxy(request: NextRequest) {
// Next.js automatically buffers the body with the configured size limit
// You can read the body in proxy...
const body = await request.text()
// If the body exceeded the limit, only partial data will be available
console.log('Body size:', body.length)
return NextResponse.next()
}import { NextRequest, NextResponse } from 'next/server'
export async function POST(request: NextRequest) {
// ...and the body is still available in your route handler
const body = await request.text()
console.log('Body in route handler:', body.length)
return NextResponse.json({ received: body.length })
}