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
+102
View File
@@ -0,0 +1,102 @@
import { screen, waitFor } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import heroContent from '@/content/heroContent';
import { renderWithProviders } from '@/test/renderWithProviders';
import type { LoadJson } from '@/components/PackagesProvider/PackagesProvider';
import useHero from './useHero';
const answer = (title: string) => ({
title,
slides: [
{
id: 'a',
alt: 'Alt',
decorative: false,
image: { width: 10, height: 8, src: '/m/a.webp' },
},
],
});
function Title({ load }: { load: LoadJson }) {
return <p data-testid="title">{useHero(heroContent, load).title}</p>;
}
const block = (data: unknown) => {
const script = document.createElement('script');
script.type = 'application/json';
script.id = 'cms-data';
script.textContent = JSON.stringify(data);
document.body.append(script);
};
afterEach(() => {
document.getElementById('cms-data')?.remove();
vi.useRealTimers();
});
describe('useHero', () => {
it('takes the hero of the data block and asks nothing', async () => {
block({ hero: answer('Van de host') });
const load = vi.fn<LoadJson>();
await renderWithProviders(<Title load={load} />);
await waitFor(() =>
expect(screen.getByTestId('title')).toHaveTextContent(
'Van de host',
),
);
expect(load).not.toHaveBeenCalled();
});
it('keeps the baked hero when the block has no usable one', async () => {
block({ hero: { title: 'x' } });
await renderWithProviders(<Title load={vi.fn<LoadJson>()} />);
expect(screen.getByTestId('title')).toHaveTextContent(
heroContent.title,
);
});
it('asks the API without a block and shows a usable answer', async () => {
const load = vi.fn<LoadJson>().mockResolvedValue(answer('Van de API'));
await renderWithProviders(<Title load={load} />);
await waitFor(() =>
expect(screen.getByTestId('title')).toHaveTextContent('Van de API'),
);
expect(load.mock.calls[0][0]).toBe('/api/v1/hero');
});
it('keeps the baked hero on a 204, an error or an unusable answer', async () => {
for (const load of [
vi.fn<LoadJson>().mockResolvedValue(undefined),
vi.fn<LoadJson>().mockRejectedValue(new Error('status 500')),
vi.fn<LoadJson>().mockResolvedValue({ title: 'x' }),
]) {
const { unmount } = await renderWithProviders(
<Title load={load} />,
);
await waitFor(() => expect(load).toHaveBeenCalled());
await Promise.resolve();
expect(screen.getByTestId('title')).toHaveTextContent(
heroContent.title,
);
unmount();
}
});
it('gives up after the time-out', async () => {
vi.useFakeTimers();
const load = vi.fn<LoadJson>(
(_path, signal) =>
new Promise((_resolve, reject) =>
signal.addEventListener('abort', () =>
reject(new Error('aborted')),
),
),
);
await renderWithProviders(<Title load={load} />);
await vi.advanceTimersByTimeAsync(8001);
expect(load.mock.calls[0][1].aborted).toBe(true);
expect(screen.getByTestId('title')).toHaveTextContent(
heroContent.title,
);
});
});