Skip to content

responsive-images

Terminal window
npx skills add https://github.com/crystallizeapi/ai --skill responsive-images

Responsive Images with Crystallize

Crystallize pre-generates every image at a ladder of widths in up to three formats and serves them from its own CDN at media.crystallize.com. The optimization has already happened. Your job on the frontend is to pick the smallest file that still looks right — not to re-encode anything.

Almost all wasted image bandwidth on a Crystallize storefront comes from two mistakes, both covered below: shipping AVIF because it is newer, and omitting sizes.

Measured on 2026-09-15 against two unrelated production tenants (347 format comparisons, 80 images), and read from @crystallize/reactjs-components v5.0.0. The width ladder and the AVIF/WebP result are observations about today’s transcoder output, not contractual guarantees — re-measure before betting something load-bearing on them.

Rule 0: do not put another image optimizer in front of the CDN

Do not serve Crystallize images through next/image, or any other framework image pipeline, in its default configuration. It is the single most expensive mistake available here.

The variants already exist as static files on Crystallize’s CDN. Routing them through a hosting platform’s image optimizer:

  • produces no smaller file — it re-encodes an already-encoded derivative
  • adds a proxy hop, and a transform on the first request for every unseen size
  • bills you for image-optimization units and platform egress for bytes the Crystallize CDN already serves

Use a plain <picture> / <img> pointing straight at media.crystallize.com. That is what the component below emits.

If a project is already committed to next/image, pass unoptimized, or give it a custom loader that snaps the requested width to one in that image’s variants and returns that variant’s url, because any other @{width} has no file behind it — but at that point it is doing strictly less than the Crystallize Image component, with more configuration.

What the API actually returns

Each image carries a list of variants. Verified shape:

https://media.crystallize.com/{tenant}/{path}/@{width}/{filename}.{ext}
Widths drawn from 100, 200, 500, 768, 1024, 1366, 1600, 1920, 3200
Formats avif and webp always, plus original if jpeg or png
Fields url, key, width, height, size (bytes)

Read URLs from the API, never compose them from scratch. On a tenant with a custom media domain, Catalogue and Discovery return that host and drop the {tenant} segment. What you may do is derive a rendition from a URL the API returned for that same image, on the same host: replace @{width} with another width the API listed. {dir}/@{w}/{name}.webp held for all 48,519 renditions on one tenant, so a listing can ask for variants(types: "webp", minWidth, maxWidth) { width } only and build the URLs from the first one; that cut a 16-card listing read from 345 to 108 KB. Only use widths that came back.

Original-format variants exist only for JPEG and PNG sources, and a .jpg source gets .jpeg variants. A WebP, AVIF or TIFF source gets webp and avif only.

The width ladder is capped at the source image’s own width, so it differs per image. A 1200px original yields 100/200/500/768/1024; a 500px original yields only 100/200/500. A source under 100px wide gets no variants at all. In a sample of 80 images, eight distinct width sets appeared.

Never hardcode the ladder. Build srcset from the variants the API returned for that image, or you will emit URLs that 404 and a browser that picks them will show nothing.

variants can be empty, so keep url as the fallback. They are generated after upload: until then Catalogue returns null and Discovery []. Discovery captures them when the item is indexed and does not pick them up by itself, so an item published too early keeps [] until it is published again. See [[media]].

Query the fields you need — including size

firstImage {
url
altText
width
height
variants {
url
width
height
size
}
}

Always select size. It is not decoration: the React component uses it to decide whether AVIF is worth emitting (see below). Omit it and the component silently ships both formats, which on measured data is the more expensive outcome.

After an image is replaced, Catalogue’s image-level width and height still describe the first upload, while url and variants are the new file’s. If a replacement can change the aspect ratio, drop them from a Catalogue selection: the component then uses the largest variant.

On Discovery you can also trim the payload server-side — variants takes arguments:

variants(types: "webp", minWidth: 400, maxWidth: 1600) {
url
width
size
}

Useful for a listing page that will never render a 3200px hero. Ask per context — cards up to 1024, a gallery or hero up to 1920, chat or MCP cards up to 500 — and one image drops from about 27 rendition URLs to about 4. On a grid this removed every AVIF and original-format rendition from the payload. One build saw maxWidth ignored, so check the answer on your tenant. width picks one exact width. types matches the end of the variant key, so ask for "jpeg", never "jpg". An image with nothing in range returns [], not a smaller variant, so keep url selected. Catalogue’s variants takes no arguments — filter client-side there.

Watch for repeated images. variants { images } on a product with many sizes repeats the colourway’s images on every size variant: 24 cards of a 260-size style came to 8.2 MB and broke the hosting cache (“Single item size exceeds maxSize”), against 106 KB with defaultVariant only and card-sized WebP. Discovery’s variantGroups { images } takes no arguments and returns every group’s images (355 images for 16 hits). Ask for the default variant’s images on a card and load the gallery on the product page. The query side is in [[query]].

Format: measure, do not assume AVIF

Conventional advice says prefer AVIF. On Crystallize’s current transcoder settings that is usually wrong.

Comparisons (2 tenants, same width, both formats present) 347
WebP produced the smaller file 343
AVIF produced the smaller file 4
Average advantage to WebP ~25–30%

AVIF generally wins at equal visual quality; what matters for payload is file size at the quality the encoder actually targeted, and here WebP wins nearly every time. On one tenant AVIF averaged larger than the original JPEG.

So: ship one modern format, not both. Two <source> elements do not make the page faster — the browser takes the first it supports, and if that is the larger AVIF you have paid for the privilege. Keep the original-format source as the compatibility fallback.

The component already does this for you, if you queried size.

Use the component

@crystallize/reactjs-components v5 implements the above. Spread the API image object into it:

import { Image } from "@crystallize/reactjs-components";
<Image {...image} sizes="(max-width: 600px) 90vw, 700px" className="product-image" loading="lazy" />;

What it does for you:

  • builds a srcset per format from variants, with w descriptors
  • emits <source type="image/avif"> only when the first AVIF variant is smaller than the first WebP variant — the comparison needs size, and falls back to emitting both when it is missing
  • keeps a <source> for the original format and a plain src on the <img> as the fallback
  • sets width/height from the props you pass (the spread image’s own, when selected), else from the largest variant, which is what prevents layout shift
  • resolves alt as alt → altText (from the API) → fallbackAlt → ""

What it does not do, and you must:

  • pass sizes — it has no default, see below
  • pass loading and fetchPriority — they forward to the <img>, but nothing is set automatically. fetchPriority is missing from ImageProps, so as a JSX attribute it fails type-checking. In TypeScript, spread it instead: {...{ fetchPriority: "high" }}

Surprises worth knowing before you style it:

  • It always renders <figure> wrapping <picture>, plus a <figcaption> even when empty. Budget for that in CSS, or use the render-prop escape hatch.
  • className lands on the <figure>, not the <img>: target .product-image img. style and any prop the component does not know land on the <img>, so keep focalPoint, meta and the like out of the object you spread.
  • caption needs the Catalogue shape, and only caption { json } renders as rich text: html prints its tags as text. Discovery’s caption is a scalar, so the <figcaption> stays empty unless you pass the text as captionPassed.
  • _availableSizes / _availableFormats synthesize URLs from the @{width} scheme instead of using variants. Because the ladder is per-image, this can invent URLs that do not exist. Prefer passing real variants.

For full markup control, pass a function as children and receive { srcSet, srcSetWebp, srcSetAvif, useAvif, useWebP, className, sizes, media, src, alt, width, height, originalFileExtension }, plus any other prop you passed.

Not using React? See references/without-react.md.

sizes is the whole ballgame

This is the most common and most expensive mistake on the list.

srcset w-descriptors tell the browser how wide each file is. They say nothing about how big the image will be on the page. sizes is what tells it that — and the browser must choose a file before it has laid the page out, so it cannot work this out on its own.

Omit sizes and the browser assumes 100vw — full viewport width. A 180px thumbnail on a 1440px-wide laptop then downloads the 1024px file. Every time, on every thumbnail.

Write sizes to mirror the CSS that will actually size the element, gutters and column counts included:

<!-- Four-column grid at desktop, two at tablet, one at mobile -->
sizes="(max-width: 600px) calc(100vw - 32px), (max-width: 1024px) calc(50vw - 24px), calc(25vw - 32px)"
<!-- Full-bleed hero -->
sizes="100vw"
<!-- Fixed-width thumbnail — no media query needed -->
sizes="180px"

Checking it: open DevTools, find the <img>, compare currentSrc against the element’s rendered width times the device pixel ratio. If currentSrc is much wider, sizes is lying.

When sizes and the CSS disagree, the CSS wins visually and sizes wins on bandwidth — so a wrong sizes costs you bytes with nothing to show for it.

LCP and CLS

  • The hero is the LCP element. Give it loading="eager" and fetchPriority="high", and never loading="lazy" — lazy-loading the LCP image reliably makes the score worse.
  • Everything below the fold gets loading="lazy".
  • Always render width and height so the browser can reserve the box. The component does this from the props you pass, else the largest variant; if you hand-roll, do it yourself. This is the entire fix for image-driven layout shift.
  • Do not lazy-load images already in the viewport on first paint — the request starts later than it needs to.
  • At most one or two fetchpriority="high" per page. Marking every card high is the same as marking none. Give images in a closed mega menu or drawer fetchpriority="low" (and loading="lazy").
  • Get the LCP image into the first HTML. Preload it, or render it in the static shell: in a Next.js partial prerender, a cached listing preview as the <Suspense> fallback put the LCP image into the shell, which was the biggest LCP win on listings in one build.
  • Preconnect to the media host without crossOrigin: <link rel="preconnect" href="https://media.crystallize.com">. With crossorigin the browser opens a CORS connection that <img> requests do not reuse. Use the custom media domain if the tenant has one.
  • Placeholders are yours to make. Discovery serves no dominant colour or blur hash (image meta holds only import provenance), so generate one at import and store it with the item if you want one.

Share and structured-data images

og:image, twitter:image, schema.org image in JSON-LD and merchant feeds are read by crawlers and link previewers, not by a browser that negotiates formats:

  • Use the original format, never AVIF. Many preview bots and feed readers do not decode AVIF, and some not WebP. Pick the widest original-format variant (JPEG or PNG), else the widest WebP; skip AVIF. A .jpg source’s variants are .jpeg.
  • At least 1200 px wide when the source allows. The capped ladder means a 1200 px source tops out at 1024, so use the image’s url then: the original upload, with the image’s own width and height, provided it is not itself AVIF.
  • Absolute URLs, with dimensions. Give og:image its og:image:width and og:image:height, from the variant you picked.
  • The packshot goes in Product JSON-LD and feeds. Use the white-background product shot, the first image of the default variant if that is how the catalogue is authored; lifestyle shots belong on the page, not in the structured data. A manufacturer photo padded with white bars (3:2 letterboxed to 4:3 or square) reads as a small product in previews: crop it and import it as a new image, as in [[media]].
  • Generated share cards (a Next opengraph-image.tsx with packshot, name and key specs) fetch the image at render time, so allow the media host in the image fetch and in any CSP.
/** og:image, twitter:image and JSON-LD images: original format first, then WebP, never AVIF, 1200 px or wider. */
const isAvif = (url: string) => /\.avif(\?|$)/i.test(url);
const formatRank = (url: string) => (isAvif(url) ? 2 : /\.webp(\?|$)/i.test(url) ? 1 : 0);
type ShareImage = { url: string; width?: number; height?: number };
export function shareImage(image?: {
url: string;
width?: number | null;
height?: number | null;
variants?: { url: string; width: number; height: number }[] | null;
} | null): ShareImage | undefined {
if (!image) return undefined;
const best = (image.variants ?? [])
.filter((v) => !isAvif(v.url))
.sort((a, b) => formatRank(a.url) - formatRank(b.url) || b.width - a.width)[0];
if (best && best.width >= 1200) return best;
// The ladder stops below the source width, so the original upload is the larger file.
if (!isAvif(image.url)) return { url: image.url, width: image.width ?? undefined, height: image.height ?? undefined };
return best; // an AVIF upload: the widest WebP rendition is all there is
}

Where these tags and the JSON-LD are emitted, and how they are validated, is in [[seo-geo]].

Failure modes

Symptom Cause Fix
Thumbnails download 1024px files sizes missing — browser assumed 100vw Write sizes to match the rendered width
Both AVIF and WebP <source> emitted size not selected in the GraphQL query Add size to variants
Images heavier after “upgrading” to AVIF AVIF is usually larger here Ship WebP; keep the original as fallback
Some srcset URLs 404 Width ladder hardcoded, or _availableSizes used Build srcset from the returned variants
variants is null or [] Variants not generated yet, or source under 100px Use url; republish once they exist
Page jumps as images load No width/height on the <img> Render both; they set the aspect ratio
Poor LCP on the product page Hero is loading="lazy" eager + fetchPriority="high"
Image bill higher than expected Images routed through a framework optimizer Serve straight from media.crystallize.com
alt is empty everywhere altText not selected, or not authored Query altText; author it in the PIM
Link previews show no image og:image points at an AVIF rendition Original format or WebP, never AVIF
A listing read is hundreds of KB Every rendition, or variants { images }, queried variants(types:, maxWidth:); default variant images only
<img> opens a second connection preconnect carries crossorigin Preconnect without crossOrigin

[[query]] covers the Catalogue and Discovery queries these fields come from, and [[content-model]] covers the Images component itself — including that product variant images are built-in rather than components. [[media]] covers importing images and setting altText, [[seo-geo]] the share and structured-data tags, and [[storefront]] where image reads sit in the request path.


Reference Details

Responsive Crystallize Images Without React

The @crystallize/reactjs-components Image component is React-only. Everywhere else — Svelte, Vue, Astro, Remix loaders emitting plain HTML, email templates, server-rendered templates — build the same markup by hand. This page is the fallback, not the recommendation: if you are in React, use the component.

What you are reproducing

<picture>
<source type="image/webp" srcset="…400w, …800w, …1200w" sizes="…" />
<source type="image/jpeg" srcset="…400w, …800w, …1200w" sizes="…" />
<img src="…fallback.jpeg" alt="…" width="1200" height="800" loading="lazy" />
</picture>

Four things make it correct:

  1. One <source> per format, modern format first — the browser takes the first it supports.
  2. srcset entries carry w descriptors matching the file’s real pixel width.
  3. sizes is present on every <source> and describes the rendered width, not the file width.
  4. <img> carries src (fallback), alt, width, height.

Building it from the API response

type Variant = { url: string; width: number; height?: number; size?: number };
const extOf = (url: string) => url.split(".").pop()!.toLowerCase();
const srcsetFor = (variants: Variant[], ext: string) =>
variants
.filter((v) => extOf(v.url) === ext)
.sort((a, b) => a.width - b.width)
.map((v) => `${v.url} ${v.width}w`)
.join(", ");

Build from the variants the API returned. The width ladder is capped at the source image’s own width, so it differs per image — a hardcoded list produces URLs that 404.

Choosing the modern format

Ship one modern format. Two <source> elements do not make the page faster; the browser takes the first it supports, and on Crystallize’s current transcoder output that is usually the larger file.

/** Pick the smaller of webp/avif at a common width; default to webp. */
function pickModernFormat(variants: Variant[]): "webp" | "avif" {
const at = (ext: string) =>
variants.filter((v) => extOf(v.url) === ext && v.size).sort((a, b) => a.width - b.width);
const [webp] = at("webp");
const [avif] = at("avif");
if (!webp?.size || !avif?.size) return "webp";
return avif.size < webp.size ? "avif" : "webp";
}

This mirrors what the React component does. It needs size in the query — without it, default to webp, which wins the large majority of the time.

Original-format variants exist only for jpeg and png sources, and a .jpg source gets .jpeg variants. Derive the format rather than assuming:

const originalExt = (image: { url: string }) => {
const ext = extOf(image.url);
return ext === "jpg" ? "jpeg" : ext;
};

Putting it together

const escapeAttr = (value: string) =>
value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
function pictureHtml(
image: { url: string; altText?: string; width?: number; height?: number; variants?: Variant[] | null },
sizes: string,
{ priority = false }: { priority?: boolean } = {}, // true for the LCP image only
) {
const variants = image.variants ?? [];
const modern = pickModernFormat(variants);
const original = originalExt(image);
const biggest = [...variants].sort((a, b) => b.width - a.width)[0];
const sources = [...new Set([modern, original])]
.map((ext) => ({ ext, srcset: srcsetFor(variants, ext) }))
.filter(({ srcset }) => srcset)
.map(({ ext, srcset }) => `<source type="image/${ext}" srcset="${srcset}" sizes="${sizes}">`)
.join("\n ");
return `
<picture>
${sources}
<img src="${image.url}" alt="${escapeAttr(image.altText ?? "")}"
width="${biggest?.width ?? image.width ?? ""}" height="${biggest?.height ?? image.height ?? ""}"
${priority ? 'loading="eager" fetchpriority="high"' : 'loading="lazy"'}>
</picture>`;
}

variants is null on Catalogue and [] on Discovery until they are generated, and for a source under 100px wide. A WebP, AVIF or TIFF source has no original-format variants. In those cases there is no <source> to emit, and <img src> carries the image.

Escape altText for the target context — it is author-entered content, not a literal.

Art direction: a different crop per breakpoint

sizes picks a size. When you need a different image — a tall portrait crop on mobile, a wide one on desktop — that is media, and it takes separate <source> elements:

<picture>
<source media="(max-width: 600px)" type="image/webp" srcset="…portrait srcset…" sizes="100vw" />
<source type="image/webp" srcset="…landscape srcset…" sizes="(max-width: 1200px) 100vw, 1200px" />
<img src="…" alt="…" width="1600" height="900" />
</picture>

Two different crops are two different images in the PIM. Do not try to fake art direction with object-fit alone — see focal point below for the case where one image is genuinely enough.

Focal point

focalPoint ({ x, y }, normalized 0–1) is authored in the PIM and tells you where the subject is. It is what makes a single image survive being cropped to different aspect ratios. Select focalPoint { x y } on Catalogue. On Discovery it is a scalar: select plain focalPoint, and an image without one returns {}, not null, so test x, not the object:

.card-image {
aspect-ratio: 4 / 3;
object-fit: cover;
object-position: var(--focal-x, 50%) var(--focal-y, 50%);
}
const focal = image.focalPoint;
const style = focal?.x != null ? `--focal-x:${focal.x * 100}%;--focal-y:${focal.y * 100}%` : "";

Without it, object-fit: cover crops from the centre and decapitates people in portrait shots.

Don’t

  • Don’t route these URLs through a framework image optimizer. The variants are already static files on the CDN; re-optimizing yields no smaller file and bills you for the transform and the egress. See Rule 0 in SKILL.md.
  • Don’t omit sizes. The browser then assumes 100vw and downloads the largest candidate for a 200px thumbnail.
  • Don’t put sizes only on the <img>. Each <source> needs it too; <img>-only leaves the <source> elements defaulting to 100vw.
  • Don’t lazy-load the LCP image. The hero gets loading="eager" and fetchpriority="high".
  • Don’t hardcode the width ladder. It is capped per image at the source width.


Crystallize AI