# Images

> Responsive images with no optimizer to run — unpic for a CDN, imagetools for files in the repo.

There is no image component and no image server. Images on a CDN get their
`srcset` from [unpic](https://unpic.pics), and images in your repository are
resized once, at build time:

```tsx title="src/app/page.tsx"
import { Image } from '@unpic/react';

<Image src="https://res.cloudinary.com/demo/image/upload/sample.jpg" width={800} height={600} alt="A sample" />
```

Neither runs at request time, and neither adds JavaScript to the page.

## Images on a CDN

If the file is on Cloudinary, imgix, Cloudflare Images, Bunny, Vercel, Netlify
or any of the [other providers unpic knows](https://unpic.pics/img/react/), it
already has a resizing URL. unpic writes the `srcset` against it:

```tsx title="src/app/page.tsx"
import { Image } from '@unpic/react';

<Image
  src="https://res.cloudinary.com/demo/image/upload/sample.jpg"
  layout="constrained"
  width={800}
  height={600}
  alt="A sample"
/>
```

It is a plain component, so it renders in a server component and the browser
receives only an `<img>`:

- a `srcset` from 640w up, and `sizes`;
- an `aspect-ratio`, so the layout doesn't jump;
- `loading="lazy"` and `decoding="async"`.

None of unpic's code reaches the browser. The CDN is detected from the URL, so
there is nothing to configure. TanStack Start and Astro use the same library.

### A hero image

The image at the top of a page should load at once. Turn lazy loading off and
raise its priority:

```tsx
<Image
  src="https://res.cloudinary.com/demo/image/upload/hero.jpg"
  layout="fullWidth"
  height={480}
  priority
  alt="…"
/>
```

`layout="fullWidth"` fills the container's width. `priority` sets
`loading="eager"` and `fetchpriority="high"`.

### An uploaded image

A user's upload saved to a CDN is the same: store the URL, then pass it to
`Image`:

```tsx title="src/app/users/[id]/page.tsx"
import { Image } from '@unpic/react';

export default async function Profile({ params }: { params: Promise<{ id: string }> }) {
  const user = await findUser((await params).id);

  return <Image src={user.avatarUrl} width={96} height={96} alt={user.name} />;
}
```

## Images in the repository

[vite-imagetools](https://github.com/JonasKruckenberg/imagetools) resizes a file
under `src/` once, when the app builds. Add the plugin:

```ts title="vite.config.ts"
import { imagetools } from 'vite-imagetools';

export default defineConfig({
  plugins: [imagetools(), rscKit(), ...],
});
```

Then say what you want in the import's query:

```tsx title="src/app/page.tsx"
import hero from '../hero.png?w=400;800;1200&format=webp&as=srcset';
import heroSrc from '../hero.png?w=800&format=webp';

<img srcSet={hero} src={heroSrc} sizes="(min-width: 800px) 800px, 100vw" width={800} height={600} alt="…" />
```

The build writes `hero-<hash>.webp` at each width into `assets/`, hashed and
cacheable forever. A stored page carries the URLs.

Resizing costs build time, once per image per width, and nothing at request
time. That is why it is opt-in: a hundred hero images at four widths is a
noticeable build, and most of them belong on a CDN.

### Typing the imports

Declare the queries, so TypeScript accepts them and the build's typecheck lets
them through:

```ts title="src/images.d.ts"
// A pattern may hold one `*`, so each matches on the tail of the query.
declare module '*&as=srcset' {
  const srcset: string;
  export default srcset;
}
declare module '*&format=webp' {
  const url: string;
  export default url;
}
```

## Icons and logos

A small image that doesn't need resizing is a plain `<img>`, or an SVG inlined
as a component:

```tsx
<img src="/logo.svg" width={120} height={32} alt="Acme" />
```

## Choosing one

| The image is | Use |
| --- | --- |
| uploaded by users, or already on a CDN | unpic |
| in the repo, a handful | imagetools |
| in the repo, hundreds | put them on a CDN and use unpic |
| an icon or a logo | `<img>`, or an inline SVG |

## Why there is no image server

`next/image` is two things together: a component that writes `srcset` and
`sizes`, and an optimizer that resizes on the fly. The optimizer is a process
with sharp in it, a cache to manage, and CPU spent on every cold hit. The
component is worth having, and unpic is that. The resizing belongs to whatever
already serves your images.
