# <Image> (Remote)

> Securing external image optimization.

Subject: Next.js | Lesson 48 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/image-remote
Previous: [<Image> (Local)](https://codewithanimation.in/nextjs/image-local.md) | Next: [<Image> (Advanced)](https://codewithanimation.in/nextjs/image-advanced.md)

## External Images

Unlike local images, Next.js cannot inspect remote images (like those from an S3 bucket or CMS) during the build process.

This introduces two strict requirements: you must explicitly define the image's dimensions, and you must whitelist the remote domain.

## The Security Risk

Image optimization is an expensive server-side process.

If Next.js allowed any external URL, attackers could abuse your server by repeatedly requesting optimizations for massive, malicious, or external third-party images.

To prevent this Server-Side Request Forgery (SSRF) and resource exhaustion, Next.js blocks all external images by default.

## Remote Patterns

To allow an external image, you must whitelist its domain in your `next.config.js` file using `remotePatterns`.

```js
// next.config.js
module.exports = {
  images: {
    remotePatterns: [{ hostname: 'my-cms.com' }],
  },
}
```

Because Next.js cannot inspect the file at build time, you must also provide explicit `width` and `height` props to prevent Cumulative Layout Shift.

## Common mistake: CMS Integration Failures

A very common trap occurs when developers build a blog using a CMS (like Sanity or Contentful).

Everything works locally with placeholder images, but when they connect the CMS in production, every single image immediately breaks with an Unconfigured Host error.

## Try it: The Security Barrier

Watch how the Next.js Optimization Server intercepts external requests. If the domain isn't whitelisted in the configuration, the server drops the request to protect itself.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/image-remote

## Check yourself

### Question 1

Why does Next.js strictly require you to whitelist external domains in `next.config.js` before optimizing them?

- A. To prevent attackers from using your server to optimize arbitrary or malicious images.
- B. To download the images to your local /public folder.
- C. To automatically calculate their width and height.
- D. To bypass CORS restrictions in the browser.

Correct answer: A

Image optimization requires significant server CPU and memory. Whitelisting prevents attackers from abusing your infrastructure to process arbitrary external files.

- Why not B: Next.js optimizes them on-the-fly, it doesn't statically download them.
- Why not C: Next.js cannot calculate dimensions for external images; you must provide them manually.
- Why not D: Optimization happens on the server, so browser CORS is not the primary issue here.

## Remember this

- Remote images require explicit `width` and `height` props to prevent layout shift.
- You must whitelist external domains in `next.config.js` using `remotePatterns`.
- This restriction exists to protect your server from malicious abuse and resource exhaustion.
