Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

next/image does not load images from external URL after deployment

I use the Next.js Image component for image optimization. It works great on dev but it doesn't load images from external URLs in production.

What can I do?

like image 673
Mahdi Avatar asked Dec 03 '20 10:12

Mahdi


2 Answers

You need to set configuration on the next.config.js file first

Example:

on next.config.js

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

on pages/your_page_file.tsx

<Image
    alt="The guitarist in the concert."
    src="https://images.unsplash.com/photo-1464375117522-1311d6a5b81f?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=2250&q=80"
    width={2250}
    height={1390}
    layout="responsive"
/>
like image 84
Athachai Avatar answered Sep 28 '22 04:09

Athachai


For future references, I was having the same problem after deploying my next.js site to Netlify. Only later, reading my logs, I found

Image domains set in next.config.js are ignored. Please set the env variable NEXT_IMAGE_ALLOWED_DOMAINS to "cdn.sanity.io" instead

So this is probably something to note. In the meanwhile before I saw it, I plugged this next-sanity-image plugin https://www.sanity.io/plugins/next-sanity-image to my page, which also bypassed the problem

like image 31
op_exchanger Avatar answered Sep 28 '22 03:09

op_exchanger