Structured Data
Overview
Structured data is a standardized format for providing information about a page and classifying its content. It helps search engines understand the content of your pages and can enhance how your pages appear in search results.
Component
The StructuredDataScript
component provides a type-safe way to add structured data to your Next.js pages using the schema-dts
package. It follows Next.js 15's recommended approach for implementing structured data.
import type { Thing, WithContext } from "schema-dts";
type StructuredDataScriptProps<T extends Thing> = {
data: WithContext<T>;
id?: string;
};
export default function StructuredDataScript<T extends Thing>({
data,
id,
}: StructuredDataScriptProps<T>) {
return (
<script
id={id}
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify(data),
}}
/>
);
}
Usage Examples
Basic Usage
import { StructuredDataScript } from "@/components/structured-data";
import type { WithContext, Article } from "schema-dts";
const articleData: WithContext<Article> = {
"@context": "https://schema.org",
"@type": "Article",
headline: "My Article Title",
author: {
"@type": "Person",
name: "John Doe",
},
datePublished: "2024-03-20",
};
export default function ArticlePage() {
return (
<>
<StructuredDataScript data={articleData} />
{/* Your page content */}
</>
);
}
References
- Next.js Structured Data Documentation
- Schema.org
- Google Rich Results Test
- Google Search Central - Structured Data
Tools
- schema-dts - TypeScript types for Schema.org vocabulary
- SEO Meta 1 Copy - Chrome extension for testing structured data
- Schema.org Validator - Validate your structured data against Schema.org