fix: raise Next.js proxy body buffer so large uploads work - #846
Open
BrianBoyCN wants to merge 1 commit into
Open
BrianBoyCN wants to merge 1 commit into
BrianBoyCN wants to merge 1 commit into
Conversation
Next.js buffers the request body whenever a proxy/middleware is present and caps that buffer at 10 MB by default. Pi Web's upload route accepts up to 100 MB per request, so any upload over 10 MB was silently truncated by the proxy layer and failed with 'Failed to parse body as FormData.' (500) instead of succeeding or returning the intended 413. Set experimental.proxyClientMaxBodySize from a new PI_WEB_MAX_BODY_SIZE env var (default 128mb). The value is read at config load, so it can be changed without rebuilding. Accepts b/kb/mb/gb suffixes or a raw byte count; invalid values fall back to the default.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Uploading a file larger than 10 MB through the Pi Web file panel fails with HTTP 500:
{"error":"Failed to parse body as FormData."}Smaller uploads work. The failure is deterministic at exactly the 10 MB boundary, and it is not a proxy problem — it reproduces on
127.0.0.1:3000with no reverse proxy in the path.Root cause
Pi Web registers
proxy.tswithmatcher: ["/", "/login", "/api/:path*"], so every API request passes through the Next.js proxy/middleware layer. When a proxy is present, Next.js clones and buffers the request body and caps that buffer at 10 MB by default (experimental.proxyClientMaxBodySize).Once the body exceeds the cap, Next only forwards the first 10 MB. The upload route's
parseFormDataWithinLimit()then rebuilds aResponsefrom the truncated bytes, and undici's multipart parser throwsFailed to parse body as FormData.— which the route surfaces as a 500.The server log shows the real reason:
The upload route advertises a 25 MB per-file / 100 MB per-request limit with dedicated 413 responses, but requests never reach that logic — the proxy layer truncates them first.
Fix
Set
experimental.proxyClientMaxBodySizeabove the route's own 100 MB cap, configurable via a newPI_WEB_MAX_BODY_SIZEenvironment variable (default128mb):bin/max-body-size.mjsparsesb/kb/mb/gbsuffixes or a raw byte count, falling back to128mbfor blank/invalid input.next.config.tsloads, so it can be changed without rebuilding (verified below).pi-web --helplists the new variable.Verification
Built the app, then against the production server:
Failed to parse body as FormData.Each upload must be 25MB or smaller(route's own limit)Runtime override confirmed by starting the built server with
PI_WEB_MAX_BODY_SIZE=1mb: a 5 MB upload then fails with the 1 MB buffering warning, proving the env var takes effect without a rebuild.npm test(1024 passing),npx tsc --noEmit, andnpm run lintare all green. New unit tests cover size parsing/fallback and assert the config stays above the 100 MB upload cap.Fixes the missing 10 MB buffering configuration introduced when
proxy.tswas added.