concept

Asset variant resolution (light/dark, 1x/2x/3x)

Images, videos, and hero backgrounds all store their pixel-level content the same way: not as one URL, but as a flat array of {url, traits} variants — one entry per combination of appearance (light/dark) and, for raster images, pixel density (1x/2x/3x). A shared JS-side mixin does the bucketing for every consumer. Fullest single-kind treatment prior to this page: reference-image.html, which walks the ImageAsset.vue path end to end; reference-video.html covers the video-specific half (light/dark only, no density).

1. How swift-docc-render consumes this

Every asset reference kind (ImageRenderReference, VideoRenderReference, and the background art on a hero section) stores its variants as one flat array, each entry a plain {url, traits, svgID?} object with no nesting by appearance or density — the grouping happens entirely in JS at render time, not on the wire. The shared logic lives in src/mixins/imageAsset.js, built on two small utilities in src/utils/assets.js:

function separateVariantsByAppearance(variants) {
  const dark = [];
  const rest = [];
  variants.forEach((variant) => {
    if (variant.traits.includes('dark')) {
      dark.push(variant);
    } else {
      rest.push(variant);
    }
  });
  return { light: rest, dark };
}

function extractDensities(variants) {
  return variants.reduce((all, variant) => {
    const density = variant.traits.find(trait => /^[123]x$/.test(trait));
    if (!density) return all;
    return { ...all, [density]: variant.url };
  }, {});
}

(Paraphrased from src/utils/assets.js, lines ~1-40 — separateVariantsByAppearance buckets by the presence of a "dark" trait, treating everything else, including a variant with no appearance trait at all, as "light"; extractDensities then reduces whichever bucket is selected into a {"1x": url, "2x": url, "3x": url} map by matching each variant's traits against a 1x/2x/3x pattern.) The critical, easy-to-miss behavior: extractDensities silently drops any variant whose traits array has no density trait — there is no fallback or warning, the variant simply never appears in the resulting srcset. A hand-authored fixture that forgets to tag a variant "1x" (relying on it being "the only one") will render with an empty srcset for that appearance bucket.

ImageAsset.vue's mixin (mixins/imageAsset.js) combines both bucketing steps into lightDensities/darkDensities computed properties, then joins each density map into a single srcset string ("url 1x, url2 2x, url3 3x") for the <img>/<source> it emits. VideoAsset.vue reuses only the appearance half (separateVariantsByAppearance) since video files have no meaningful "density" concept — it picks one url per appearance bucket and swaps the <video>'s single <source> element by watching prefers-color-scheme/app theme state in JS rather than relying on the unreliable cross-browser support for a media attribute on <source> itself (see reference-video.html §1 for the code comment explaining why).

2. Worked example: one icon, two consumers

Real fixture: the plus.svg icon reference from swift-docc's Tests/SwiftDocCTests/Indexing/RenderIndexTests.swift (lines 803-818, also cited on reference-image.html), extended here with a second, hand-added density variant to show the bucketing behavior on more than one entry:

"plus.svg": {
  "type": "image",
  "identifier": "plus.svg",
  "alt": "A plus icon.",
  "variants": [
    { "url": "/images/plus.svg", "traits": ["1x", "light"], "svgID": "plus-id" },
    { "url": "/images/plus@2x.png", "traits": ["2x", "light"] },
    { "url": "/images/plus-dark.svg", "traits": ["1x", "dark"], "svgID": "plus-id-dark" }
  ]
}

separateVariantsByAppearance splits this into light: [1x, 2x] and dark: [1x]. extractDensities then turns the light bucket into {"1x": "/images/plus.svg", "2x": "/images/plus@2x.png"} and the dark bucket into {"1x": "/images/plus-dark.svg"} — note there is no dark 2x entry, so a high-density display in dark mode falls back to the dark 1x asset (browser-native srcset behavior when a requested density isn't listed, not anything DocC-render does explicitly). ImageAsset.vue's "Auto" template branch (the default) then emits:

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="/images/plus-dark.svg 1x">
  <img srcset="/images/plus.svg 1x, /images/plus@2x.png 2x" alt="A plus icon.">
</picture>

A VideoRenderReference with the same shape of variants array (light/dark only, per reference-video.html's data sheet) would instead produce one <video poster="..."><source src="..."></video>, with the single src swapped by a Vue :key remount when the appearance changes, rather than a native <picture>/<source media> pair — the density half of the mechanism simply doesn't apply.

3. Data sheet

Source: Sources/DocCArchive/Vendored/RenderNode.spec.json. The shared variant shape, RenderReferenceVariant (referenced from both ImageRenderReference.variants and VideoRenderReference.variants), requires a url string and a traits array of RenderReferenceVariantTrait — a five-value enum: "1x", "2x", "3x", "light", "dark" (generated as case _1x = "1x", etc., in Types.swift). Nothing in the schema enforces that a variant carry exactly one density trait and one appearance trait, or indeed any traits at all — a variant with an empty traits array is schema-valid but, per Section 1, invisible to extractDensities (silently excluded from every srcset) while still counting as "light" to separateVariantsByAppearance (since that function only tests for the presence of "dark"). An optional svgID field exists only on the image side, naming an element id inside an SVG sprite sheet to render for that specific variant.

On the swift-docc production side, both ImageReference and VideoReference store their variants internally as a DataAsset — a general-purpose map keyed by DataTraitCollection (a genuinely dictionary-shaped structure on the authoring side) — and only flatten it into the wire's array-of-{url,traits} shape at encode(to:) time. This is a deliberate representation change between the authoring model (a real dictionary, trivially guaranteeing at most one asset per trait combination) and the wire format (a flat array with no such guarantee) — two variants with identical traits are schema-valid on the wire even though the authoring-side DataAsset could never produce that shape.

4. Where else this shows up

Beyond image and video references, the same variant-array shape drives a tutorial project's cover art (node-tutorial.html) and a Tutorials Overview page's hero background (section-hero.html's backgroundImage, which resolves through the same references lookup and ImageAsset pipeline as an inline image). Two pages that mention "variants" in an unrelated sense are worth explicitly excluding here: reference-xcoderequirement.html renders a hardcoded XcodeIcon SVG component rather than resolving any asset variants, and block-codelisting.html's use of the word refers to RenderBlockContent's own discriminated-union arms, not to light/dark/density asset variants.

5. Modern CSS-only alternative

Partial

As on reference-image.html, the "Auto" branch — the default, and what most visitors see — already leans on native, JS-free CSS: a <picture>/<source media="(prefers-color-scheme: dark)"> pair for appearance, and a native srcset/density-descriptor string for density; browsers resolve both without any script running. What JS still does, and what CSS cannot replace, is (a) deciding whether to emit the native construct at all — gated on a three-state, app-level preferredColorScheme preference that overrides the OS setting, which has no CSS selector equivalent — and (b) the density/appearance bucketing algorithm itself (separateVariantsByAppearance/extractDensities), which is a data transformation from the wire's flat array into the strings a native <picture>/srcset actually needs; CSS has no mechanism to filter and regroup an arbitrary JSON array before deciding what to render. Once that transformation has happened, the remaining selection behavior is native and CSS-driven; the transformation step itself is not something CSS can perform.