From 7063b199b4748a9c354ed37e64cdc84c512f2c0c Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
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/recherche/index.tsx | 57 ++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 31 deletions(-)

(limited to 'src/pages/recherche/index.tsx')

diff --git a/src/pages/recherche/index.tsx b/src/pages/recherche/index.tsx
index fd7f9e1..84e75af 100644
--- a/src/pages/recherche/index.tsx
+++ b/src/pages/recherche/index.tsx
@@ -1,8 +1,6 @@
-/* eslint-disable max-statements */
 import type { GetStaticProps } from 'next';
 import Head from 'next/head';
 import { useRouter } from 'next/router';
-import Script from 'next/script';
 import { useCallback } from 'react';
 import { useIntl } from 'react-intl';
 import {
@@ -37,11 +35,11 @@ import type {
 import { CONFIG } from '../../utils/config';
 import { ROUTES } from '../../utils/constants';
 import {
-  getBlogSchema,
   getLinksItemData,
   getPostsWithUrl,
-  getSchemaJson,
-  getWebPageSchema,
+  getSchemaFrom,
+  getSearchResultsPageGraph,
+  getWebPageGraph,
 } from '../../utils/helpers';
 import { loadTranslation, type Messages } from '../../utils/helpers/server';
 import {
@@ -165,17 +163,17 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ data }) => {
         ? intl.formatMessage(
             {
               defaultMessage:
-                'Discover search results for {query} on {websiteName}.',
+                'Discover search results for {query} on {websiteName} website.',
               description: 'SearchPage: SEO - Meta description',
-              id: 'pg26sn',
+              id: 'bW6Zda',
             },
             { query: query.s as string, websiteName: CONFIG.name }
           )
         : intl.formatMessage(
             {
-              defaultMessage: 'Search for a post on {websiteName}.',
+              defaultMessage: 'Search for a post on {websiteName} website.',
               description: 'SearchPage: SEO - Meta description',
-              id: 'npisb3',
+              id: 'rEp1mS',
             },
             { websiteName: CONFIG.name }
           ),
@@ -213,21 +211,20 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ data }) => {
 
   const { items: breadcrumbItems, schema: breadcrumbSchema } = useBreadcrumbs();
 
-  const webpageSchema = getWebPageSchema({
-    description: messages.seo.metaDesc,
-    locale: CONFIG.locales.defaultLocale,
-    slug: asPath,
-    title: messages.pageTitle,
-  });
-  const blogSchema = getBlogSchema({
-    isSinglePage: false,
-    locale: CONFIG.locales.defaultLocale,
-    slug: asPath,
-  });
-  const schemaJsonLd = getSchemaJson([
-    webpageSchema,
-    blogSchema,
-    breadcrumbSchema,
+  const jsonLd = getSchemaFrom([
+    query.s
+      ? getSearchResultsPageGraph({
+          breadcrumb: breadcrumbSchema,
+          description: messages.seo.metaDesc,
+          slug: asPath,
+          title: messages.pageTitle,
+        })
+      : getWebPageGraph({
+          breadcrumb: breadcrumbSchema,
+          description: messages.seo.metaDesc,
+          slug: asPath,
+          title: messages.pageTitle,
+        }),
   ]);
 
   const pageUrl = `${CONFIG.url}${asPath}`;
@@ -243,14 +240,12 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ data }) => {
         <meta property="og:type" content="website" />
         <meta property="og:title" content={messages.pageTitle} />
         <meta property="og:description" content={messages.seo.title} />
+        <script
+          // eslint-disable-next-line react/no-danger
+          dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
+          type="application/ld+json"
+        />
       </Head>
-      <Script
-        // eslint-disable-next-line react/jsx-no-literals -- Id allowed
-        id="schema-blog"
-        type="application/ld+json"
-        // eslint-disable-next-line react/no-danger -- Necessary for schema
-        dangerouslySetInnerHTML={{ __html: JSON.stringify(schemaJsonLd) }}
-      />
       <PageHeader
         heading={messages.pageTitle}
         meta={{ total: articles ? articles[0].pageInfo.total : undefined }}
-- 
cgit v1.2.3

