Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: serverActions.bodySizeLimit is ignored #59277

Closed
1 task done
TryingToImprove opened this issue Dec 5, 2023 · 9 comments · Fixed by #59877
Closed
1 task done

bug: serverActions.bodySizeLimit is ignored #59277

TryingToImprove opened this issue Dec 5, 2023 · 9 comments · Fixed by #59877
Labels
bug Issue was opened via the bug report template. locked

Comments

@TryingToImprove
Copy link
Contributor

Link to the code that reproduces this issue

https://github.com/TryingToImprove/next-app-router-body-size-issue

To Reproduce

  1. Start the application
  2. Click submit button
  3. It should not throw a error because the data posted is not the same size as what is expected

Current vs. Expected behavior

Should not fail - bodySizeLimit should work

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
Binaries:
  Node: 18.17.0
  npm: N/A
  Yarn: N/A
  pnpm: 8.11.0
Relevant Packages:
  next: 14.0.4-canary.40
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

Relevant code is here: TryingToImprove/next-app-router-body-size-issue@6efd384

@TryingToImprove TryingToImprove added the bug Issue was opened via the bug report template. label Dec 5, 2023
@TryingToImprove TryingToImprove changed the title bug: bodySizeLimit is ignored bug: serverActions.bodySizeLimit is ignored Dec 5, 2023
@LavransBjerkestrand
Copy link

I'm having the same problem, set it to 64mb but in reality it just stays at 1mb/1,048,576 bytes. Tried numerical values but same result

@TryingToImprove
Copy link
Contributor Author

TryingToImprove commented Dec 22, 2023

I have done some investigation I think I have figured out what the issue is.

When using a Server Action as a action on a form the request to the server is a multipart request whereas if making the request in a click handler it a normal request.

When making a multipart request next have a different way of handling the decoding of the data where it uses busboy without any configurations. If busboy is used without any configuration the default limit for a field is 1MB which is way it get truncated.

The place where busboy is used is here: https://github.com/vercel/next.js/blob/canary/packages/next/src/server/app-render/action-handler.ts#L467-L470

There is a test for larger Server Actions here: https://github.com/vercel/next.js/blob/canary/test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts#L79-L120 however it is not testing for form-submits.

I am able to get it work by applying this

--- a/packages/next/src/server/app-render/action-handler.ts
+++ b/packages/next/src/server/app-render/action-handler.ts
@@ -463,8 +463,11 @@ export async function handleAction({

         if (isMultipartAction) {
           if (isFetchAction) {
+            const readableLimit = serverActions?.bodySizeLimit ?? '1 MB'
+            const limit = require('next/dist/compiled/bytes').parse(readableLimit)
+
             const busboy = require('busboy')
-            const bb = busboy({ headers: req.headers })
+            const bb = busboy({ headers: req.headers, limits: { fieldSize: limit } })
             req.pipe(bb)

             bound = await decodeReplyFromBusboy(bb, serverModuleMap)

However there seems to be a mismatch for how next and busboy handle size limits. next sets the limit for the entire body whereas busboy is for each of the fields.

I am not sure how to proceed from here.

@mikeacre
Copy link

mikeacre commented Jan 23, 2024

I am having this issue as well.. forms were working no problem with server actions now all of a sudden today I am having this issue.

Why do devs just randomly decide to implement this kind of behavior? None of the config changes are working and now my forms are limited to 1MB which is insane.

@nikmatswace
Copy link

nikmatswace commented Mar 11, 2024

I'm also having this problem. Forms being limited to 1 MB is a really rough situation to be in if you want to do any kind of image/file uploading.

I'm at Next 14.0.4, is there any kind of workaround?

@TryingToImprove
Copy link
Contributor Author

TryingToImprove commented Mar 11, 2024

I'm also having this problem. Forms being limited to 1 MB is a really rough situation to be in if you want to do any kind of image/file uploading.

I'm at Next 14.0.4, is there any kind of workaround?

I think you would be able to do it with a client-side fetch and a API route.

function submit(formData){
   fetch('/api/upload', { method: 'POST', body: formData })
}

<form action={submit}>...

For large JSON you could do it without FormData

@IVEN21
Copy link

IVEN21 commented Mar 13, 2024

I'm having the same issue, I persist to handle file form submission in server action rather in client.

@JaedonSpurlock01
Copy link

I am also having this issue as well. I am trying to upload JSON data to Amazon S3.

shuding added a commit that referenced this issue Mar 18, 2024
When using Server Actions with a form the fields are getting truncated
at 1MB because of `busboy`'s default `fieldSize` limit of 1MB.

This PR tries to solve #59277
however there is a mismatch about `fieldSize` and `bodySize`. I have
tried creating a PR for `busboy`
mscdex/busboy#351 to allow configuring a max
size for the entire body.

### TODO:

- [ ] Figure out if this is acceptable
- [ ] Throw error when `bodySizeLimit` is hit.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

Fixes #59277, closes #61462.

---------

Co-authored-by: Shu Ding <g@shud.in>
@sendmenas
Copy link

Have the same problem. I am sending base64 file string and the data that is received in server action is always limited to 1048576 characters.

Copy link
Contributor

github-actions bot commented Apr 7, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Apr 7, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
7 participants