Demo, all content is generated
Question

Server action upload fails with 'Body exceeded 1 MB limit'

Solved · 162 views · asked by imani_w · edited

Users upload a PDF (usually 2–8 MB) through a form that posts to a server action. Anything above 1MB:

Error: Body exceeded 1 MB limit.
To configure the body size limit for Server Actions, see: https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit
What I’ve tried

Set serverActions.bodySizeLimit to '10mb' in next.config. Works locally, on Vercel the big ones still fail with a 413.

Comment
Local works because there's no platform limit locally, classic. kenji_w · edited

3 answers

Marked as helpful by the asker
rafa_dev · edited

Right, bodySizeLimit is Next's limit, but Vercel functions have their own request body limit of 4.5MB that you can't raise. So files shouldn't go through your function at all.

Upload straight from the browser to storage with a signed URL:

  1. Server action creates a signed upload URL (Supabase createSignedUploadUrl, S3 presigned PUT, Vercel Blob client uploads, whatever you use).
  2. Browser uploads the file directly to that URL.
  3. Browser calls a second action with just the path to save the record.

Your function only handles tiny JSON, and uploads can be as big as your storage allows.

Comment
I'm on supabase so createSignedUploadUrl it is. works with a 12mb test file imani_w · edited
mo_saleh · edited

When the browser uploads directly, validate on the save step: check the file exists at that path, the size and the content type. The signed URL means users can upload whatever they want to that path, your server should not trust the client's claim that it's a PDF.

Comment
good point, adding a size + mime check on the save step imani_w · edited
jonas_k · edited

If you're not tied to Supabase: Vercel Blob has a client upload helper (handleUpload on the server, upload() in the browser) that does exactly this flow with a token.

Comment