From 2c2e27e337c7b7fa973b6e3dc7e30c097e1067d8 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sat, 31 Oct 2026 18:17:26 +0100 Subject: [PATCH] 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 --- .../ContactDetails/ContactDetails.test.tsx | 72 +++++++++ .../ContactDetails/ContactDetails.tsx | 99 +++++++----- .../ContactForm/ContactForm.test.tsx | 19 +++ .../components/ContactForm/ContactForm.tsx | 4 +- .../ContactForm/ContactFormGate.test.tsx | 76 +++++++++ .../ContactForm/ContactFormGate.tsx | 60 +++++-- web/src/components/Footer/Footer.test.tsx | 97 ++++++++++- web/src/components/Footer/Footer.tsx | 44 +++-- .../components/Hero/useHeroSlider.test.tsx | 72 +++++++++ .../components/NavigationBar/MobileNav.tsx | 5 + .../NavigationBar/NavigationBar.test.tsx | 41 +++++ .../NavigationBar/NavigationBar.tsx | 9 +- .../components/StatusPage/NotFound.test.tsx | 49 ++++++ web/src/components/StatusPage/NotFound.tsx | 6 +- .../home/Testimonials/Testimonials.test.tsx | 62 ++++++++ .../home/Testimonials/Testimonials.tsx | 16 +- web/src/hooks/useEmailAddress.test.tsx | 72 +++++++++ web/src/hooks/useEmailAddress.ts | 18 +++ web/src/hooks/useHydrated.test.tsx | 35 ++++ web/src/hooks/useInView.test.tsx | 82 ++++++++++ web/src/hooks/useInView.ts | 3 + web/src/hooks/usePageLoaded.test.tsx | 45 +++++- web/src/hooks/useReveal.test.tsx | 33 ++++ web/src/lib/contactChannels.test.ts | 150 ++++++++++++------ web/src/lib/contactChannels.ts | 35 ++-- web/src/lib/emailAddress.test.ts | 32 +++- web/src/lib/emailAddress.ts | 13 +- web/src/routes/contact.tsx | 6 +- web/src/routes/hydration.test.tsx | 73 +++++++++ web/src/routes/index.tsx | 6 +- web/src/routes/portfolio.tsx | 6 +- web/src/test/hydrate.tsx | 130 +++++++++++++++ 32 files changed, 1322 insertions(+), 148 deletions(-) create mode 100644 web/src/components/StatusPage/NotFound.test.tsx create mode 100644 web/src/hooks/useEmailAddress.test.tsx create mode 100644 web/src/hooks/useEmailAddress.ts create mode 100644 web/src/hooks/useHydrated.test.tsx create mode 100644 web/src/hooks/useInView.test.tsx create mode 100644 web/src/routes/hydration.test.tsx create mode 100644 web/src/test/hydrate.tsx diff --git a/web/src/components/ContactDetails/ContactDetails.test.tsx b/web/src/components/ContactDetails/ContactDetails.test.tsx index 8b0cb8b..4417089 100644 --- a/web/src/components/ContactDetails/ContactDetails.test.tsx +++ b/web/src/components/ContactDetails/ContactDetails.test.tsx @@ -1,7 +1,10 @@ import { screen, within } from '@testing-library/react'; +import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import siteContent from '@/content/siteContent'; +import { validEmail } from '@/test/arbitraries'; import { axeResults } from '@/test/axe'; +import { hydratePage, serverHtml } from '@/test/hydrate'; import { renderWithProviders } from '@/test/renderWithProviders'; import ContactDetails from './ContactDetails'; @@ -104,3 +107,72 @@ describe('ContactDetails', () => { expect(await axeResults(container)).toHaveNoViolations(); }); }); + +// The address is only there after hydrating; before, the first item is an +// empty placeholder of the same height (BR-MAIL-04, BR-MAIL-07) +describe('ContactDetails in the prerendered HTML', () => { + it('holds a placeholder first and the socials, but no address', async () => { + const html = await serverHtml(card()); + expect(html.toLowerCase()).not.toContain('info@andreaslop.nl'); + expect(html).not.toContain('mailto:'); + expect(html).toContain('data-testid="contact-details-email-pending"'); + expect(html.indexOf('contact-details-email-pending')).toBeLessThan( + html.indexOf('contact-details-instagram'), + ); + expect(html).toContain('contact-details-facebook'); + }); + + it('never holds the address, whatever the content (invariant)', async () => { + await fc.assert( + fc.asyncProperty(validEmail, async (email) => { + const html = await serverHtml(card({ email })); + expect(html.toLowerCase()).not.toContain( + `${email.user}@${email.domain}`.toLowerCase(), + ); + expect(html).not.toContain('mailto:'); + }), + ); + }); + + it('swaps the placeholder for the link after hydrating', async () => { + const { errors } = await hydratePage(card()); + const mail = await screen.findByTestId('contact-details-email'); + expect(mail).toHaveAttribute( + 'href', + 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl', + ); + expect( + screen.queryByTestId('contact-details-email-pending'), + ).not.toBeInTheDocument(); + expect(errors).toEqual([]); + }); + + it('marks the placeholder hidden, empty and without a link', async () => { + const html = await serverHtml(card()); + const doc = new DOMParser().parseFromString(html, 'text/html'); + const pending = doc.querySelector('[data-email-pending]')!; + expect(pending.getAttribute('aria-hidden')).toBe('true'); + expect(pending.textContent).toBe(''); + expect(pending.querySelector('a')).toBeNull(); + }); + + it('has no placeholder on a plain mount', async () => { + const { container } = await renderWithProviders(card()); + expect(container.querySelector('[data-email-pending]')).toBeNull(); + }); + + it('has no placeholder without a valid address, only the socials', async () => { + const html = await serverHtml( + card({ email: { user: '', domain: 'andreaslop.nl' } }), + ); + expect(html).not.toContain('email-pending'); + expect(html).toContain('contact-details-instagram'); + }); + + it('renders nothing without an address and without socials', async () => { + const html = await serverHtml( + card({ email: { user: '', domain: '' }, socials: [] }), + ); + expect(html).not.toContain('data-testid="details"'); + }); +}); diff --git a/web/src/components/ContactDetails/ContactDetails.tsx b/web/src/components/ContactDetails/ContactDetails.tsx index b551bee..92c5070 100644 --- a/web/src/components/ContactDetails/ContactDetails.tsx +++ b/web/src/components/ContactDetails/ContactDetails.tsx @@ -11,6 +11,7 @@ import { Icon } from '@iconify/react/offline'; import Eyebrow from '@/components/Eyebrow/Eyebrow'; import socialPlatforms from '@/content/socialPlatforms'; import type { ContentImage, EmailAddress, SocialLink } from '@/content/types'; +import useHydrated from '@/hooks/useHydrated'; import { contactChannels, type ContactChannel } from '@/lib/contactChannels'; type ContactDetailsProps = { @@ -25,13 +26,18 @@ type ContactDetailsProps = { 'data-testid'?: string; }; -const iconOf = (channel: ContactChannel) => +// Channels that render as a link; the pending e-mail item is not one +type LinkChannel = Exclude; + +const iconOf = (channel: LinkChannel) => channel.kind === 'email' ? mailFilled : socialPlatforms[channel.platform].icon; // "Direct contact": a card with the ways to reach Andrea, each an icon and a -// link (BR-CD-01..08). Renders nothing when no channel is valid. +// link (BR-CD-01..08). Renders nothing when no channel is valid. Before +// hydration the e-mail item is an empty placeholder of the same height +// (BR-MAIL-04). export default function ContactDetails({ titleId, title, @@ -40,7 +46,8 @@ export default function ContactDetails({ portrait, 'data-testid': testId, }: ContactDetailsProps) { - const channels = contactChannels(email, socials); + // The address is only assembled after hydration (BR-MAIL-07) + const channels = contactChannels(email, socials, useHydrated()); if (channels.length === 0) return null; return ( @@ -71,47 +78,57 @@ export default function ContactDetails({ - {channels.map((channel) => ( - - + channel.kind === 'email-pending' ? ( + - ))} + {channel.label} + {channel.external && ( + <> + {' '} + + (opent in nieuw tabblad) + + + )} + + + ), + )} ); diff --git a/web/src/components/ContactForm/ContactForm.test.tsx b/web/src/components/ContactForm/ContactForm.test.tsx index 11121e4..2a55818 100644 --- a/web/src/components/ContactForm/ContactForm.test.tsx +++ b/web/src/components/ContactForm/ContactForm.test.tsx @@ -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( + , + ); + expect(html).toContain('contact-form-submit'); + expect(html.toLowerCase()).not.toContain('info@andreaslop.nl'); + expect(html).not.toContain('mailto:'); + }); +}); diff --git a/web/src/components/ContactForm/ContactForm.tsx b/web/src/components/ContactForm/ContactForm.tsx index 40097d7..5f8a1b5 100644 --- a/web/src/components/ContactForm/ContactForm.tsx +++ b/web/src/components/ContactForm/ContactForm.tsx @@ -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') { diff --git a/web/src/components/ContactForm/ContactFormGate.test.tsx b/web/src/components/ContactForm/ContactFormGate.test.tsx index 39b3e79..f6895b0 100644 --- a/web/src/components/ContactForm/ContactFormGate.test.tsx +++ b/web/src/components/ContactForm/ContactFormGate.test.tsx @@ -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) => ( + +
+ + ); + 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.'); + }); +}); diff --git a/web/src/components/ContactForm/ContactFormGate.tsx b/web/src/components/ContactForm/ContactFormGate.tsx index 41b3f10..e8081c5 100644 --- a/web/src/components/ContactForm/ContactFormGate.tsx +++ b/web/src/components/ContactForm/ContactFormGate.tsx @@ -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" > Binnenkort - + Het contactformulier komt binnenkort. - {address && ( + {address ? ( <> - {' Mail me voor nu op '} + {MAIL_LEAD} {address} + ) : ( + available && ( + // Reserves the width of the lead and the address + // (counted without assembling it), so the line + // breaks where it will after hydration + - {address && ( - - Stuur een e-mail - - )} + {available && + (address ? ( + + Stuur een e-mail + + ) : ( + // Same recipe and label, so the notice is as high as + // it will be; hidden from sight and from readers + + ))} {/* Readable now, while the form's own line is inert (BR-PR-03) */} diff --git a/web/src/components/Footer/Footer.test.tsx b/web/src/components/Footer/Footer.test.tsx index 3220347..90a3fca 100644 --- a/web/src/components/Footer/Footer.test.tsx +++ b/web/src/components/Footer/Footer.test.tsx @@ -1,8 +1,11 @@ import { screen, within } from '@testing-library/react'; -import { describe, expect, it } from 'vitest'; +import fc from 'fast-check'; +import { describe, expect, it, vi } from 'vitest'; import siteContent from '@/content/siteContent'; import type { SiteContent } from '@/content/types'; +import { validEmail } from '@/test/arbitraries'; import { axeResults } from '@/test/axe'; +import { hydratePage, serverHtml } from '@/test/hydrate'; import { renderWithProviders } from '@/test/renderWithProviders'; import Footer from './Footer'; @@ -126,3 +129,95 @@ describe('Footer', () => { expect(screen.queryByTestId('footer-email-link')).toBeNull(); }); }); + +// The page is prerendered and hydrated in the browser: the address and the +// current year only come after hydrating (BR-MAIL-01, BR-HYD-14) +describe('Footer in the prerendered HTML', () => { + const noAddress = (html: string, address: string) => { + expect(html.toLowerCase()).not.toContain(address.toLowerCase()); + expect(html).not.toContain('mailto:'); + }; + + it('holds an empty placeholder instead of the address', async () => { + const html = await serverHtml(