Skip to content

Conversation

@sohamingle
Copy link

@sohamingle sohamingle commented Jan 3, 2026

What?

Strip file data from formState before sending it to the getFormState server action to avoid exceeding Next.js Server Actions' 1MB body size limit.

Why?

When uploading files larger than 1MB, the following error occurs:

Error: Body exceeded 1mb limit.
To configure the body size limit for Server Actions

This happens because:

  1. When a file is selected in the Upload component, it's stored in formState.file.value
  2. The Form component's onChange handler (debounced) sends the entire formState to getFormState for validation, field rendering, and permissions checking
  3. getFormState is a Server Action, which has a default 1MB body size limit in Next.js
  4. Files larger than 1MB cause the request to fail

How?

The fix strips the file value from formState before sending it to the server action:

const formStateArgs = rest as typeof rest & { formState?: FormState }
if (formStateArgs.formState?.file?.value instanceof File) {
  formStateArgs.formState = {
    ...formStateArgs.formState,
    file: {
      ...formStateArgs.formState.file,
      value: null,
    },
  }
}

This is safe because:

  • The file value is not needed for form state operations - buildFormState handles field validation, rendering, permissions, and document locking - none of which require the actual file binary
  • The file is preserved on the client side - the original form state in the component still has the file
  • Actual file upload uses a different path - form submission sends the file via REST API (POST /api/[collection]) with multipart form data, not through Server Actions
  • The server already handles this case - in buildFormState.ts, line 223-226, it preserves the file field from the incoming form state, so passing null doesn't break anything

Video Demo

Before

Arc.2026-01-04.01-41-22.mp4

After

Arc.2026-01-04.01-41-58.mp4

How to test?

  1. Create an upload collection (or use an existing one)
  2. Try uploading a file larger than 1MB
  3. Before fix: Error "Body exceeded 1mb limit" appears immediately when file is selected
  4. After fix: File is selected successfully, and form can be submitted

Fixes #15073

…ding Server Action body size limit

### What?
Modifies the ServerFunctionsProvider to remove file data from formState before sending it to the server.

### Why?
To avoid exceeding the 5MB body size limit for Server Actions, as file objects are not needed for form state validation.

### Changes
- Updated the handling of formStateArgs to nullify the file value if it is an instance of File.

No breaking changes introduced.
@sohamingle sohamingle changed the title fix(upload): prevent 1MB body limit error when uploading files fix(next): prevent 1MB body limit error when uploading files Jan 3, 2026
@paulpopus paulpopus requested a review from r1tsuu January 5, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

s3Storage clientUploads still sends file binary to Server Action, causing 413 error

2 participants