Back to results for “Security”
Next.js › Image & Font Optimization

How do you configure Next.js to allow optimizing images that are hosted on an external domain?

Intermediate

Answer

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.

Common follow-ups: What happens if you try to use an external image domain that has not been configured?;Is there a performance cost to optimizing many external images?