|
| 1 | +--- |
| 2 | +title: experimental.middlewareClientMaxBodySize |
| 3 | +description: Configure the maximum request body size when using middleware. |
| 4 | +version: experimental |
| 5 | +--- |
| 6 | + |
| 7 | +When middleware is used, Next.js automatically clones the request body and buffers it in memory to enable multiple reads - both in middleware and the underlying route handler. To prevent excessive memory usage, this configuration option sets a size limit on the buffered body. |
| 8 | + |
| 9 | +By default, the maximum body size is **10MB**. 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. |
| 10 | + |
| 11 | +## Options |
| 12 | + |
| 13 | +### String format (recommended) |
| 14 | + |
| 15 | +Specify the size using a human-readable string format: |
| 16 | + |
| 17 | +```ts filename="next.config.ts" switcher |
| 18 | +import type { NextConfig } from 'next' |
| 19 | + |
| 20 | +const nextConfig: NextConfig = { |
| 21 | + experimental: { |
| 22 | + middlewareClientMaxBodySize: '1mb', |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +export default nextConfig |
| 27 | +``` |
| 28 | + |
| 29 | +```js filename="next.config.js" switcher |
| 30 | +/** @type {import('next').NextConfig} */ |
| 31 | +const nextConfig = { |
| 32 | + experimental: { |
| 33 | + middlewareClientMaxBodySize: '1mb', |
| 34 | + }, |
| 35 | +} |
| 36 | + |
| 37 | +module.exports = nextConfig |
| 38 | +``` |
| 39 | + |
| 40 | +Supported units: `b`, `kb`, `mb`, `gb` |
| 41 | + |
| 42 | +### Number format |
| 43 | + |
| 44 | +Alternatively, specify the size in bytes as a number: |
| 45 | + |
| 46 | +```ts filename="next.config.ts" switcher |
| 47 | +import type { NextConfig } from 'next' |
| 48 | + |
| 49 | +const nextConfig: NextConfig = { |
| 50 | + experimental: { |
| 51 | + middlewareClientMaxBodySize: 1048576, // 1MB in bytes |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +export default nextConfig |
| 56 | +``` |
| 57 | + |
| 58 | +```js filename="next.config.js" switcher |
| 59 | +/** @type {import('next').NextConfig} */ |
| 60 | +const nextConfig = { |
| 61 | + experimental: { |
| 62 | + middlewareClientMaxBodySize: 1048576, // 1MB in bytes |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +module.exports = nextConfig |
| 67 | +``` |
| 68 | + |
| 69 | +## Behavior |
| 70 | + |
| 71 | +When a request body exceeds the configured limit: |
| 72 | + |
| 73 | +1. Next.js will buffer only the first N bytes (up to the limit) |
| 74 | +2. A warning will be logged to the console indicating the route that exceeded the limit |
| 75 | +3. The request will continue processing normally, but only the partial body will be available |
| 76 | +4. The request will **not** fail or return an error to the client |
| 77 | + |
| 78 | +If your application needs to process the full request body, you should either: |
| 79 | + |
| 80 | +- Increase the `middlewareClientMaxBodySize` limit |
| 81 | +- Handle the partial body gracefully in your application logic |
| 82 | + |
| 83 | +## Example |
| 84 | + |
| 85 | +```ts filename="middleware.ts" |
| 86 | +import { NextRequest, NextResponse } from 'next/server' |
| 87 | + |
| 88 | +export async function middleware(request: NextRequest) { |
| 89 | + // Next.js automatically buffers the body with the configured size limit |
| 90 | + // You can read the body in middleware... |
| 91 | + const body = await request.text() |
| 92 | + |
| 93 | + // If the body exceeded the limit, only partial data will be available |
| 94 | + console.log('Body size:', body.length) |
| 95 | + |
| 96 | + return NextResponse.next() |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +```ts filename="app/api/upload/route.ts" |
| 101 | +import { NextRequest, NextResponse } from 'next/server' |
| 102 | + |
| 103 | +export async function POST(request: NextRequest) { |
| 104 | + // ...and the body is still available in your route handler |
| 105 | + const body = await request.text() |
| 106 | + |
| 107 | + console.log('Body in route handler:', body.length) |
| 108 | + |
| 109 | + return NextResponse.json({ received: body.length }) |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Good to know |
| 114 | + |
| 115 | +- This setting only applies when middleware is used in your application |
| 116 | +- The default limit of 10MB is designed to balance memory usage and typical use cases |
| 117 | +- The limit applies per-request, not globally across all concurrent requests |
| 118 | +- For applications handling large file uploads, consider increasing the limit accordingly |
0 commit comments