fix(next-image): resolve HTTP 413 Request Entity Too Large#736
Open
CharlesWong wants to merge 1 commit intoMerit-Systems:masterfrom
Open
fix(next-image): resolve HTTP 413 Request Entity Too Large#736CharlesWong wants to merge 1 commit intoMerit-Systems:masterfrom
CharlesWong wants to merge 1 commit intoMerit-Systems:masterfrom
Conversation
…imit (Merit-Systems#561) The template had `export const config = { api: { bodyParser: { sizeLimit: '4mb' } } }` in both route handlers. This is Pages Router syntax and is **silently ignored** by Next.js App Router, leaving the default body size limit in place and causing HTTP 413 errors when users submit large base64-encoded images. ## Root cause App Router route handlers do not honour the `export const config` body-parser option. The correct mechanism is: - `experimental.serverActions.bodySizeLimit` in `next.config.ts` ## Changes ### next.config.ts — server-side fix (primary fix) - Add `experimental.serverActions.bodySizeLimit: '10mb'` so App Router route handlers accept bodies up to 10 MB (covers multiple high-res images). - Add detailed comments explaining why the old approach was a no-op. ### route.ts (generate-image & edit-image) — dead code removal + docs - Remove the dead `export const config` block from both routes. - Add `export const maxDuration = 60` (App Router segment config). - Add comments explaining where the body-size limit is configured. ### src/lib/image-utils.ts — client-side defence (belt-and-suspenders) - Add `compressImageDataUrl()`: downscales images to max 1024px and re-encodes as JPEG at 85% quality before serialisation. Small images (<512 KB) pass through. - Add `fileToCompressedDataUrl()`: convenience wrapper. - Preserve all existing exports unchanged. ### src/components/image-generator.tsx — wire up compression - Use `fileToCompressedDataUrl` when preparing image files for API submission. - Keep `fileToDataUrl` for the display/history path. Closes Merit-Systems#561
Contributor
|
@CharlesWong is attempting to deploy a commit to the Merit Systems Team on Vercel. A member of the Team first needs to authorize it. |
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
The
next-imagetemplate throws HTTP 413 "Request Entity Too Large" when users submit images for editing. The root cause is that both API route handlers contained dead code:Next.js App Router completely ignores this
export const configpattern. It is Pages Router-only syntax. The default body limit was therefore never raised, causing 413 errors on any moderately-sized image.Closes #561
Fix
This PR applies a two-layer defence that is more complete than any existing proposal:
Layer 1 — Server-side (primary fix):
next.config.tsThis is the only supported way to raise the body-size limit for App Router route handlers in Next.js 15. The PR includes detailed inline comments explaining why the old approach was a no-op.
Layer 2 — Client-side (belt-and-suspenders):
image-utils.tsNew
compressImageDataUrl()andfileToCompressedDataUrl()helpers:image-generator.tsxusesfileToCompressedDataUrlwhen preparing images for API submission (edit path), keepingfileToDataUrlfor the display/history path where full quality is desired.Dead code removal: both
route.tsfilesexport const configblocksexport const maxDuration = 60(correct App Router segment config for long-running AI calls)Why this is more complete than existing proposals
next.config.ts)maxDurationfor AI timeoutThe server-side fix alone (
next.config.ts) is what actually prevents 413 errors. Without it, any image larger than ~750 KB base64 will still fail regardless of client-side compression.