The prerendered hero is plain HTML between the markers (first photo, h1, buttons), the slider takes over after hydration with the hero of the host block, the API or the build. The head preload is added to the finished HTML. Hero texts move into nl.json. The stand-in host and the e2e tests cover the hero. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
105 lines
4.3 KiB
TypeScript
105 lines
4.3 KiB
TypeScript
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';
|
|
|
|
// The srcset of one ratio and format ("tall" or "wide"; WebP unless said)
|
|
export const variantSrcset = (
|
|
variants: HeroImageVariant[],
|
|
ratio: string,
|
|
type = 'image/webp',
|
|
): string =>
|
|
variants
|
|
.filter((v) => v.ratio === ratio && v.type === type)
|
|
.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 = variantSrcset(image.variants, 'tall');
|
|
const wide = variantSrcset(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 = variantSrcset(image.variants, 'tall');
|
|
const wide = variantSrcset(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)
|
|
);
|
|
}
|