Skip to content

fix(next): prevent 1MB body limit error when uploading files - #15078

Closed
sohamingle wants to merge 5 commits into
payloadcms:mainfrom
sohamingle:fix/client-file-size
Closed

sohamingle wants to merge 5 commits into
payloadcms:mainfrom
sohamingle:fix/client-file-size

Conversation

@sohamingle

@sohamingle sohamingle commented Jan 3, 2026 •

Copy link
Copy Markdown

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
r1tsuu added a commit that referenced this pull request Jan 12, 2026
Fixes #15073
Surprasses #15078

We use the `deepCopyObjectSimpleWithoutReactComponents` function to
serialize data for the form state action. This PR adds a new option to
`deepCopyObjectSimpleWithoutReactComponents` - `excludeFiles: boolean`
and adds usage of it in places where we call the form state action to:

* Avoid overhead of sending the file on every form state calculation
when the collection has uploads enabled, since we don't actually use the
file inside the form state action.
* Fix the issue with client uploads when the file exceeds the platform
limit.
@DanRibbens

Copy link
Copy Markdown
Contributor

Thank you for working on a solution for this! Unfortunately, we made a confusing situation from earlier regressions which should all be fixed by the recently merged #15174.

Closing this but we really appreciate taking your time on this.

@DanRibbens DanRibbens closed this Jan 12, 2026
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

3 participants