DEV Community

Cover image for How to Add JSON-LD Structured Data in Next.js for SEO
Ajin Varghese Chandy
Ajin Varghese Chandy

Posted on

How to Add JSON-LD Structured Data in Next.js for SEO

Search engines like Google use structured data (JSON-LD) to better understand your website's content, enabling rich search results. In this tutorial, you'll learn how to implement JSON-LD in a Next.js application using the <Head> component.

Why Use JSON-LD?

  • Helps search engines index your site more effectively.
  • Enables rich snippets (FAQ, breadcrumbs, business info, etc.).
  • Improves visibility and click-through rates in search results.

Step-by-Step Guide to Adding JSON-LD in Next.js

1. Install Next.js (If Not Already Installed)

If you don’t have a Next.js project, create one by running:

npx create-next-app@latest my-nextjs-app
cd my-nextjs-app
npm run dev
Enter fullscreen mode Exit fullscreen mode

2. Add JSON-LD in Your Next.js Page

Next.js provides a built-in <Head> component to manage metadata. We will use this to inject our JSON-LD script dynamically.

**Example: Implementing JSON-LD in **pages/index.tsx

import Head from "next/head";

export default function Home() {
  const jsonLd = {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "WebSite",
        "name": "ared.dev",
        "url": "https://ared.dev",
        "author": {
          "@type": "Person",
          "name": "Ajin Varghese Chandy",
          "url": "https://ared.dev"
        }
      },
      {
        "@type": "WebPage",
        "name": "Ajin Varghese Chandy | Web Developer",
        "description": "Ajin is a web developer proficient in Next.js, Svelte, Node.js, and Express. He has developed multiple websites and Chrome extensions.",
        "url": "https://ared.dev",
        "isPartOf": {
          "@type": "WebSite",
          "url": "https://ared.dev"
        }
      },
      {
        "@type": "Person",
        "name": "Ajin Varghese Chandy",
        "url": "https://ared.dev",
        "sameAs": [
          "https://github.com/aajinn",
          "https://x.com/ChandyAjin"
        ],
        "jobTitle": "Web Developer",
        "worksFor": {
          "@type": "Organization",
          "name": "ared.dev",
          "url": "https://ared.dev"
        },
        "image": {
          "@type": "ImageObject",
          "url": "https://ared.dev/path-to-your-profile-image.jpg"
        }
      }
    ]
  };

  return (
    <>
      <Head>
        <title>ared.dev | Web Developer</title>
        <meta name="description" content="Ajin Varghese Chandy - Web Developer proficient in Next.js, Svelte, Node.js, and Express." />
        <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} />
      </Head>
      <main>
        <h1>Welcome to ared.dev</h1>
      </main>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

3. Explanation of the Code

  • Head** Component**: Used to add metadata inside <head>.
  • JSON-LD Object: Defines structured data for the website.
  • dangerouslySetInnerHTML: Injects JSON-LD as a script tag inside <Head>.
  • @graph** Property**: Contains multiple schema types (WebSite, WebPage, Person) for better SEO.

4. Validate Your JSON-LD Schema

After adding structured data, test it using:

Final Thoughts

By adding JSON-LD structured data in Next.js, you improve SEO and enhance how your site appears in search results. This method ensures your metadata is dynamically added and up-to-date.


Do you have questions or need further improvements? Let me know! 🚀

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay