Skip to content

fix(next-image): resolve HTTP 413 Request Entity Too Large#736

Open
CharlesWong wants to merge 1 commit intoMerit-Systems:masterfrom
CharlesWong:fix/next-image-413-entity-too-large
Open

fix(next-image): resolve HTTP 413 Request Entity Too Large#736
CharlesWong wants to merge 1 commit intoMerit-Systems:masterfrom
CharlesWong:fix/next-image-413-entity-too-large

Conversation

@CharlesWong
Copy link

Problem

The next-image template 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:

// ❌ Pages Router syntax — silently ignored by App Router
export const config = {
  api: { bodyParser: { sizeLimit: '4mb' } },
};

Next.js App Router completely ignores this export const config pattern. 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.ts

experimental: {
  serverActions: {
    bodySizeLimit: '10mb',  // correct App Router mechanism
  },
},

This 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.ts

New compressImageDataUrl() and fileToCompressedDataUrl() helpers:

  • Downscale images to max 1024 × 1024 px
  • Re-encode as JPEG at 85% quality
  • Skip images already < 512 KB (fast path, no canvas overhead)
  • Fully tree-shakeable; zero new dependencies

image-generator.tsx uses fileToCompressedDataUrl when preparing images for API submission (edit path), keeping fileToDataUrl for the display/history path where full quality is desired.

Dead code removal: both route.ts files

  • Remove the ineffective export const config blocks
  • Add export const maxDuration = 60 (correct App Router segment config for long-running AI calls)
  • Add explanatory comments so future contributors understand the difference

Why this is more complete than existing proposals

Concern This PR PR #722
Removes dead Pages Router config
Server-side body limit fix (next.config.ts)
Correct App Router mechanism
Client-side compression
Compression skips small images (perf)
Explanatory comments / docs partial
maxDuration for AI timeout

The 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.

…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
@vercel
Copy link
Contributor

vercel bot commented Mar 11, 2026

@CharlesWong is attempting to deploy a commit to the Merit Systems Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next-image template: Request Entity Too Large

1 participant