Files
AndreaSlopFotografie/web/config/heroPreload.ts
SluijsensandClaude Sonnet 5.5 ad0b114828 Hero from the CMS: markers HERO-HTML and HERO-PRELOAD, hero key in the data block, picture per ratio, baked fallback
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>
2026-11-16 16:48:00 +01:00

46 lines
1.6 KiB
TypeScript

import { heroPreloadHtml } from '../src/lib/heroHtml.ts';
// The preload link of the hero in the head of a prerendered page, between the
// markers the CMS host rewrites (<!--CMS:HERO-PRELOAD-->). React cannot write
// a comment into the head, so the build step adds it to the finished HTML,
// from the baked first photo of the page. Pages without a hero stay as they
// are, and so does a page that already has the markers (running twice is fine).
const MARK = '<!--CMS:HERO-PRELOAD-->';
const HERO_IMAGE =
/<img\b[^>]*\bclass="cms-hero-image"[^>]*\bsrc="([^"]+)"|<img\b[^>]*\bsrc="([^"]+)"[^>]*\bclass="cms-hero-image"/;
export function injectHeroPreload(html: string): string {
if (html.includes(MARK)) return html;
const found = HERO_IMAGE.exec(html);
const src = found?.[1] ?? found?.[2];
const end = html.indexOf('</head>');
if (src === undefined || end === -1) return html;
// The src is already escaped in the HTML; the link is built from the same
// text, so it is written back unchanged
const link = heroPreloadHtml({
display: 'single',
title: '',
buttons: [],
slides: [
{
id: 'baked',
alt: '',
decorative: false,
image: {
width: 1,
height: 1,
focus: null,
src: '',
variants: [],
},
},
],
}).replace('href=""', `href="${src}"`);
return (
html.slice(0, end) +
`${MARK}${link}<!--/CMS:HERO-PRELOAD-->` +
html.slice(end)
);
}