400 bad request from next/image

Viewed 9782

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);
6 Answers

This issue is due to next.js version 11. The issue has been fixed with the next@11.0.2-canary.4 version. You can update the version. The problem will be solved.

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'],
    }
  }
});

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"
/>

I had to add a domain name including www prefix into next.config.js. E.g. both googlapis.com and www.googleapis.com.

Run in console at proyect route: rm -rf .next/

Then run the server again and try

Related