#next.config.js
1 messages · Page 1 of 1 (latest)
To use the <Image> component with external URLs in Next.js, you need to configure the next.config.js file to allow remote images. Here’s how you can do it:
-
Update
next.config.js:
Add theremotePatternsconfiguration to specify the domains from which images can be loaded.// next.config.js module.exports = { images: { remotePatterns: [ { protocol: 'https', hostname: 'flagcdn.com', port: '', pathname: '/w20/**', }, ], }, } -
Restart your development server:
After updating thenext.config.jsfile, make sure to restart your development server to apply the changes. -
Use the
<Image>component:
Now you can use the<Image>component with the specified external URL.import Image from 'next/image'; function MyComponent({ option }) { return ( <Image loading="lazy" width={20} height={15} src={`<https://flagcdn.com/w20/${option.code.toLowerCase(>)}.png`} alt={`Flag of ${option.label}`} /> ); } export default MyComponent;
By following these steps, you should be able to use the <Image> component with external URLs in your Next.js application.
For more information, you can refer to the Next.js Image Configuration documentation (https://nextjs.org/docs/app/api-reference/components/image#configuration-options).
If you encounter any issues, make sure to check the console for any warnings or errors that might provide more context on what might be going wrong.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@rapid hamlet kapa.ai is still learning and improving, please let us know how it did by reacting below
Let me check the knowledge base for you. One moment! :books:
Yes, your next.config.js configuration looks good. It correctly specifies the remotePatterns to allow images from the flagcdn.com domain. Here is the properly formatted configuration:
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'flagcdn.com',
port: '',
pathname: '/w20/**',
},
],
},
};
module.exports = nextConfig;
This configuration ensures that images from <https://flagcdn.com/w20/**> are allowed to be optimized and served by the Next.js Image Optimization API.