Logo
BlogDashboardAboutProjects
Logo
HomeBlogDashboardAboutProjects
GithubGithub
LinkedinLinkedin
TwitterTwitter
v1.12.1
23 Jan 2023
·
1 min read

Adding JSON-LD structured data to your React blog

Google uses structured data to understand what's on your page. JSON-LD is the recommended format. Here's a minimal React component to add it, and how to verify it works.

Search engines like Google use structured data to understand the contents of our page better. JSON-LD is a format that most use to describe the data.

Structured Data

Below is an example of a piece of structured data.

{
  "@context": "https://json-ld.org/contexts/person.jsonld",
  "@id": "http://dbpedia.org/resource/John_Lennon",
  "name": "John Lennon",
  "born": "1940-10-09",
  "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
}

Source: JSON-LD

Adding Structured Data

  1. Create the following component StructuredData.jsx.
export const StructuredData = ({ data }) => {
  return <script type="application/ld+json">{JSON.stringify(data)}</script>;
};
  1. Use the StructuredData component in your respective component containing the UI for your post.
export const BlogPost = ({ post }) => {
  const structuredData = {
    "@context": "https://schema.org",
    "@type": "Article",
    headline: post.title,
    description: post.description,
    author: [
      {
        "@type": "Person",
        name: post.author,
      },
    ],
    image: post.imageUrl,
    datePublished: post.publishedAt,
  };
};

return (
  <>
    <Structured data={structuredData} />
    {/* ... the rest of your blog post */}
  </>
);

Validate Structured Data

Before deploying the changes to production, we can inspect the HTML elements on a browser to see if the changes are reflected in the <head> section of the page.

Upon successful deployment, we can use the Schema Markup Validator to validate.

Related Articles

11 Nov 2023
1 tag
process.env vs import.meta: environment variables in Node and the browser
process.env only runs in Node. It won't work in the browser. import.meta is the module-aware alternative, and it works with TypeScript too.
23 Nov 2023
1 tag
What the ! operator does in TypeScript and Swift
The ! at the end of a TypeScript expression tells the compiler you know the value isn't null or undefined. It's the same idea as force unwrapping in Swift. Useful, but worth understanding before reaching for it.
19 Jan 2023
1 tag
Using the Web Share API to trigger the OS share sheet
Instead of building a custom share UI, you can hand off to the OS share sheet with one function call. navigator.share() works on mobile browsers. Here's how to use it.
29 Mar 2023
1 tag
Feature flags: ship code without shipping features
A feature flag is an if/else around a piece of code, controlled by an environment variable. It lets you merge and deploy work-in-progress without turning it on. Fewer conflicts, safer releases.