Next.js › Image & Font Optimization
How do you configure Next.js to allow optimizing images that are hosted on an external domain?
IntermediateAnswer
You add the external domain's hostname to the images configuration inside your next.config.js file, since Next.js only optimizes images from domains you explicitly trust, preventing it from being used to proxy and optimize arbitrary images from anywhere on the internet.
// next.config.js
module.exports = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'images.example.com' }
]
}
};Real-world example An online store fetching product images from a separate content delivery network adds that domain to its Next.js configuration, allowing the Image component to properly optimize those external images.