Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

400 bad request from next/image

So I am trying to display images with next/image's component. However, the images are not being displayed. I believe this is because of a 400 bad request error next/image is giving me.

enter image description here

When I click on that URL, it says "The requested resource isn't a valid image" which is strange because after retrieving the image url from the backend, I AM able to download the image to see that it is a valid image, so this error is happening right after the image link is passed in the props of the component. Basically, my requests are correct, but next/image's interaction with the image url is being messed up. What's weird is that I also did not have this error a few days ago, and after not changing anything, I'm seeing this error.

I've configured the next.js config file like this, and the request to the backend does retrieve a downloadable image (next/image is just not displaying it correctly).

Here is my config file for next.js:

const withPlugins = require('next-compose-plugins');
const withImages = require('next-images');

const nextConfig = {
  images: {
    domains: [
      'url.s3.amazonaws.com',
      'url.s3.amazonaws.com',
      'url.s3.amazonaws.com',
    ],
  },
  
};


module.exports = withPlugins([[withImages]], nextConfig);
like image 871
emma Avatar asked May 25 '21 18:05

emma


3 Answers

This issue is due to next.js version 11. The issue has been fixed with the [email protected] version. You can update the version. The problem will be solved.

like image 109
Gucal Avatar answered Oct 08 '22 04:10

Gucal


I'm late for the topic, but hope my answer will help someone else.

Adding the domain's config into the next.config.js is not enough (only work for local):

module.exports = {
  ...
  images: {
    domains: ['firebasestorage.googleapis.com'],
  }
}

For production, you need to make sure that your "next" instance grabs that config.

So in my case, what I did to make it work is:

Before

const nextjsDistDir = join("src", require("./src/next.config.js").distDir);
const nextjsServer = next({
  dev: isDev,
  conf: {
    distDir: nextjsDistDir
  }
});

After

const nextjsDistDir = join("src", require("./src/next.config.js").distDir);
const nextjsServer = next({
  dev: isDev,
  conf: {
    distDir: nextjsDistDir,
    images: {
      domains: ['firebasestorage.googleapis.com'],
    }
  }
});
like image 23
doannx Avatar answered Oct 08 '22 03:10

doannx


Using loader function solves the issue. Don't know why. But updating the version is the best option.

<Image
    loader={()=>user.coverImage}
      src={user.coverImage}
      alt="user cover image"
      layout="fill"
      objectFit="cover"
/>
like image 1
Shiroshan Avatar answered Oct 08 '22 05:10

Shiroshan