import type { ReactElement } from 'react'
export interface SeoAuthor {
name: string
url?: string
/** @default "Person" */
type?: 'Person' | 'Organization'
}
export interface SeoArticle {
/** @default "Article" */
type?: 'Article' | 'BlogPosting' | 'NewsArticle'
publishedAt?: string
updatedAt?: string
authors?: readonly SeoAuthor[]
section?: string
tags?: readonly string[]
}
export interface SeoBreadcrumb {
name: string
url: string
}
export interface SeoProps {
title: string
description?: string
canonical?: string
image?: string
imageAlt?: string
siteName?: string
type?: 'website' | 'article' | 'profile'
locale?: string
noindex?: boolean
/**
* Shape of the link preview card on X.
*
* Emitted as ``: that is the attribute name X's
* crawler still reads, so the tag keeps the platform's former name even
* though the prop does not.
*/
card?: 'summary' | 'summary_large_image'
/** Explicit article facts used to generate Article JSON-LD. */
article?: SeoArticle
/** Ordered path from the site root to the current page. */
breadcrumbs?: readonly SeoBreadcrumb[]
jsonLd?: Record | Array>
}
/**
* Render React 19 document metadata for search engines and social previews.
* React hoists these tags into `` during server rendering.
*/
export function Seo({
title,
description,
canonical,
image,
imageAlt,
siteName,
type = 'website',
locale,
noindex = false,
card,
article,
breadcrumbs,
jsonLd,
}: Readonly): ReactElement {
const structuredData = createStructuredData({
title,
description,
canonical,
image,
article,
breadcrumbs,
jsonLd,
})
return (
<>
{title}
{description ? : null}
{canonical ? : null}
{description ? : null}
{canonical ? : null}
{siteName ? : null}
{locale ? : null}
{image ? : null}
{image && imageAlt ? : null}
{description ? : null}
{image ? : null}
{image && imageAlt ? : null}
{structuredData ? (
) : null}
>
)
}
function createStructuredData({
title,
description,
canonical,
image,
article,
breadcrumbs,
jsonLd,
}: Pick<
SeoProps,
'title' | 'description' | 'canonical' | 'image' | 'article' | 'breadcrumbs' | 'jsonLd'
>): SeoProps['jsonLd'] {
const generated: Array> = []
if (article) {
generated.push({
'@context': 'https://schema.org',
'@type': article.type ?? 'Article',
headline: title,
...(description ? { description } : {}),
...(canonical ? { mainEntityOfPage: { '@type': 'WebPage', '@id': canonical } } : {}),
...(image ? { image: [image] } : {}),
...(article.publishedAt ? { datePublished: article.publishedAt } : {}),
...(article.updatedAt ? { dateModified: article.updatedAt } : {}),
...(article.authors?.length
? {
author: article.authors.map((author) => ({
'@type': author.type ?? 'Person',
name: author.name,
...(author.url ? { url: author.url } : {}),
})),
}
: {}),
...(article.section ? { articleSection: article.section } : {}),
...(article.tags?.length ? { keywords: [...article.tags] } : {}),
})
}
if (breadcrumbs?.length) {
generated.push({
'@context': 'https://schema.org',
'@type': 'BreadcrumbList',
itemListElement: breadcrumbs.map((breadcrumb, index) => ({
'@type': 'ListItem',
position: index + 1,
name: breadcrumb.name,
item: breadcrumb.url,
})),
})
}
if (generated.length === 0) return jsonLd
if (!jsonLd) return generated.length === 1 ? generated[0] : generated
const custom = Array.isArray(jsonLd) ? jsonLd : [jsonLd]
return [...generated, ...custom]
}
function safeStructuredData(value: NonNullable): string {
return JSON.stringify(value).replace(/