Web Development

Large JSON POSTs intermittently rejected with 413 from Next.js API on Vercel despite sizeLimit set

Swoop
1mo ago
Environment

Environment

Next.js 13 app deployed to Vercel using serverless Node.js functions (not Edge runtime). API route /api/ingest accepts JSON up to 25MB and sets export const config = { api: { bodyParser: { sizeLimit: '25mb' } } }. Clients report intermittent HTTP 413 when function cold-starts; retries usually succeed.

Symptoms

Symptoms

Client uploads of 12–20MB JSON intermittently receive 413 Payload Too Large immediately, predominantly during periods with increased cold-starts (deploys or traffic spikes). The 413 is returned before application middleware logs the request. Local Vercel CLI doesn't reproduce 413s.

Observations

Observations

Vercel edge logs show request arrival then 413 returned by the platform edge, not by user code. No explicit content-length validation performed in app. Some successful uploads appear when client sets Content-Length header; chunked Transfer-Encoding cases are more likely to fail.

Already attempted

Already attempted

Verified Next.js config includes sizeLimit, redeployed, attempted to parse raw request body in middleware, implemented a multipart streaming endpoint as experiment, tested direct curl requests with explicit Content-Length — large requests accepted consistently only when sent with Content-Length in many cases.

Question

Question

Why are large JSON uploads intermittently rejected with 413 in production even though sizeLimit is configured, and how can I reliably accept large payloads without moving upload storage off-platform?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Vercel edge/reverse-proxy enforces a hard request body size for some cold-start or routing paths 95%
Next.js serverless runtime body parser configuration not honored before platform proxy checks body size 60%
Clients using Transfer-Encoding: chunked causing proxy to reject pre-body 30%

Summary

Evidence indicates the platform (Vercel CDN/edge reverse proxy) is rejecting some large requests before they reach the runtime — most likely a proxy body-size enforcement or a mismatch where the platform's front door imposes a stricter limit under certain paths (e.g., cold-start routing or when Transfer-Encoding: chunked is used). The app-level sizeLimit is effective only after the platform forwards the body. Reliable solutions are to avoid sending large bodies through the serverless function (use presigned upload to object storage), convert to streaming/multipart upload that the platform supports, or confirm with Vercel support whether the platform enforces an upper bound on request size for serverless functions.

Suggested Fixes

Diagnostic step 1
Switch to presigned uploads: have clients upload JSON as file to S3 (presigned URL), then notify /api endpoint with a small pointer/manifest. This avoids serverless payload limits.
Diagnostic step 2
If JSON must be sent to the function, implement streaming/parsing (e.g., busboy) and test with explicit Content-Length headers; prefer multipart with chunked uploads handled client-side.
Diagnostic step 3
Reproduce with curl including/omitting Content-Length and Transfer-Encoding headers to confirm proxy behavior; capture edge logs to confirm 413 origin.

Continue troubleshooting

Matched from the title, description and tags