Skip to content

Commit

Permalink
Add StructuredData Component (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
timoclsn authored Sep 8, 2023
1 parent 04df185 commit fc65214
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
75 changes: 36 additions & 39 deletions apps/website/src/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { format } from "date-fns";
import { CalendarDays, Clock, User } from "lucide-react";
import { groq } from "next-sanity";
import readingTime from "reading-time";
import { Article, WithContext } from "schema-dts";
import { z } from "zod";
import { ArticleHeader } from "../../../components/ArticleHeader/ArticleHeader";
import { MDXContent } from "../../../components/MDXContent/MDXContent";
import { StructuredData } from "../../../components/StructuredData/StructuredData";
import { Container } from "../../../design-system/Container/Container";
import { createGenerateMetadata, ogImage } from "../../../lib/metadata";
import { queryContent } from "../../../lib/sanity";
Expand All @@ -29,7 +29,7 @@ export const generateMetadata = createGenerateMetadata(async ({ params }) => {
summary: z.string().nullable(),
image: z.string(),
content: z.string(),
})
}),
);

const stats = readingTime(content);
Expand Down Expand Up @@ -80,8 +80,8 @@ export const generateStaticParams = async () => {
z.array(
z.object({
slug: z.string(),
})
)
}),
),
);

return blogPosts.map((blogPost) => ({
Expand Down Expand Up @@ -129,53 +129,50 @@ const BlogPostPage = async ({ params }: Props) => {
.array(
z.object({
title: z.string(),
})
}),
)
.nullable(),
topics: z
.array(
z.object({
title: z.string(),
})
}),
)
.nullable(),
content: z.string(),
})
}),
);

const stats = readingTime(blogPost.content);

const jsonLd: WithContext<Article> = {
"@context": "https://schema.org",
"@type": "Article",
mainEntityOfPage: {
"@type": "BlogPosting",
"@id": `https://katharinaclasen.com/blog/${blogPost.slug}`,
},
headline: blogPost.title,
image: [blogPost.image.url],
datePublished: blogPost.date,
dateModified: blogPost.date,
author: {
"@type": "Person",
name: "Katharina Clasen",
},
publisher: {
"@type": "Organization",
name: "Katharina Clasen",
logo: {
"@type": "ImageObject",
url: "/favicon.png",
},
},
description: blogPost.summary || "Blog post by Katharina Clasen",
};
return (
<>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
<StructuredData type="Article">
{{
"@context": "https://schema.org",
"@type": "Article",
mainEntityOfPage: {
"@type": "BlogPosting",
"@id": `https://katharinaclasen.com/blog/${blogPost.slug}`,
},
headline: blogPost.title,
image: [blogPost.image.url],
datePublished: blogPost.date,
dateModified: blogPost.date,
author: {
"@type": "Person",
name: "Katharina Clasen",
},
publisher: {
"@type": "Organization",
name: "Katharina Clasen",
logo: {
"@type": "ImageObject",
url: "/favicon.png",
},
},
description: blogPost.summary || "Blog post by Katharina Clasen",
}}
</StructuredData>
<article className="py-20 sm:py-32">
<Container size="small" inset>
<ArticleHeader
Expand All @@ -202,7 +199,7 @@ const BlogPostPage = async ({ params }: Props) => {
({
outline: "solid",
text: service.title,
} as const)
}) as const,
)
: []),
...(blogPost.topics
Expand All @@ -211,7 +208,7 @@ const BlogPostPage = async ({ params }: Props) => {
({
outline: "dash",
text: topic.title,
} as const)
}) as const,
)
: []),
]}
Expand Down
21 changes: 21 additions & 0 deletions apps/website/src/components/StructuredData/StructuredData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Thing, WithContext } from "schema-dts";

type ThingWithoutString = Exclude<Thing, string>;

interface Props<TThing extends ThingWithoutString> {
type: TThing["@type"];
children: WithContext<TThing>;
}

export const StructuredData = <T extends ThingWithoutString>({
children,
}: Props<T>) => {
return (
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify(children),
}}
/>
);
};

2 comments on commit fc65214

@vercel
Copy link

@vercel vercel bot commented on fc65214 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on fc65214 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.