Skip to content

fix: upload images to hosted URLs to resolve 413 errors#777

Open
selenaalpha77-sketch wants to merge 4 commits intoMerit-Systems:masterfrom
selenaalpha77-sketch:fix/next-image-413-hosted-urls
Open

fix: upload images to hosted URLs to resolve 413 errors#777
selenaalpha77-sketch wants to merge 4 commits intoMerit-Systems:masterfrom
selenaalpha77-sketch:fix/next-image-413-hosted-urls

Conversation

@selenaalpha77-sketch
Copy link
Copy Markdown

/claim #561

Problem

Large images sent as base64 in JSON request bodies exceed Next.js's 4MB default body size limit, causing HTTP 413 errors for image generation and editing.

Solution

Images are now uploaded to Vercel Blob storage and passed as hosted URLs instead of base64 strings. This approach:

  • Works for all image sizes (no client-side resize needed)
  • Compatible with both OpenAI and Gemini APIs (both accept url image inputs)
  • No changes needed to downstream API calls

Changes

  • New /api/upload-image endpoint using multipart form data
  • All 4 API handlers (generate/edit × openai/gemini) updated to accept hosted URLs
  • Client updated to upload via FormData instead of base64 JSON
  • Removed invalid config.api.bodyParser from App Router routes
  • Added maxDuration = 60 for long-running image operations
  • Updated .env.local with BLOB_READ_WRITE_TOKEN instructions

…rors

Closes Merit-Systems#561

The previous approach passed base64-encoded images through Next.js API routes,
hitting Vercel's function payload limit (FUNCTION_PAYLOAD_TOO_LARGE / HTTP 413).

Changes:
- Add @vercel/blob dependency for image hosting
- New /api/upload-image endpoint: accepts multipart form data, stores in Vercel Blob, returns hosted URL
- generate-image (openai + gemini): store generated image in Vercel Blob, return hosted URL instead of base64
- edit-image (openai): fetch images from hosted URLs server-side instead of receiving base64
- edit-image (gemini): pass hosted URLs directly (Gemini supports URL inputs natively)
- edit-image (openai + gemini): store edited image in Vercel Blob, return hosted URL
- image-generator.tsx: upload attachment files via /api/upload-image before sending to edit API
- Remove invalid Pages Router config (export const config = { api: { bodyParser: ... } }) from App Router routes
- Add export const maxDuration = 60 for longer generation timeouts on Vercel
- Update .env.local and README with BLOB_READ_WRITE_TOKEN setup instructions
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 25, 2026

@selenaalpha77-sketch is attempting to deploy a commit to the Merit Systems Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

@vercel vercel Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestions:

  1. Next.js Image component will fail to load images from Vercel Blob storage due to missing images.remotePatterns configuration
  1. Image utility functions crash when receiving hosted URLs because they expect data URL format

Fix on Vercel

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.

1 participant