From 7063b199b4748a9c354ed37e64cdc84c512f2c0c Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 14 Dec 2023 15:30:34 +0100 Subject: refactor(pages): rewrite helpers to output schema in json-ld format * make sure url are absolutes * nest breadcrumb schema in webpage schema * trim HTML tags from content/description * use a regular script instead of next/script (with the latter the schema is not updated on route change) * place the script in document head * add keywords, wordCount and readingTime keys in BlogPosting schema * fix breadcrumbs in search page (without query) * add tests (a `MatchInlineSnapshot` will be better but Prettier 3 is not supported yet) --- src/pages/thematique/[slug].tsx | 48 +++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) (limited to 'src/pages/thematique') diff --git a/src/pages/thematique/[slug].tsx b/src/pages/thematique/[slug].tsx index f019341..56b956f 100644 --- a/src/pages/thematique/[slug].tsx +++ b/src/pages/thematique/[slug].tsx @@ -3,7 +3,6 @@ import type { ParsedUrlQuery } from 'querystring'; import type { GetStaticPaths, GetStaticProps } from 'next'; import Head from 'next/head'; import { useRouter } from 'next/router'; -import Script from 'next/script'; import { useIntl } from 'react-intl'; import { getLayout, @@ -36,10 +35,10 @@ import { CONFIG } from '../../utils/config'; import { getLinksItemData, getPostsWithUrl, - getSchemaJson, - getSinglePageSchema, - getWebPageSchema, + getSchemaFrom, + getWebPageGraph, slugify, + trimHTMLTags, } from '../../utils/helpers'; import { loadTranslation, type Messages } from '../../utils/helpers/server'; import { @@ -79,26 +78,15 @@ const ThematicPage: NextPageWithLayout = ({ data }) => { const { content, intro, meta, slug, title } = thematic; const { articles, dates, seo, relatedTopics } = meta; - const webpageSchema = getWebPageSchema({ - description: seo.description, - locale: CONFIG.locales.defaultLocale, - slug, - title: seo.title, - updateDate: dates.update, - }); - const articleSchema = getSinglePageSchema({ - dates, - description: intro, - id: 'thematic', - kind: 'page', - locale: CONFIG.locales.defaultLocale, - slug, - title, - }); - const schemaJsonLd = getSchemaJson([ - webpageSchema, - articleSchema, - breadcrumbSchema, + const jsonLd = getSchemaFrom([ + getWebPageGraph({ + breadcrumb: breadcrumbSchema, + copyrightYear: new Date(dates.publication).getFullYear(), + dates, + description: trimHTMLTags(intro), + slug, + title, + }), ]); const messages = { @@ -148,14 +136,12 @@ const ThematicPage: NextPageWithLayout = ({ data }) => { +