question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CORS Error when fetching images (maybe only when image comes from AWS)

See original GitHub issue

What version of Next.js are you using?

10.0.2

What version of Node.js are you using?

14.16.1

What browser are you using?

Firefox and Chrome

What operating system are you using?

Ubuntu

How are you deploying your application?

next start

Describe the Bug

The new version of Next seems to be having problems with CORS. I think this error, for some reason I couldn’t figure out, only happens when the image comes from AWS.

Expected Behavior

To check the expected follow these steps:

  1. Create a new package
  yarn create next-app bug-next-demo
  1. In pages/index.js, place this content:
import Image from 'next/image'

export default function Home() {
  return (
    Image src="https://assets.instabuy.com.br/ib.store.image.medium/m-99852d734ad2478aa05dfeff49e29085.png" width={60} height={60} />
  )
}

  1. Set the next.config.js.
  module.exports = {
    images: {
    domains: [
     'assets.instabuy.com.br',
    ]
  }
}
  1. Change the next version to 10.0.9
  // package.json
  "dependencies": {
    "next": "10.0.9"
   }
  1. After installing the dependencies, and running them on the index page you will check this in your web browser: Screenshot from 2021-05-11 15-18-29

To Reproduce

To reproduce, you just need to update next package to 10.2.0 from the expected behavior scenario, as follows:

    "dependencies": {
      "next": "10.2.0",
    }

Now, just re-run the installation and the development execution on the index page of the browser. You will check the following CORS error: Screenshot from 2021-05-11 15-11-45

Note:

This image comes from AWS. It doesn’t seem to happen from other sources.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SarhadSalamcommented, May 11, 2021

I don’t think this is a bug. It is not related to the CORS issue.

If you refer to this file at line 193 you can see that a new condition was added to check for the content type header of a response.

After checking the link for the image you provided, the Content-Type header provided by the image is binary/octet-stream and not an image/png as I believe it is required to be to use the Image module provided by Next.js.

In order to fix it, for your S3 bucket change Content-Type of the image your are providing to image and I believe it should work fine 😃

Hopefully that helps!

0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting CORS - Amazon Simple Storage Service
If you encounter unexpected behavior while accessing buckets set with the CORS configuration, try the following steps to troubleshoot:.
Read more >
fetching images from Amazon s3 gives CORS error. (Chrome ...
and the error I receive is : Image from origin 'https://s3.amazonaws.com' has been blocked from loading by Cross-Origin Resource Sharing policy: ...
Read more >
Dealing with image CORS error in Chrome, Chromium and ...
The issue comes from the way that Chromium caches the images. The way that the initial image is cached is - without the...
Read more >
How to solve S3 CORS file error with "Access-Control-Allow ...
Did you upload a file to S3 to power your web application or visualization, and now it just won't work? Let's figure out...
Read more >
CORS is still getting error - Images - Cloudflare Community
Someone need to fix Cloudflare Image CORS issue, it's been a while and no one help us. Cloudflare Images CORS error.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found