Skip to content

fix(image): return 502 when upstream request fails - #98573

Open
ShakilUrRehman21 wants to merge 7 commits into
vercel:canaryfrom
ShakilUrRehman21:fix/image-upstream-network-error-98569
Open

ShakilUrRehman21 wants to merge 7 commits into
vercel:canaryfrom
ShakilUrRehman21:fix/image-upstream-network-error-98569

Conversation

@ShakilUrRehman21

Copy link
Copy Markdown

Summary

Fixes #98569

When an upstream image request fails due to a network error, such as DNS resolution failure or connection refusal, /_next/image currently returns a 500 Internal Server Error.

This changes fetchExternalImage to convert non-timeout upstream fetch failures into an ImageError with HTTP 502 (Bad Gateway), indicating that the upstream request failed.

Timeout errors continue to return 504 as before.

Changes

  • Return ImageError(502) for rejected upstream image fetches.
  • Preserve the existing 504 response for upstream timeouts.
  • Add a regression test covering rejected upstream fetch() requests.

Testing

  • ./node_modules/.bin/jest test/unit/image-optimizer/fetch-external-image.test.ts --runInBand
    • 8 tests passed
  • pnpm --filter next build
    • Passed

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: /_next/image returns 500 (raw "fetch failed") when the remote host fails DNS or refuses the connection

1 participant