vercel/next.js · error · ImageError

Unable to optimize image and unable to fallback to upstream

Error message

Unable to optimize image and unable to fallback to upstream image

What it means

Thrown by imageOptimizer when the optimization step (optimizeImage via the native SWC/sharp binding) fails AND there is no upstreamType to fall back to. Without a detected content type the optimizer cannot return the original bytes, so it errors out with HTTP 400.

Source

Thrown at packages/next/src/server/image-optimizer.ts:1208

      buffer: optimizedBuffer,
      contentType,
      maxAge,
      etag: getImageEtag(optimizedBuffer),
      upstreamEtag,
    }
  } catch (error) {
    if (upstreamType) {
      // If we fail to optimize, fallback to the original image
      return {
        buffer: upstreamBuffer,
        contentType: upstreamType,
        maxAge: nextConfig.images.minimumCacheTTL,
        etag: upstreamEtag,
        upstreamEtag,
        error,
      }
    } else {
      throw new ImageError(
        400,
        'Unable to optimize image and unable to fallback to upstream image'
      )
    }
  }
}

function getFileNameWithExtension(
  url: string,
  contentType: string | null
): string {
  const [urlWithoutQueryParams] = url.split('?', 1)
  const fileNameWithExtension = urlWithoutQueryParams.split('/').pop()
  if (!contentType || !fileNameWithExtension) {
    return 'image.bin'
  }

  const [fileName] = fileNameWithExtension.split('.', 1)

View on GitHub (pinned to 0ae8c72462)

Solutions

  1. Verify the source image is not corrupt by opening it locally or re-encoding it.
  2. If this appears after a branch switch/upgrade, delete packages/next-swc/native/*.node and reinstall to fix a stale native binary.
  3. Add `unoptimized` to the <Image> to serve the original bytes directly.
  4. Check that images.formats includes the relevant format and that the OS has the needed codecs.

Example fix

// before: corrupt/exotic image fails optimization with no fallback
<Image src="/uploads/photo.heic" width={500} height={500} />

// after: bypass optimizer for unsupported/corrupt assets
<Image src="/uploads/photo.heic" width={500} height={500} unoptimized />
Defensive patterns

Strategy: fallback

Validate before calling

null

Type guard

function isOptimizable(type: string | undefined): boolean {
  return !!type && type.startsWith('image/')
}

Try / catch

try {
  return await imageOptimizer(...)
} catch (e) {
  if (e instanceof ImageError && e.message.includes('Unable to optimize')) {
    return <img src={href} /> // serve original
  }
}

Prevention

When it happens

Trigger: The native image optimizer crashed on a malformed/corrupt image, and detectContentType had also failed earlier in a way that left upstreamType empty, eliminating the fallback path.

Common situations: Corrupt image files that pass an initial sniff but fail during decode/resize, native binary mismatch (stale next-swc .node), or an image format the optimizer doesn't support (e.g. exotic HEIF without support).

Related errors


AI-assisted analysis of vercel/next.js@0ae8c72462 (2026-08-06). Data as JSON: /api/errors/741dd07196ad725b. Report an issue: GitHub.