Adds the hero contract: golden files for the picture and the preload links, mapping and seed

Model B (wide 3:2 and tall 4:5 variants, focus point), agreed with the CMS
session. Pure functions mapHero, heroHtml and heroPreloadHtml, golden files in
web/contract/host-injection/hero with a README, and a seed with a first focus
estimate per photo. No site change yet.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 14:44:30 +01:00
co-authored by Claude Sonnet 5.5
parent 2c15cda7c4
commit 13f78c48cd
24 changed files with 793 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
import { escapeHtml } from './cmsHtml.ts';
import type { HeroData, HeroImageVariant, HeroSlide } from './hero.ts';
// The neutral HTML of the hero between the CMS markers, and the preload links
// in the head. The host (SLP Modular CMS) writes the same markup from live
// data; the golden files in contract/host-injection/hero are the contract.
//
// Only the FIRST slide is written (it is the LCP image); the client takes the
// rest from the data block. The picture holds one <source> for portrait
// screens (ratio "tall") and the <img> itself is the landscape one (ratio
// "wide"). Selection is by orientation, because the hero is as high as the
// screen. The preload links repeat exactly the same srcset, sizes and media,
// so the browser starts the very file the picture will pick.
//
// Only WebP is used here: a preload cannot say "AVIF if supported", so a
// second format would be fetched twice. Later slides may use AVIF.
const PORTRAIT = '(orientation: portrait)';
const LANDSCAPE = '(orientation: landscape)';
const SIZES = '100vw';
const srcsetOf = (variants: HeroImageVariant[], ratio: string): string =>
variants
.filter((v) => v.ratio === ratio && v.type === 'image/webp')
.sort((a, b) => a.width - b.width)
.map((v) => `${escapeHtml(v.url)} ${v.width}w`)
.join(', ');
const percent = (unit: number): string => `${Number((unit * 100).toFixed(2))}%`;
const focusStyle = (slide: HeroSlide): string => {
const { focus } = slide.image;
return focus
? ` style="object-position:${percent(focus.x)} ${percent(focus.y)}"`
: '';
};
function pictureHtml(slide: HeroSlide): string {
const { image } = slide;
const tall = srcsetOf(image.variants, 'tall');
const wide = srcsetOf(image.variants, 'wide');
const source = tall
? `<source media="${PORTRAIT}" srcset="${tall}" sizes="${SIZES}" type="image/webp">`
: '';
const img =
`<img class="cms-hero-image" src="${escapeHtml(image.src)}"` +
(wide ? ` srcset="${wide}" sizes="${SIZES}"` : '') +
` width="${image.width}" height="${image.height}"` +
` alt="${escapeHtml(slide.alt)}"${focusStyle(slide)}` +
' fetchpriority="high">';
return `<picture class="cms-hero-picture">${source}${img}</picture>`;
}
function headingHtml(hero: HeroData): string {
const tagline = hero.tagline
? `<span class="cms-hero-tagline">${escapeHtml(hero.tagline)}</span> `
: '';
// A new line in the title is a line break
const name = escapeHtml(hero.title).replace(/\r?\n/g, '<br>');
return `<h1 class="cms-hero-title">${tagline}<span class="cms-hero-name">${name}</span></h1>`;
}
function buttonsHtml(hero: HeroData): string {
if (hero.buttons.length === 0) return '';
const links = hero.buttons
.map(
(button) =>
`<a class="cms-hero-button" href="${escapeHtml(button.target)}">${escapeHtml(button.label)}</a>`,
)
.join('');
return `<p class="cms-hero-buttons">${links}</p>`;
}
// Between <!--CMS:HERO-HTML--> and <!--/CMS:HERO-HTML-->; nothing for no hero
export function heroHtml(hero: HeroData | null): string {
if (!hero) return '';
return pictureHtml(hero.slides[0]) + headingHtml(hero) + buttonsHtml(hero);
}
// Between <!--CMS:HERO-PRELOAD--> and <!--/CMS:HERO-PRELOAD--> in the head: one
// link for portrait screens (when a tall variant exists) and one for the
// others. Without a tall variant a single link without media is enough.
export function heroPreloadHtml(hero: HeroData | null): string {
if (!hero) return '';
const { image } = hero.slides[0];
const tall = srcsetOf(image.variants, 'tall');
const wide = srcsetOf(image.variants, 'wide');
const link = (target: string, media: string): string =>
`<link rel="preload" as="image" ${target}` +
`${media ? ` media="${media}"` : ''} fetchpriority="high">`;
const wideTarget = wide
? `imagesrcset="${wide}" imagesizes="${SIZES}"`
: `href="${escapeHtml(image.src)}"`;
if (!tall) return link(wideTarget, '');
return (
link(`imagesrcset="${tall}" imagesizes="${SIZES}"`, PORTRAIT) +
link(wideTarget, LANDSCAPE)
);
}