nextjs data_error ai_generated partial

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
1Evidence
2024-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
[email protected] active
[email protected] active
[email protected] active

Root Cause

Server Actions have a default body size limit of 4MB; uploading large files or sending large FormData objects triggers this error.

generic

中文

服务器操作的默认主体大小限制为 4MB;上传大文件或发送大型 FormData 对象会触发此错误。

Official Documentation

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

Workarounds

  1. 85% success Increase the body size limit for the specific server action route in next.config.js: export default { experimental: { serverActions: { bodySizeLimit: '10mb', }, }, }; Or set it per action using the config export: export const config = { api: { bodyParser: { sizeLimit: '10mb' } } };
    Increase the body size limit for the specific server action route in next.config.js:
    
    export default {
      experimental: {
        serverActions: {
          bodySizeLimit: '10mb',
        },
      },
    };
    
    Or set it per action using the config export: export const config = { api: { bodyParser: { sizeLimit: '10mb' } } };
  2. 90% success Upload large files directly to a cloud storage service (e.g., S3) using a presigned URL, and only send the file reference to the server action.
    Upload large files directly to a cloud storage service (e.g., S3) using a presigned URL, and only send the file reference to the server action.
  3. 75% success Implement chunked uploads client-side, sending parts of the file as separate server actions, then reassemble on the server.
    Implement chunked uploads client-side, sending parts of the file as separate server actions, then reassemble on the server.

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. 40% fail

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

  2. 70% fail

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

  3. 60% fail

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