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:
@@ -2,43 +2,52 @@ import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import siteContent from '../content/siteContent';
|
||||
import socialPlatforms from '../content/socialPlatforms';
|
||||
import { anyEmail, socialLink } from '../test/arbitraries';
|
||||
import { contactChannels } from './contactChannels';
|
||||
import { anyEmail, socialLink, validEmail } from '../test/arbitraries';
|
||||
import { type ContactChannel, contactChannels } from './contactChannels';
|
||||
import { emailAddress } from './emailAddress';
|
||||
import { visibleSocials } from './visibleSocials';
|
||||
|
||||
const socialLinks = fc.array(socialLink, { maxLength: 5 });
|
||||
|
||||
// With the address assembled there is no placeholder channel, so the narrowed
|
||||
// type lets the existing tests read href, label and external
|
||||
const assembled = (
|
||||
email: Parameters<typeof contactChannels>[0],
|
||||
socials: Parameters<typeof contactChannels>[1],
|
||||
) =>
|
||||
contactChannels(email, socials, true) as Exclude<
|
||||
ContactChannel,
|
||||
{ kind: 'email-pending' }
|
||||
>[];
|
||||
|
||||
describe('contactChannels', () => {
|
||||
it('lists e-mail, Instagram and Facebook for the site content', () => {
|
||||
expect(contactChannels(siteContent.email, siteContent.socials)).toEqual(
|
||||
[
|
||||
{
|
||||
kind: 'email',
|
||||
href: 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl',
|
||||
label: 'info@andreaslop.nl',
|
||||
external: false,
|
||||
},
|
||||
{
|
||||
kind: 'social',
|
||||
platform: 'instagram',
|
||||
href: 'https://www.instagram.com/andreaslopfotografie/',
|
||||
label: '@andreaslopfotografie',
|
||||
external: true,
|
||||
},
|
||||
{
|
||||
kind: 'social',
|
||||
platform: 'facebook',
|
||||
href: 'https://www.facebook.com/andreaslopfotografie/',
|
||||
label: 'Andrea Slop Fotografie',
|
||||
external: true,
|
||||
},
|
||||
],
|
||||
);
|
||||
expect(assembled(siteContent.email, siteContent.socials)).toEqual([
|
||||
{
|
||||
kind: 'email',
|
||||
href: 'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl',
|
||||
label: 'info@andreaslop.nl',
|
||||
external: false,
|
||||
},
|
||||
{
|
||||
kind: 'social',
|
||||
platform: 'instagram',
|
||||
href: 'https://www.instagram.com/andreaslopfotografie/',
|
||||
label: '@andreaslopfotografie',
|
||||
external: true,
|
||||
},
|
||||
{
|
||||
kind: 'social',
|
||||
platform: 'facebook',
|
||||
href: 'https://www.facebook.com/andreaslopfotografie/',
|
||||
label: 'Andrea Slop Fotografie',
|
||||
external: true,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('leaves out an invalid e-mail address and invalid links', () => {
|
||||
const channels = contactChannels({ user: 'info', domain: 'nodot' }, [
|
||||
const channels = assembled({ user: 'info', domain: 'nodot' }, [
|
||||
{ platform: 'facebook', href: 'javascript:alert(1)' },
|
||||
{ platform: 'instagram', href: 'https://instagram.com/x' },
|
||||
]);
|
||||
@@ -46,7 +55,7 @@ describe('contactChannels', () => {
|
||||
});
|
||||
|
||||
it('falls back to the platform name without a usable label', () => {
|
||||
const channels = contactChannels({ user: '', domain: '' }, [
|
||||
const channels = assembled({ user: '', domain: '' }, [
|
||||
{ platform: 'instagram', href: 'https://instagram.com/x' },
|
||||
{
|
||||
platform: 'facebook',
|
||||
@@ -61,13 +70,13 @@ describe('contactChannels', () => {
|
||||
});
|
||||
|
||||
it('is empty when nothing is valid', () => {
|
||||
expect(contactChannels({ user: '', domain: '' }, [])).toEqual([]);
|
||||
expect(assembled({ user: '', domain: '' }, [])).toEqual([]);
|
||||
});
|
||||
|
||||
it('puts the e-mail channel first exactly when the address is valid (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, socialLinks, (email, socials) => {
|
||||
const channels = contactChannels(email, socials);
|
||||
const channels = assembled(email, socials);
|
||||
const valid = emailAddress(email) !== null;
|
||||
expect(channels[0]?.kind === 'email').toBe(valid);
|
||||
expect(
|
||||
@@ -80,16 +89,15 @@ describe('contactChannels', () => {
|
||||
it('shows exactly the visible social links, in order (oracle)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, socialLinks, (email, socials) => {
|
||||
const shown = contactChannels(email, socials).flatMap(
|
||||
(channel) =>
|
||||
channel.kind === 'social'
|
||||
? [
|
||||
{
|
||||
platform: channel.platform,
|
||||
href: channel.href,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
const shown = assembled(email, socials).flatMap((channel) =>
|
||||
channel.kind === 'social'
|
||||
? [
|
||||
{
|
||||
platform: channel.platform,
|
||||
href: channel.href,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
);
|
||||
expect(shown).toEqual(
|
||||
visibleSocials(socials).map(({ platform, href }) => ({
|
||||
@@ -104,10 +112,7 @@ describe('contactChannels', () => {
|
||||
it('labels a social link with its trimmed label, or else the platform name (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(socialLinks, (socials) => {
|
||||
const channels = contactChannels(
|
||||
{ user: '', domain: '' },
|
||||
socials,
|
||||
);
|
||||
const channels = assembled({ user: '', domain: '' }, socials);
|
||||
visibleSocials(socials).forEach((link, index) => {
|
||||
const expected =
|
||||
link.label?.trim() ||
|
||||
@@ -122,7 +127,7 @@ describe('contactChannels', () => {
|
||||
it('only links to mailto: or https: (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, socialLinks, (email, socials) => {
|
||||
for (const channel of contactChannels(email, socials)) {
|
||||
for (const channel of assembled(email, socials)) {
|
||||
const scheme = new URL(channel.href).protocol;
|
||||
expect(
|
||||
channel.kind === 'email' ? 'mailto:' : 'https:',
|
||||
@@ -135,7 +140,7 @@ describe('contactChannels', () => {
|
||||
it('opens exactly the social channels in a new tab (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, socialLinks, (email, socials) => {
|
||||
for (const channel of contactChannels(email, socials)) {
|
||||
for (const channel of assembled(email, socials)) {
|
||||
expect(channel.external).toBe(channel.kind === 'social');
|
||||
}
|
||||
}),
|
||||
@@ -145,9 +150,56 @@ describe('contactChannels', () => {
|
||||
it('has at most one channel per link plus the e-mail (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, socialLinks, (email, socials) => {
|
||||
expect(
|
||||
contactChannels(email, socials).length,
|
||||
).toBeLessThanOrEqual(1 + socials.length);
|
||||
expect(assembled(email, socials).length).toBeLessThanOrEqual(
|
||||
1 + socials.length,
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('contactChannels before the address may be assembled', () => {
|
||||
it('puts a placeholder without href and label first for valid parts', () => {
|
||||
expect(
|
||||
contactChannels(siteContent.email, siteContent.socials, false)[0],
|
||||
).toEqual({ kind: 'email-pending' });
|
||||
});
|
||||
|
||||
it('keeps the social channels the same as when assembling', () => {
|
||||
const socials = (assemble: boolean) =>
|
||||
contactChannels(
|
||||
siteContent.email,
|
||||
siteContent.socials,
|
||||
assemble,
|
||||
).filter((channel) => channel.kind === 'social');
|
||||
expect(socials(false)).toEqual(socials(true));
|
||||
});
|
||||
|
||||
it('gives no placeholder for invalid parts', () => {
|
||||
expect(
|
||||
contactChannels({ user: 'info', domain: 'nodot' }, [], false),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('never contains the assembled address (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(validEmail, socialLinks, (email, socials) => {
|
||||
const address = emailAddress(email)!;
|
||||
const json = JSON.stringify(
|
||||
contactChannels(email, socials, false),
|
||||
);
|
||||
expect(json).not.toContain(address);
|
||||
expect(json).not.toContain('mailto:');
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('has as many channels as when assembling (invariant)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, socialLinks, (email, socials) => {
|
||||
expect(contactChannels(email, socials, false)).toHaveLength(
|
||||
contactChannels(email, socials, true).length,
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -2,13 +2,16 @@ import socialPlatforms, {
|
||||
type SocialPlatform,
|
||||
} from '../content/socialPlatforms.ts';
|
||||
import type { EmailAddress, SocialLink } from '../content/types.ts';
|
||||
import { emailAddress } from './emailAddress.ts';
|
||||
import { emailAddress, isEmailAddressValid } from './emailAddress.ts';
|
||||
import { mailSubject, mailtoLink } from './mailLink.ts';
|
||||
import { visibleSocials } from './visibleSocials.ts';
|
||||
|
||||
// One way to reach Andrea, as the contact card shows it
|
||||
export type ContactChannel =
|
||||
| { kind: 'email'; href: string; label: string; external: false }
|
||||
// Stands in for the e-mail item until the address may be assembled
|
||||
// (BR-MAIL-07): no href and no label, so nothing readable or clickable
|
||||
| { kind: 'email-pending' }
|
||||
| {
|
||||
kind: 'social';
|
||||
platform: SocialPlatform;
|
||||
@@ -21,22 +24,28 @@ export type ContactChannel =
|
||||
// The channels of the contact card (BR-CD-01..05): the e-mail address first
|
||||
// when it is valid, then the valid social links in content order, each with
|
||||
// its label or else the platform name. Only mailto: and https: links come
|
||||
// out, because both helpers already drop everything else.
|
||||
// out, because both helpers already drop everything else. Without `assemble`
|
||||
// (prerender and hydrating render) a valid address gives an `email-pending`
|
||||
// placeholder, so the address stays out of the HTML (BR-MAIL-01).
|
||||
export function contactChannels(
|
||||
email: EmailAddress,
|
||||
socials: readonly SocialLink[],
|
||||
assemble: boolean,
|
||||
): ContactChannel[] {
|
||||
const address = emailAddress(email);
|
||||
const mail: ContactChannel[] = address
|
||||
? [
|
||||
{
|
||||
kind: 'email',
|
||||
href: mailtoLink(address, mailSubject()),
|
||||
label: address,
|
||||
external: false,
|
||||
},
|
||||
]
|
||||
: [];
|
||||
const mail: ContactChannel[] = [];
|
||||
if (isEmailAddressValid(email)) {
|
||||
if (assemble) {
|
||||
const address = emailAddress(email)!;
|
||||
mail.push({
|
||||
kind: 'email',
|
||||
href: mailtoLink(address, mailSubject()),
|
||||
label: address,
|
||||
external: false,
|
||||
});
|
||||
} else {
|
||||
mail.push({ kind: 'email-pending' });
|
||||
}
|
||||
}
|
||||
const social = visibleSocials(socials).map((link): ContactChannel => ({
|
||||
kind: 'social',
|
||||
platform: link.platform,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { anyEmail, validEmail } from '../test/arbitraries';
|
||||
import { emailAddress } from './emailAddress';
|
||||
import { emailAddress, isEmailAddressValid } from './emailAddress';
|
||||
|
||||
describe('emailAddress', () => {
|
||||
it('voegt geldige delen samen', () => {
|
||||
@@ -46,3 +46,33 @@ describe('emailAddress', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isEmailAddressValid', () => {
|
||||
it('is waar voor geldige delen', () => {
|
||||
expect(
|
||||
isEmailAddressValid({ user: 'info', domain: 'andreaslop.nl' }),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('is onwaar bij ongeldige delen', () => {
|
||||
for (const parts of [
|
||||
{ user: '', domain: 'andreaslop.nl' },
|
||||
{ user: 'info', domain: '' },
|
||||
{ user: 'in fo', domain: 'andreaslop.nl' },
|
||||
{ user: 'info', domain: 'andreaslop' },
|
||||
{ user: 'info', domain: 'andrea@slop.nl' },
|
||||
]) {
|
||||
expect(isEmailAddressValid(parts)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it('is precies dan waar als emailAddress een adres geeft (oracle)', () => {
|
||||
fc.assert(
|
||||
fc.property(anyEmail, (parts) => {
|
||||
expect(isEmailAddressValid(parts)).toBe(
|
||||
emailAddress(parts) !== null,
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,11 +2,14 @@ import type { EmailAddress } from '../content/types';
|
||||
|
||||
const invalidPart = (part: string) => part === '' || /[\s@]/.test(part);
|
||||
|
||||
// Zegt of de delen een geldig adres geven, zonder het samen te voegen. Zo
|
||||
// weten de pagina's al voor de hydratatie of er een adres is (BR-MAIL-08).
|
||||
export function isEmailAddressValid({ user, domain }: EmailAddress): boolean {
|
||||
return !invalidPart(user) && !invalidPart(domain) && domain.includes('.');
|
||||
}
|
||||
|
||||
// Voegt het e-mailadres pas in de browser samen (BR-EMAIL-01). Geeft null bij
|
||||
// ongeldige delen, zodat de footer de Contact-kolom kan weglaten (BR-EMAIL-02).
|
||||
export function emailAddress({ user, domain }: EmailAddress): string | null {
|
||||
if (invalidPart(user) || invalidPart(domain) || !domain.includes('.')) {
|
||||
return null;
|
||||
}
|
||||
return `${user}@${domain}`;
|
||||
export function emailAddress(parts: EmailAddress): string | null {
|
||||
return isEmailAddressValid(parts) ? `${parts.user}@${parts.domain}` : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user