Makes the first render match the prerendered HTML

Search values, the 404 buttons and the footer year now apply after hydration.
The e-mail address stays out of the HTML behind a placeholder, and all review
texts are in the page, so crawlers can read them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 18:17:26 +01:00
co-authored by Claude Opus 5.5
parent 1574726c1d
commit 2c2e27e337
32 changed files with 1322 additions and 148 deletions
@@ -5,6 +5,7 @@ import packages from '@/content/packages';
import siteContent from '@/content/siteContent';
import { shootOptions } from '@/lib/shootOptions';
import { axeResults } from '@/test/axe';
import { serverHtml } from '@/test/hydrate';
import { renderWithProviders } from '@/test/renderWithProviders';
import ContactForm from './ContactForm';
@@ -234,3 +235,21 @@ describe('ContactForm', () => {
expect(container.querySelector('[data-robot-check]')).not.toBeNull();
});
});
describe('ContactForm in the prerendered HTML', () => {
it('does not hold the address (BR-MAIL-06)', async () => {
const html = await serverHtml(
<ContactForm
idPrefix="test"
options={options}
mailSubject="Vraag via andreaslop.nl"
email={siteContent.email}
replyTime="1-2 werkdagen"
submit={vi.fn()}
/>,
);
expect(html).toContain('contact-form-submit');
expect(html.toLowerCase()).not.toContain('info@andreaslop.nl');
expect(html).not.toContain('mailto:');
});
});
@@ -26,7 +26,7 @@ import {
INITIAL_CONTACT_STATE,
type ContactFormEvent,
} from '@/lib/contactFormReducer';
import { emailAddress } from '@/lib/emailAddress';
import useEmailAddress from '@/hooks/useEmailAddress';
import { mailtoLink } from '@/lib/mailLink';
import { toContactPayload } from '@/lib/toContactPayload';
import { CONTACT_FIELDS } from '@/lib/validateContact';
@@ -92,7 +92,7 @@ export default function ContactForm({
if (state.status === 'sent') sentMessage.current?.focus();
}, [state.status]);
const address = emailAddress(email);
const { address } = useEmailAddress(email);
const disabled = state.status === 'sending';
if (state.status === 'sent') {
@@ -2,6 +2,8 @@ import { screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import siteContent from '@/content/siteContent';
import { axeResults } from '@/test/axe';
import { cssRulesFor } from '@/test/cssRules';
import { hydratePage, serverHtml } from '@/test/hydrate';
import { renderWithProviders } from '@/test/renderWithProviders';
import ContactFormGate from './ContactFormGate';
@@ -71,3 +73,77 @@ describe('ContactFormGate', () => {
expect(await axeResults(container)).toHaveNoViolations();
});
});
// Before hydrating the notice has the sentence only and reserves the room of
// the address and the button (BR-MAIL-05)
describe('ContactFormGate in the prerendered HTML', () => {
const gate = (email = siteContent.email) => (
<ContactFormGate
enabled={false}
email={email}
subject="Aanvraag fotoshoot: Newborn"
>
<form aria-label="Formulier" />
</ContactFormGate>
);
const prerendered = async (email?: typeof siteContent.email) => {
const html = await serverHtml(gate(email));
const doc = new DOMParser().parseFromString(html, 'text/html');
return { html, doc };
};
it('holds the sentence only, hidden placeholders and no address', async () => {
const { html, doc } = await prerendered();
expect(html.toLowerCase()).not.toContain('info@andreaslop.nl');
expect(html).not.toContain('mailto:');
expect(html).not.toContain('Mail me voor nu op');
expect(html).not.toContain('data-testid="contact-gate-mail"');
expect(
doc.querySelector('[data-testid="contact-gate-notice"]')!
.textContent,
).toContain('Het contactformulier komt binnenkort.');
const pending = doc.querySelector(
'[data-testid="contact-gate-mail-pending"]',
)!;
expect(pending.getAttribute('aria-hidden')).toBe('true');
expect(pending.hasAttribute('data-email-pending')).toBe(true);
expect(pending.getAttribute('href')).toBeNull();
});
it('reserves the width of the lead and the address in characters', async () => {
const { doc } = await prerendered();
const reserved = doc.querySelector('p [data-email-pending]')!;
// Hydrating makes React generate the same classes, which puts their
// rules in the document
await hydratePage(gate());
const probe = document.createElement('span');
probe.className = reserved.className;
// ' Mail me voor nu op ' (20) + 'info@andreaslop.nl' (18)
expect(cssRulesFor(probe).join(' ')).toContain('width: 38ch');
});
it('shows the address, link and button after hydrating', async () => {
const { errors } = await hydratePage(gate());
const mail = await screen.findByTestId('contact-gate-mail');
expect(mail).toHaveAttribute(
'href',
'mailto:info@andreaslop.nl?subject=Aanvraag%20fotoshoot%3A%20Newborn',
);
expect(screen.getByTestId('contact-gate-notice')).toHaveTextContent(
'Het contactformulier komt binnenkort. Mail me voor nu op info@andreaslop.nl',
);
expect(
screen.queryByTestId('contact-gate-mail-pending'),
).not.toBeInTheDocument();
expect(errors).toEqual([]);
});
it('has no placeholders without a valid address', async () => {
const { html } = await prerendered({
user: '',
domain: 'andreaslop.nl',
});
expect(html).not.toContain('email-pending');
expect(html).toContain('Het contactformulier komt binnenkort.');
});
});
@@ -3,7 +3,7 @@ import { Box, chakra, Link, Text, useRecipe } from '@chakra-ui/react';
import Eyebrow from '@/components/Eyebrow/Eyebrow';
import PrivacyNote from '@/components/PrivacyNote/PrivacyNote';
import type { EmailAddress } from '@/content/types';
import { emailAddress } from '@/lib/emailAddress';
import useEmailAddress from '@/hooks/useEmailAddress';
import { mailtoLink } from '@/lib/mailLink';
type ContactFormGateProps = {
@@ -13,6 +13,9 @@ type ContactFormGateProps = {
children: ReactNode;
};
// The text that comes before the address once it is shown
const MAIL_LEAD = ' Mail me voor nu op ';
// Until there is a back-end the form stays visible but dimmed and inert
// under a "coming soon" notice with a mail button (FR-HP-07). inert keeps
// keyboard and screen reader users out of a form that does nothing.
@@ -23,10 +26,11 @@ export default function ContactFormGate({
children,
}: ContactFormGateProps) {
const linkButton = useRecipe({ key: 'linkButton' });
// Assembled after hydration, so the address is nowhere in the HTML or the
// bundle (BR-MAIL-05)
const { available, address } = useEmailAddress(email);
if (enabled) return <>{children}</>;
// Assembled in the browser, so the address is nowhere in the bundle
const address = emailAddress(email);
const href = address ? mailtoLink(address, subject) : undefined;
return (
@@ -49,26 +53,52 @@ export default function ContactFormGate({
data-testid="contact-gate-notice"
>
<Eyebrow>Binnenkort</Eyebrow>
<Text color="ink.soft" mb={address ? 7 : 0}>
<Text color="ink.soft" mb={available ? 7 : 0}>
Het contactformulier komt binnenkort.
{address && (
{address ? (
<>
{' Mail me voor nu op '}
{MAIL_LEAD}
<Link variant="underline" href={href}>
{address}
</Link>
</>
) : (
available && (
// Reserves the width of the lead and the address
// (counted without assembling it), so the line
// breaks where it will after hydration
<chakra.span
display="inline-block"
visibility="hidden"
aria-hidden="true"
w={`${MAIL_LEAD.length + email.user.length + 1 + email.domain.length}ch`}
data-email-pending
/>
)
)}
</Text>
{address && (
<chakra.a
href={href}
css={linkButton({ variant: 'accent' })}
data-testid="contact-gate-mail"
>
Stuur een e-mail
</chakra.a>
)}
{available &&
(address ? (
<chakra.a
href={href}
css={linkButton({ variant: 'accent' })}
data-testid="contact-gate-mail"
>
Stuur een e-mail
</chakra.a>
) : (
// Same recipe and label, so the notice is as high as
// it will be; hidden from sight and from readers
<chakra.span
css={linkButton({ variant: 'accent' })}
visibility="hidden"
aria-hidden="true"
data-email-pending
data-testid="contact-gate-mail-pending"
>
Stuur een e-mail
</chakra.span>
))}
{/* Readable now, while the form's own line is inert (BR-PR-03) */}
<PrivacyNote mt={6} />
</Box>