image-optimizer: add configurable upstream fetch timeout #87593
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.
Summary
This PR introduces a new experimental configuration option,
experimental.imgFetchTimeoutInSeconds, to make the upstream fetch timeout used by Next.js Image Optimization configurable for remote images.The default behavior is unchanged (7s). Setting the value to
0disables the upstream fetch timeout (noAbortSignal.timeoutis attached).Motivation
Remote image optimization can fail on slower origins due to the hard-coded upstream fetch timeout. Making this timeout configurable allows applications to tune the bound while preserving Next.js’s default “bounded work” behavior.
Changes
Add
experimental.imgFetchTimeoutInSecondsto the Next.js config schema/shared config:undefined=> default remains 7 seconds (no behavior change).0=> disable upstream fetch timeout (noAbortSignal.timeout; upstream waits can be unbounded).Apply the configured fetch timeout in the remote image fetch path used by
/_next/image.Harden timeout detection and ensure stable error semantics:
TimeoutErroris always treated as an upstream fetch timeout.AbortError(includingerr.cause) is treated as an upstream fetch timeout only when a timeout signal was attached for that request.ImageError504 with a stablecode(e.g.IMAGE_FETCH_TIMEOUT).Documentation updates:
experimental.imgFetchTimeoutInSecondsfor both App Router (next/image) and Pages Router legacy Image docs, including the0escape hatch and warning about unbounded waits.Behavior / Compatibility
Config example
Note: experimental.imgOptTimeoutInSeconds continues to control the Sharp processing timeout; this new option only affects the upstream fetch timeout.
Tests
Related