nextjs
data_error
ai_generated
partial
错误:服务器操作负载超过最大大小限制(4MB)。
Error: Server Action payload exceeds the maximum size limit (4MB).
ID: nextjs/server-action-body-size-exceeded
80%修复率
82%置信度
1证据数
2024-01-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
根因分析
服务器操作的默认主体大小限制为 4MB;上传大文件或发送大型 FormData 对象会触发此错误。
English
Server Actions have a default body size limit of 4MB; uploading large files or sending large FormData objects triggers this error.
官方文档
https://nextjs.org/docs/app/api-reference/functions/server-actions#size-limitation解决方案
-
在 next.config.js 中增加特定服务器操作路由的主体大小限制: export default { experimental: { serverActions: { bodySizeLimit: '10mb', }, }, }; 或者使用配置导出为每个操作设置:export const config = { api: { bodyParser: { sizeLimit: '10mb' } } }; -
使用预签名 URL 将大文件直接上传到云存储服务(例如 S3),只将文件引用发送到服务器操作。
-
在客户端实现分块上传,将文件的一部分作为单独的服务器操作发送,然后在服务器上重新组装。
无效尝试
常见但无效的做法:
-
40% 失败
This works but may not be allowed in some hosting environments; also, it's a security risk if not carefully scoped.
-
70% 失败
Server Actions expect FormData; compression breaks the form structure unless handled server-side.
-
60% 失败
This adds complexity and may cause race conditions or partial failures without proper transaction handling.