Skip to content

Conversation

@TerryTaoYY
Copy link

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 0 disables the upstream fetch timeout (no AbortSignal.timeout is 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.imgFetchTimeoutInSeconds to the Next.js config schema/shared config:

    • Must be a finite, non-negative integer.
    • undefined => default remains 7 seconds (no behavior change).
    • 0 => disable upstream fetch timeout (no AbortSignal.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:

    • TimeoutError is always treated as an upstream fetch timeout.
    • AbortError (including err.cause) is treated as an upstream fetch timeout only when a timeout signal was attached for that request.
    • Upstream fetch timeouts map to ImageError 504 with a stable code (e.g. IMAGE_FETCH_TIMEOUT).
  • Documentation updates:

    • Document experimental.imgFetchTimeoutInSeconds for both App Router (next/image) and Pages Router legacy Image docs, including the 0 escape hatch and warning about unbounded waits.

Behavior / Compatibility

  • Default behavior remains unchanged: upstream remote image fetch timeout is 7s unless configured.
  • No redirect fallback is introduced; timeouts continue to surface as 504.
  • This PR does not implement request header/cookie forwarding for remote images (out of scope; requires careful cache-key isolation and security considerations).

Config example

// next.config.js
module.exports = {
  experimental: {
    imgFetchTimeoutInSeconds: 30, // 0 disables the upstream fetch timeout
  },
}

Note: experimental.imgOptTimeoutInSeconds continues to control the Sharp processing timeout; this new option only affects the upstream fetch timeout.

Tests

  • pnpm --filter next build
  • pnpm jest --runTestsByPath test/unit/image-optimizer/fetch-external-image.test.ts
  • pnpm jest --runTestsByPath test/integration/image-optimizer/test/index.test.ts

Related

Introduce experimental.imgFetchTimeoutInSeconds to control upstream fetch timeouts for remote images (default 7s; 0 disables the fetch timeout).

Harden timeout classification (TimeoutError + AbortError when a timeout signal is used) and map fetch timeouts to a stable 504 ImageError code.

Update next/image docs and add unit + integration coverage.
@nextjs-bot nextjs-bot added Documentation Related to Next.js' official documentation. tests type: next labels Dec 21, 2025
@nextjs-bot
Copy link
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: 4e15e61

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

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

Labels

Documentation Related to Next.js' official documentation. tests type: next

2 participants