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>
This commit is contained in:
2026-11-16 16:48:00 +01:00
co-authored by Claude Sonnet 5.5
parent 13f78c48cd
commit ad0b114828
31 changed files with 863 additions and 134 deletions
+2 -1
View File
@@ -28,6 +28,7 @@ import {
externalizeInlineScripts,
findExecutableInlineScripts,
} from './inlineScripts.ts';
import { injectHeroPreload } from './heroPreload.ts';
import { NOT_FOUND_FILE, pageFile } from './pageFiles.ts';
export type ViolationCode =
@@ -179,7 +180,7 @@ export function planExternalization(
let result;
try {
result = scripts.externalize(html);
result = scripts.externalize(injectHeroPreload(html));
} catch (error) {
if (!(error instanceof ScriptError)) throw error;
add(file, error.code, detailOf(error));
+36
View File
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';
import { injectHeroPreload } from './heroPreload.ts';
const page = (body: string, head = '<title>t</title>') =>
`<html><head>${head}</head><body>${body}</body></html>`;
describe('injectHeroPreload', () => {
it('writes the preload of the first photo between markers before </head>', () => {
const out = injectHeroPreload(
page(
'<img class="cms-hero-image" src="/assets/hero-8-abc.jpg" width="1" height="1" alt="">',
),
);
expect(out).toContain(
'<title>t</title><!--CMS:HERO-PRELOAD--><link rel="preload" as="image" href="/assets/hero-8-abc.jpg" fetchpriority="high"><!--/CMS:HERO-PRELOAD--></head>',
);
});
it('finds the image when src comes before class', () => {
const out = injectHeroPreload(
page('<img src="/a.jpg" alt="" class="cms-hero-image">'),
);
expect(out).toContain('href="/a.jpg"');
});
it('leaves a page without a hero, without a head and a done page alone', () => {
const plain = page('<img src="/a.jpg" alt="">');
expect(injectHeroPreload(plain)).toBe(plain);
const noHead = '<img class="cms-hero-image" src="/a.jpg">';
expect(injectHeroPreload(noHead)).toBe(noHead);
const once = injectHeroPreload(
page('<img class="cms-hero-image" src="/a.jpg" alt="">'),
);
expect(injectHeroPreload(once)).toBe(once);
});
});
+45
View File
@@ -0,0 +1,45 @@
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)
);
}
+39
View File
@@ -54,6 +54,45 @@ describe('injectCms regions', () => {
});
});
describe('injectCms hero', () => {
const hero = {
title: 'Titel',
slides: [
{
id: 'a',
alt: 'Alt',
decorative: false,
image: { width: 10, height: 8, src: '/m/a.webp' },
},
],
};
const regions =
'<!--CMS:HERO-PRELOAD-->old<!--/CMS:HERO-PRELOAD--><!--CMS:HERO-HTML-->old<!--/CMS:HERO-HTML-->';
it('writes the hero and its preload and puts the hero in the data block', () => {
const out = run(
page(
regions +
'<!--CMS:DATA--><script type="application/json" id="cms-data">{}</script>',
),
{ ...live([]), hero },
);
expect(out).toContain('<h1 class="cms-hero-title">');
expect(out).toContain(
'<link rel="preload" as="image" href="/m/a.webp"',
);
expect(out).not.toContain('old');
expect(out).toContain('"hero":{"title":"Titel"');
});
it('keeps the baked hero when there is no usable one', () => {
for (const data of [live([]), { ...live([]), hero: { title: 'x' } }]) {
const out = run(page(regions), data);
expect(out).toBe(page(regions));
}
});
});
describe('injectCms data block', () => {
const baked =
'<!--CMS:DATA--><script type="application/json" id="cms-data">{"source":"build"}</script>';
+14 -5
View File
@@ -9,6 +9,8 @@
// matches that code.
import { bannerHtml, offeringsHtml } from '../src/lib/cmsHtml.ts';
import { scriptJson } from '../src/lib/cmsData.ts';
import { mapHero } from '../src/lib/hero.ts';
import { heroHtml, heroPreloadHtml } from '../src/lib/heroHtml.ts';
import { formatPrice } from '../src/lib/formatPrice.ts';
import { mapBanner, mapOfferings, mapTopBanner } from '../src/lib/offerings.ts';
@@ -16,6 +18,8 @@ export interface LiveData {
// The answers as the public API gives them
offerings: unknown;
banners: { top: unknown; bottom: unknown };
// The answer of GET /api/v1/hero; absent or unusable leaves the baked hero
hero?: unknown;
}
export interface InjectOptions {
@@ -33,7 +37,7 @@ const jsonLd = (value: unknown) =>
.replace(/&/g, '\\u0026');
const REGION =
/<!--CMS:(OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML)-->[\s\S]*?<!--\/CMS:\1-->/g;
/<!--CMS:(OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML|HERO-HTML|HERO-PRELOAD)-->[\s\S]*?<!--\/CMS:\1-->/g;
const DATA_BLOCK =
/<!--CMS:DATA-->(?:<script type="application\/json" id="cms-data">[\s\S]*?<\/script>)?/g;
const OFFER_JSON_LD =
@@ -64,19 +68,23 @@ export function injectCms(html: string, options: InjectOptions): string {
const { data } = options;
const mapped = mapOfferings(data.offerings);
const packages = mapped ? mapped.packages : [];
const hero = mapHero(data.hero);
const prices: Price = new Map(packages.map((pkg) => [pkg.id, pkg.price]));
const regions: Record<string, string> = {
'OFFERINGS-HTML': offeringsHtml(packages),
'BANNER-TOP-HTML': bannerHtml(mapTopBanner(data.banners.top)),
'BANNER-BOTTOM-HTML': bannerHtml(mapBanner(data.banners.bottom)),
'HERO-HTML': heroHtml(hero),
'HERO-PRELOAD': heroPreloadHtml(hero),
};
const siteUrl = options.publicBaseUrl ?? options.authoredBaseUrl;
let out = html
.replace(
REGION,
(_match, name: string) =>
`<!--CMS:${name}-->${regions[name]}<!--/CMS:${name}-->`,
.replace(REGION, (match, name: string) =>
// No usable hero: the baked one stays (fail open)
name.startsWith('HERO') && !hero
? match
: `<!--CMS:${name}-->${regions[name]}<!--/CMS:${name}-->`,
)
.replace(
DATA_BLOCK,
@@ -86,6 +94,7 @@ export function injectCms(html: string, options: InjectOptions): string {
...(siteUrl ? { siteUrl } : {}),
offerings: data.offerings,
banners: data.banners,
...(data.hero === undefined ? {} : { hero: data.hero }),
})}</script>`,
)
.replace(OFFER_JSON_LD, (match, slug: string, json: string) => {
+1 -1
View File
@@ -11,7 +11,7 @@
import { readFileSync, writeFileSync } from 'node:fs';
const REGION =
/(<!--CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML)-->)[\s\S]*?(<!--\/CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML)-->)/g;
/(<!--CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML|HERO-HTML|HERO-PRELOAD)-->)[\s\S]*?(<!--\/CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML|HERO-HTML|HERO-PRELOAD)-->)/g;
const DATA_BLOCK =
/(?:<!--CMS:DATA-->)?<script type="application\/json" id="cms-data">[\s\S]*?<\/script>|<!--CMS:DATA-->/g;
const OFFER_MARKER = /<!--CMS:OFFER-JSON-LD slug="[a-z0-9-]+"-->/g;
+1
View File
@@ -672,6 +672,7 @@ describe('startPreviewServer', () => {
// Live pakketten: geen gegevens, zonder 404
expect((await ask(port, '/api/v1/offerings')).status).toBe(204);
expect((await ask(port, '/api/v1/offerings/banner')).status).toBe(204);
expect((await ask(port, '/api/v1/hero')).status).toBe(204);
// Al het andere blijft zoals het was
expect((await ask(port, '/api/v1/andere')).status).toBe(404);
expect((await ask(port, '/api/v1/contact-messages')).status).toBe(404);
+3 -1
View File
@@ -227,7 +227,9 @@ export function contactStubResponse(
// asks once per page load)
if (
method === 'GET' &&
(path === '/api/v1/offerings' || path === '/api/v1/offerings/banner')
(path === '/api/v1/offerings' ||
path === '/api/v1/offerings/banner' ||
path === '/api/v1/hero')
) {
return { status: 204, body: '' };
}