Adds a generic WebPage structured data helper
webPage(type, path, extra) builds the schema.org page data every page needs: the canonical URL as @id and url, name and description from the page registry, the language, and page-specific fields last. aboutPage now uses it and gives exactly the same output. The Portfolio and Soorten fotoshoots pages build their structured data on it too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,10 +4,12 @@ import aboutContent from './content/aboutContent';
|
|||||||
import siteContent from './content/siteContent';
|
import siteContent from './content/siteContent';
|
||||||
import { emailAddress } from './lib/emailAddress';
|
import { emailAddress } from './lib/emailAddress';
|
||||||
import { visibleSocials } from './lib/visibleSocials';
|
import { visibleSocials } from './lib/visibleSocials';
|
||||||
|
import type { RoutePath } from './navigation';
|
||||||
import {
|
import {
|
||||||
aboutPage,
|
aboutPage,
|
||||||
businessId,
|
businessId,
|
||||||
canonicalUrl,
|
canonicalUrl,
|
||||||
|
defaultOgImage,
|
||||||
jsonLdScript,
|
jsonLdScript,
|
||||||
pageHead,
|
pageHead,
|
||||||
person,
|
person,
|
||||||
@@ -16,6 +18,8 @@ import {
|
|||||||
siteHead,
|
siteHead,
|
||||||
siteUrl,
|
siteUrl,
|
||||||
structuredDataScript,
|
structuredDataScript,
|
||||||
|
webPage,
|
||||||
|
type WebPageType,
|
||||||
} from './seo';
|
} from './seo';
|
||||||
import sitePages from './sitePages';
|
import sitePages from './sitePages';
|
||||||
import {
|
import {
|
||||||
@@ -278,3 +282,129 @@ describe('structured data about Andrea', () => {
|
|||||||
expect(JSON.parse(script.children)).toEqual({ name: '</script>' });
|
expect(JSON.parse(script.children)).toEqual({ name: '</script>' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('webPage', () => {
|
||||||
|
const paths = Object.keys(sitePages) as RoutePath[];
|
||||||
|
const anyPath = fc.constantFrom(...paths);
|
||||||
|
const anyType = fc.constantFrom<WebPageType>(
|
||||||
|
'WebPage',
|
||||||
|
'AboutPage',
|
||||||
|
'ContactPage',
|
||||||
|
'ImageGallery',
|
||||||
|
'CollectionPage',
|
||||||
|
);
|
||||||
|
const baseKeys = [
|
||||||
|
'@context',
|
||||||
|
'@type',
|
||||||
|
'@id',
|
||||||
|
'url',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'inLanguage',
|
||||||
|
];
|
||||||
|
// Page-specific fields: any JSON, under keys the base does not use
|
||||||
|
const anyExtra = fc.dictionary(
|
||||||
|
fc.string().filter((key) => !baseKeys.includes(key)),
|
||||||
|
fc.jsonValue(),
|
||||||
|
);
|
||||||
|
|
||||||
|
it('describes a page from the registry', () => {
|
||||||
|
expect(
|
||||||
|
webPage('ContactPage', '/contact', {
|
||||||
|
about: { '@id': businessId },
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
'@context': 'https://schema.org',
|
||||||
|
'@type': 'ContactPage',
|
||||||
|
'@id': `${siteUrl}/contact`,
|
||||||
|
url: `${siteUrl}/contact`,
|
||||||
|
name: sitePages['/contact'].title,
|
||||||
|
description: sitePages['/contact'].description,
|
||||||
|
inLanguage: 'nl-NL',
|
||||||
|
about: { '@id': businessId },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('needs no extra fields', () => {
|
||||||
|
expect(webPage('WebPage', '/')).not.toHaveProperty('mainEntity');
|
||||||
|
expect(webPage('WebPage', '/')['@id']).toBe(`${siteUrl}/`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('takes name, description and URL from the registry (invariant)', () => {
|
||||||
|
fc.assert(
|
||||||
|
fc.property(anyType, anyPath, (type, path) => {
|
||||||
|
const page = webPage(type, path);
|
||||||
|
expect(page['@context']).toBe('https://schema.org');
|
||||||
|
expect(page['@type']).toBe(type);
|
||||||
|
expect(page['@id']).toBe(canonicalUrl(path));
|
||||||
|
expect(page.url).toBe(canonicalUrl(path));
|
||||||
|
expect(page.name).toBe(sitePages[path].title);
|
||||||
|
expect(page.description).toBe(sitePages[path].description);
|
||||||
|
expect(page.inLanguage).toBe('nl-NL');
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps every extra field as given (invariant)', () => {
|
||||||
|
fc.assert(
|
||||||
|
fc.property(anyType, anyPath, anyExtra, (type, path, extra) => {
|
||||||
|
const page: Record<string, unknown> = webPage(
|
||||||
|
type,
|
||||||
|
path,
|
||||||
|
extra,
|
||||||
|
);
|
||||||
|
for (const [key, value] of Object.entries(extra)) {
|
||||||
|
expect(page[key]).toEqual(value);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('survives the ld+json script unchanged (round trip)', () => {
|
||||||
|
fc.assert(
|
||||||
|
fc.property(anyType, anyPath, anyExtra, (type, path, extra) => {
|
||||||
|
const page = webPage(type, path, extra);
|
||||||
|
expect(JSON.parse(jsonLdScript(page))).toEqual(
|
||||||
|
JSON.parse(JSON.stringify(page)),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// aboutPage as it was before it used webPage, kept as the oracle for the
|
||||||
|
// rebuilt version
|
||||||
|
function previousAboutPage(
|
||||||
|
path: RoutePath,
|
||||||
|
content: Parameters<typeof aboutPage>[1],
|
||||||
|
site: Parameters<typeof aboutPage>[2],
|
||||||
|
) {
|
||||||
|
const page = sitePages[path];
|
||||||
|
const image = siteUrl + (page.ogImage ?? defaultOgImage).path;
|
||||||
|
return {
|
||||||
|
'@context': 'https://schema.org',
|
||||||
|
'@type': 'AboutPage',
|
||||||
|
'@id': canonicalUrl(path),
|
||||||
|
url: canonicalUrl(path),
|
||||||
|
name: page.title,
|
||||||
|
description: page.description,
|
||||||
|
inLanguage: 'nl-NL',
|
||||||
|
mainEntity: person(content, site, image),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
it('leaves the about page data exactly as it was (oracle)', () => {
|
||||||
|
fc.assert(
|
||||||
|
fc.property(
|
||||||
|
anyPath,
|
||||||
|
anySiteContent,
|
||||||
|
knowsAboutEntries,
|
||||||
|
(path, site, knowsAbout) => {
|
||||||
|
const content = { ...aboutContent.person, knowsAbout };
|
||||||
|
expect(aboutPage(path, content, site)).toEqual(
|
||||||
|
previousAboutPage(path, content, site),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
+25
-8
@@ -115,27 +115,44 @@ export function person(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// The about page with Andrea as its subject (BR-SD-06); name, description
|
// The schema.org page types the site uses; add one here when a page needs it
|
||||||
// and preview image come from the page registry
|
export type WebPageType =
|
||||||
export function aboutPage(
|
'WebPage' | 'AboutPage' | 'ContactPage' | 'ImageGallery' | 'CollectionPage';
|
||||||
|
|
||||||
|
// Structured data for a page: its canonical URL as @id, name and description
|
||||||
|
// from the page registry. Page-specific fields (mainEntity, about, ...) come
|
||||||
|
// in through `extra`, which is added last and keeps its type for callers.
|
||||||
|
export function webPage<Extra extends object = Record<never, never>>(
|
||||||
|
type: WebPageType,
|
||||||
path: RoutePath,
|
path: RoutePath,
|
||||||
content: AboutPageContent['person'],
|
extra: Extra = {} as Extra,
|
||||||
site: SiteContent,
|
|
||||||
) {
|
) {
|
||||||
const page = sitePages[path];
|
const page = sitePages[path];
|
||||||
const image = siteUrl + (page.ogImage ?? defaultOgImage).path;
|
|
||||||
return {
|
return {
|
||||||
'@context': 'https://schema.org',
|
'@context': 'https://schema.org',
|
||||||
'@type': 'AboutPage',
|
'@type': type,
|
||||||
'@id': canonicalUrl(path),
|
'@id': canonicalUrl(path),
|
||||||
url: canonicalUrl(path),
|
url: canonicalUrl(path),
|
||||||
name: page.title,
|
name: page.title,
|
||||||
description: page.description,
|
description: page.description,
|
||||||
inLanguage: 'nl-NL',
|
inLanguage: 'nl-NL',
|
||||||
mainEntity: person(content, site, image),
|
...extra,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The about page with Andrea as its subject (BR-SD-06); her image is the
|
||||||
|
// page's preview image from the registry
|
||||||
|
export function aboutPage(
|
||||||
|
path: RoutePath,
|
||||||
|
content: AboutPageContent['person'],
|
||||||
|
site: SiteContent,
|
||||||
|
) {
|
||||||
|
const image = siteUrl + (sitePages[path].ogImage ?? defaultOgImage).path;
|
||||||
|
return webPage('AboutPage', path, {
|
||||||
|
mainEntity: person(content, site, image),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// A head script tag with structured data, escaped by jsonLdScript (BR-SD-07)
|
// A head script tag with structured data, escaped by jsonLdScript (BR-SD-07)
|
||||||
export function structuredDataScript(data: unknown) {
|
export function structuredDataScript(data: unknown) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user