nextjs data_error ai_generated partial

错误:服务器操作负载超过最大大小限制(4MB)。

Error: Server Action payload exceeds the maximum size limit (4MB).

ID: nextjs/server-action-body-size-exceeded

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://nextjs.org/docs/app/api-reference/functions/server-actions#size-limitation

解决方案

  1. 在 next.config.js 中增加特定服务器操作路由的主体大小限制:
    
    export default {
      experimental: {
        serverActions: {
          bodySizeLimit: '10mb',
        },
      },
    };
    
    或者使用配置导出为每个操作设置:export const config = { api: { bodyParser: { sizeLimit: '10mb' } } };
  2. 使用预签名 URL 将大文件直接上传到云存储服务(例如 S3),只将文件引用发送到服务器操作。
  3. 在客户端实现分块上传,将文件的一部分作为单独的服务器操作发送,然后在服务器上重新组装。

无效尝试

常见但无效的做法:

  1. 40% 失败

    This works but may not be allowed in some hosting environments; also, it's a security risk if not carefully scoped.

  2. 70% 失败

    Server Actions expect FormData; compression breaks the form structure unless handled server-side.

  3. 60% 失败

    This adds complexity and may cause race conditions or partial failures without proper transaction handling.