# Image Optimization

> Resize and convert images on request, with the same `images` settings and `/_vercel/image` URLs as Vercel. Next.js’s `<Image>` works as it is.

- Category: Routing and caching
- URL: https://www.forkrail.com/docs/image-optimization

## Turn it on

Add `images` to `vercel.json`, in Vercel’s shape. The deployment then answers `/_vercel/image?url=…&w=…&q=…` (and `/_next/image` for Next.js).

**vercel.json**

```json
{
  "images": {
    "sizes": [640, 1080, 1920],
    "formats": ["image/avif", "image/webp"],
    "minimumCacheTTL": 60,
    "remotePatterns": [{ "protocol": "https", "hostname": "^images\\.example\\.com$" }]
  }
}
```

Frameworks that bring their own settings work the same way: Next.js’s `images` config, SvelteKit’s adapter `images` option, Astro’s `imageService` and Nuxt Image’s Vercel provider. If `vercel.json` has `images` too, the framework’s are used and the build warns.

## How it works

- `url` is one of the deployment’s own paths, or a URL elsewhere that `domains` or `remotePatterns` allows.
- The image is resized to width `w` (never enlarged) and sent in the first of `formats` the browser accepts.
- JPEG, PNG, WebP and AVIF are resized. GIF, SVG (with `dangerouslyAllowSVG`), icons and animations are served as they are.
- Each resized image is kept once for the whole install, so a resize made through one region is there for every other. The deployment’s own files are kept by content across redeploys; others follow the longer of their `max-age` and `minimumCacheTTL`.
- Images from a function get none of the visitor’s headers (cookies, `Authorization`), since the result is kept for everyone. Keep private images at their own URLs.

## Limits and cost

| What | Limit |
| --- | --- |
| Largest source image | 8,192 pixels a side, as on Vercel |
| Image a function answers | Read up to 4 MB |
| Image from another site | Read up to 50 MB (Vercel reads up to 300 MB), public addresses only, at most 3 redirects |
| Resized images kept | 31 days from when they’re made |

Cost: nothing while unused, then about **$0.011 per 1,000 WebP resizes** (Lambda time and one S3 write), a little more for AVIF, and $0.0004 per 1,000 reads from S3 when a router’s memory doesn’t have the image.
