{"record":{"id":"741dd07196ad725b","repo":"vercel/next.js","slug":"unable-to-optimize-image-and-unable-to-fallback-to","errorCode":null,"errorMessage":"Unable to optimize image and unable to fallback to upstream image","messagePattern":"Unable to optimize image and unable to fallback to upstream image","errorType":"http","errorClass":"ImageError","httpStatus":400,"severity":"error","filePath":"packages/next/src/server/image-optimizer.ts","lineNumber":1208,"sourceCode":"      buffer: optimizedBuffer,\n      contentType,\n      maxAge,\n      etag: getImageEtag(optimizedBuffer),\n      upstreamEtag,\n    }\n  } catch (error) {\n    if (upstreamType) {\n      // If we fail to optimize, fallback to the original image\n      return {\n        buffer: upstreamBuffer,\n        contentType: upstreamType,\n        maxAge: nextConfig.images.minimumCacheTTL,\n        etag: upstreamEtag,\n        upstreamEtag,\n        error,\n      }\n    } else {\n      throw new ImageError(\n        400,\n        'Unable to optimize image and unable to fallback to upstream image'\n      )\n    }\n  }\n}\n\nfunction getFileNameWithExtension(\n  url: string,\n  contentType: string | null\n): string {\n  const [urlWithoutQueryParams] = url.split('?', 1)\n  const fileNameWithExtension = urlWithoutQueryParams.split('/').pop()\n  if (!contentType || !fileNameWithExtension) {\n    return 'image.bin'\n  }\n\n  const [fileName] = fileNameWithExtension.split('.', 1)","sourceCodeStart":1190,"sourceCodeEnd":1226,"githubUrl":"https://github.com/vercel/next.js/blob/0ae8c72462952df163f1b1e0726641bc5b40dc93/packages/next/src/server/image-optimizer.ts#L1190-L1226","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Verify the source image is not corrupt by opening it locally or re-encoding it.","If this appears after a branch switch/upgrade, delete packages/next-swc/native/*.node and reinstall to fix a stale native binary.","Add `unoptimized` to the <Image> to serve the original bytes directly.","Check that images.formats includes the relevant format and that the OS has the needed codecs."],"exampleFix":"// before: corrupt/exotic image fails optimization with no fallback\n<Image src=\"/uploads/photo.heic\" width={500} height={500} />\n\n// after: bypass optimizer for unsupported/corrupt assets\n<Image src=\"/uploads/photo.heic\" width={500} height={500} unoptimized />","handlingStrategy":"fallback","validationCode":"null","typeGuard":"function isOptimizable(type: string | undefined): boolean {\n  return !!type && type.startsWith('image/')\n}","tryCatchPattern":"try {\n  return await imageOptimizer(...)\n} catch (e) {\n  if (e instanceof ImageError && e.message.includes('Unable to optimize')) {\n    return <img src={href} /> // serve original\n  }\n}","preventionTips":["Keep the next-swc native binary in sync with the Next.js version.","Validate source images aren't corrupt before upload.","Use `unoptimized` for exotic formats the optimizer can't handle."],"tags":["image-optimizer","optimization-failure","native-binary"],"analyzedSha":"0ae8c72462952df163f1b1e0726641bc5b40dc93","analyzedAt":"2026-08-06T19:44:29.143Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}