Skip to content

Responsive images ​

Serve each device the pixels it actually needs: a srcset of CDN-resized variants plus DPR-aware quality. There is no image build pipeline, because every variant is just a URL.

Width-based srcset ​

Each variant is built from a uuid, so these need your CDN base; derive it once outside the loop rather than per width.

ts
import { serializeCdnUrl } from '@uploadcare/cdn-url'
import { resize } from '@uploadcare/cdn-url/ops'

const WIDTHS = [320, 640, 960, 1280, 1920]

function variant(uuid: string, width: number): string {
  return serializeCdnUrl({
    cdnBase: 'https://1s4oyld5dc.ucarecd.net',
    uuid,
    operations: [resize({ width })]
  })
}

const srcset = WIDTHS.map((w) => `${variant(uuid, w)} ${w}w`).join(', ')
ts
import { CdnUrl } from '@uploadcare/cdn-url/builder'
import { resize } from '@uploadcare/cdn-url/ops'

const WIDTHS = [320, 640, 960, 1280, 1920]

function variant(uuid: string, width: number): string {
  return new CdnUrl({
    cdnBase: 'https://1s4oyld5dc.ucarecd.net',
    uuid,
    operations: [resize({ width })]
  }).href
}

const srcset = WIDTHS.map((w) => `${variant(uuid, w)} ${w}w`).join(', ')
ts
import { cdn } from '@uploadcare/cdn-url/fluent'
import { prefixedCdnBase } from '@uploadcare/cdn-url/cdn-base'

const myCdn = cdn.base(prefixedCdnBase('demopublickey'))
const WIDTHS = [320, 640, 960, 1280, 1920]

function variant(uuid: string, width: number): string {
  return myCdn.file(uuid).resize({ width }).href
}

const srcset = WIDTHS.map((w) => `${variant(uuid, w)} ${w}w`).join(', ')
ts
import { modifiers, tinyBuild } from '@uploadcare/cdn-url/tiny'

const cdnBase = 'https://1s4oyld5dc.ucarecd.net'
const WIDTHS = [320, 640, 960, 1280, 1920]

function variant(uuid: string, width: number): string {
  return tinyBuild({
    cdnBase,
    uuid,
    modifiers: modifiers(`resize/${width}x`)
  })
}

const srcset = WIDTHS.map((w) => `${variant(uuid, w)} ${w}w`).join(', ')

A srcset is the case the string level was built for: one known file, one operation per variant, no validation needed, and the whole helper costs a few hundred bytes. The widths interpolate into a template literal in argument position, which is where modifiers still type-checks the operation name.

The resize alone is enough. Once a chain contains a processing operation, the CDN defaults to format/auto (AVIF/WebP negotiation) and applies adaptive quality automatically. See Best practices for when explicit format/quality/progressive overrides still earn their place.

html
<img
  :src="variant(uuid, 960)"
  :srcset="srcset"
  sizes="(max-width: 640px) 100vw, 640px"
  alt="…"
/>

resize({ width: w }) sets the width alone and keeps the aspect ratio, so every variant is exactly as wide as its w descriptor says, landscape or portrait. preview(w, w) would cap the height as well, and a portrait source would come out narrower than its descriptor. Resize also enlarges a source narrower than w; put stretch('off') before it to keep the original instead, at the cost of that variant being narrower than its descriptor.

Pixel-density variants ​

For fixed-size images (logos, avatars), density descriptors are simpler than widths:

ts
const src1x = variant(uuid, 200)
const src2x = variant(uuid, 400)
html
<img src="…200…" srcset="…400… 2x" width="200" height="200" alt="…" />

On high-DPR screens the extra pixels hide compression artifacts, so an explicit quality('lightest') override on the 2x variant typically looks identical to the adaptive default and saves real bytes. See Best practices.

Computing sizes at runtime ​

When you measure the layout instead of hardcoding widths, scale by devicePixelRatio and respect the CDN output ceiling:

ts
const MAX_DIMENSION = 3000 // 5000 with format('jpeg')

function fitted(cssWidth: number): number {
  return Math.min(
    Math.ceil(cssWidth * Math.max(devicePixelRatio, 1)),
    MAX_DIMENSION
  )
}

Or let the components do it ​

<uc-img> adaptive delivery generates srcset automatically from layout, including lazy loading and placeholders. Reach for this library when you need URLs outside an <img> tag: Open Graph images, emails, canvas, server-rendered markup.