#Payload cms vercel blob storage images throwing 400 Code: INVALID_IMAGE_OPTIMIZE_REQUEST
1 messages · Page 1 of 1 (latest)
The error 400 Code: INVALID_IMAGE_OPTIMIZE_REQUEST occurs when the image optimization request to the Next.js image API is invalid. According to the documentation, this can happen for several reasons:
- The
urlparameter must start with/,<http://,> or<https://.> - The
urlparameter must match one of the configuredremotePatternsorlocalPatternsin yournext.config.js. - The
urlparameter must have aContent-Typeheader that starts withimage/. - The
urlparameter must return a valid image response (not HTML or another type). - The
w(width) andq(quality) parameters must be valid integers and match allowed values in your config if specified.
In your case, the image URL is:
<https://example.vercel.app/_next/image?url=https%3A%2F%2Fexample.vercel.app%2Fapi%2Fmedia%2Ffile%2Fclient_one-18.png&w=96&q=75>
Possible causes based on the documentation:
-
The image URL must return a valid image response:
If<https://example.vercel.app/api/media/file/client_one-18.png> does not return an image with aContent-Typeheader starting withimage/, the optimization will fail with this error. -
The image URL must match allowed patterns:
If you are using a remote URL (even if it's your own domain), you must ensure that yournext.config.jsincludes aremotePatternsentry that matches<https://example.vercel.app/api/media/file/**.> -
The image endpoint must be publicly accessible:
If the API route is protected or requires authentication, the image optimizer cannot fetch the image.
Troubleshooting steps:
-
Visit
<https://example.vercel.app/api/media/file/client_one-18.png> directly in your browser. If it does not display the image or downloads as a non-image file, check your Payload CMS and storage configuration to ensure the correctContent-Typeis set and the file is accessible. -
Check your
next.config.jsand ensure you have something like:images: { remotePatterns: [ { protocol: 'https', hostname: 'example.vercel.app', pathname: '/api/media/file/**', }, ], } -
Make sure the API route returns the image with the correct headers (
Content-Type: image/pngfor PNG files).
For more details, see the official documentation on this error: INVALID_IMAGE_OPTIMIZE_REQUEST.
If you follow these steps and the error persists, double-check that the image endpoint is not returning an error page (like a 404 or HTML response), as this will also trigger the error you are seeing.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@ocean tapir Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@candid bough directly loading https://example.vercel.app/api/media/file/client_one-18.png loads just fine and if i add the &w=96&q=75 it shows 404
Let me check the knowledge base for you again. Give me one moment! :books:
If loading <https://example.vercel.app/api/media/file/client_one-18.png> directly works, but accessing it through the Next.js image optimizer (/_next/image?url=...&w=96&q=75) returns a 400 error, the issue is likely with the configuration of the Next.js image optimizer or the response from your API route.
According to the official documentation, the INVALID_IMAGE_OPTIMIZE_REQUEST error can occur if:
- The
urlparameter does not match the allowedremotePatternsorlocalPatternsin yournext.config.js. - The response from the image URL does not have a
Content-Typeheader that starts withimage/. - The image endpoint returns a non-image response (such as HTML or a 404 error) when accessed by the optimizer, even if it works in the browser.
- The width (
w) or quality (q) parameters are not valid or not allowed by your configuration.
Key points to check:
-
remotePatterns Configuration
Ensure yournext.config.jsincludes aremotePatternsentry that matches your API route:module.exports = { images: { remotePatterns: [ { protocol: 'https', hostname: 'example.vercel.app', pathname: '/api/media/file/**', }, ], }, }This is required for the optimizer to allow fetching images from your API route see docs.
-
API Route Response
The API route must return the image with aContent-Typeheader that starts withimage/(e.g.,image/png). If the optimizer receives a 404 or a non-image response, it will throw this error see docs.
-
Direct URL Testing
Adding&w=96&q=75to the direct image URL (e.g.,<https://example.vercel.app/api/media/file/client_one-18.png&w=96&q=75>) will not work, because your API route does not expect or handle these query parameters. The optimizer uses these parameters internally, but your API route should ignore or safely handle unknown query parameters. -
Image URL in Optimizer
The correct way to use the optimizer is via the Next.js<Image />component, not by manually appendingwandqto your API route. The optimizer will call your API route with the correct parameters.
Summary:
- Make sure your
next.config.jsallows the API route as a remote image source. - Ensure your API route always returns a valid image with the correct headers, regardless of extra query parameters.
- Do not expect your API route to handle
wandq—these are for the optimizer, not your API.