fix: resolve HTTP 413 in next-image template via client-side compression#738
Open
Gengyscan wants to merge 1 commit intoMerit-Systems:masterfrom
Open
fix: resolve HTTP 413 in next-image template via client-side compression#738Gengyscan wants to merge 1 commit intoMerit-Systems:masterfrom
Gengyscan wants to merge 1 commit intoMerit-Systems:masterfrom
Conversation
- Remove dead Pages Router bodyParser config from App Router handlers - Add compressImage() utility for client-side image resizing - Compress images before sending to edit API Fixes Merit-Systems#561
Contributor
|
Someone is attempting to deploy a commit to the Merit Systems Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
Hi! Just pinging on this fix for the HTTP 413 issue with next-image. Let me know if you need any adjustments or have questions about the approach. |
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.
Fix #561: Resolve HTTP 413 Request Entity Too Large in next-image template
Problem
The
next-imagetemplate throws HTTP 413 "Request Entity Too Large" when users submit images for editing. Two root causes:Dead body parser config: Both route handlers export a
configobject using Pages Router syntax (export const config = { api: { bodyParser: { sizeLimit } } }). This is silently ignored by App Router — it has no effect and the default body size limit applies.Uncompressed base64 payloads: Images are sent as base64 data URLs embedded in JSON request bodies. A typical smartphone photo (3-12 MB) becomes 4-16 MB as base64, easily exceeding platform limits (Vercel: 4.5 MB max for serverless functions).
Solution
Client-side image compression before sending to the API:
export const configblocks are misleading dead code in App Router. Removed from both route handlers.compressImage()utility — Resizes images to max 2048×2048 pixels and applies JPEG compression (quality 0.85) before encoding as data URLs. Preserves PNG format for images with transparency./api/edit-image.Why this approach
Per @sragss's analysis, option 2 (blob store) is the long-term ideal but "decreases the ability to rapidly self-host." Client-side compression:
Changes
src/lib/image-utils.tscompressImage()— Canvas-based resize + JPEG compressionsrc/app/api/edit-image/route.tsexport const configblocksrc/app/api/generate-image/route.tsexport const configblocksrc/components/image-generator.tsxNotes
/api/generate-image) only send text prompts, so they're unaffected by body size limits. The dead config was still removed for correctness.compressImagefunction gracefully falls back to the original data URL if Canvas context is unavailable.Fixes #561