Next.js Curriculum
MediumMetadata & SEO100% Free25 min read

Structured Data (JSON-LD) Implementation in Server Components

The 30-Second Interview Answer

Structured Data using schema.org vocabulary in JSON-LD format enables Google to understand page semantics and render rich search results (Rich Snippets, Star Ratings, FAQs, Product Badges). In Next.js App Router, JSON-LD is injected directly inside Server Components using a safe <script type="application/ld+json"> tag containing sanitized JSON.

Learning Objectives

Construct valid schema.org schemas (Course, Product, Article, FAQPage) in TypeScript.

Inject JSON-LD safely into the HTML payload without XSS vulnerabilities.

Explain how structured data enables Google Rich Snippets in search engine results.

Validate JSON-LD payloads using Google’s Rich Results Test tool.

Prerequisites & Background Knowledge
Schema.org vocabularyJSON-LD specificationServer Component rendering

Why Interviewers Care

Crucial for e-commerce, educational platforms, and news sites competing for search snippet real estate.

The Challenge

How is structured data (JSON-LD) implemented in Next.js Server Components? Create a typed Course schema for an educational interview prep lesson that triggers Google Rich Results.

Code Architecture & Implementation

1// app/nextjs/[category]/[slug]/page.tsx
2import { WithContext, Course } from 'schema-dts';
3
4export default async function LessonPage({
5 params,
6}: {
7 params: Promise<{ category: string; slug: string }>;
8}) {
9 const { category, slug } = await params;
10 const lesson = await getLessonData(slug);
11
12 // Strongly typed schema.org object
13 const jsonLd: WithContext<Course> = {
14 '@context': 'https://schema.org',
15 '@type': 'Course',
16 name: lesson.title,
17 description: lesson.interviewAnswer,
18 provider: {
19 '@type': 'Organization',
20 name: 'FAANGPrep',
21 sameAs: 'https://faangprep.com',
22 },
23 hasCourseInstance: {
24 '@type': 'CourseInstance',
25 courseMode: 'online',
26 courseWorkload: `PT${lesson.estimatedMinutes}M`,
27 },
28 };
29
30 return (
31 <article className="max-w-4xl mx-auto py-8">
32 {/*
33 Inject JSON-LD structured data into the HTML payload.
34 Server-rendered so search engine bots parse it immediately!
35 */}
36 <script
37 type="application/ld+json"
38 dangerouslySetInnerHTML={{
39 // Sanitize to prevent script injection in hostile strings
40 __html: JSON.stringify(jsonLd).replace(/</g, '\\u003c'),
41 }}
42 />
43
44 <h1 className="text-3xl font-bold">{lesson.title}</h1>
45 <p className="mt-4">{lesson.interviewAnswer}</p>
46 </article>
47 );
48}

Architectural Explanation

Search crawlers do not execute complex client-side JavaScript to determine structured schemas. Rendering JSON-LD inside Server Components embeds the <script type="application/ld+json"> tag directly in the initial HTML stream. Googlebot parses this on first pass, unlocking rich snippets like breadcrumbs, review stars, pricing, and FAQ accordions directly in search engine results pages (SERPs).

Common Mistakes & Anti-Patterns

Injecting JSON-LD from Client Components after hydration, which some crawlers may miss.

Not escaping `<` characters, creating potential XSS injection vulnerabilities if data contains untrusted strings.

Violating Google Structured Data guidelines by marking up invisible or misleading content.

Interview Follow-Up Traps

The interviewer asks: "Can you put the JSON-LD script inside the page body, or must it be in the <head>?" Google supports JSON-LD anywhere in the document, including `<head>` and `<body>`.

Frequently Asked Questions

What library provides TypeScript types for schema.org?

The community standard is `schema-dts`, maintained by Google engineers.

Yes! You can have multiple scripts (e.g. one for BreadcrumbList, one for Course, one for FAQPage).

Copy the rendered HTML or run Google's Rich Results Test tool (search.google.com/test/rich-results).