Structured data helps search engines understand your webpage better by adding clear information in a special format called JSON-LD.
Structured data (JSON-LD) in NextJS
import Script from 'next/script'; export default function Page() { const jsonLd = { "@context": "https://schema.org", "@type": "Person", "name": "Jane Doe", "jobTitle": "Software Developer", "url": "https://janedoe.com" }; return ( <> <h1>About Jane</h1> <Script id="jsonld-person" type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} /> </> ); }
Use the next/script component to safely add JSON-LD in Next.js pages.
Set type to application/ld+json and use dangerouslySetInnerHTML to insert JSON as a string.
const jsonLd = { "@context": "https://schema.org", "@type": "Product", "name": "Coffee Mug", "offers": { "@type": "Offer", "price": "12.99", "priceCurrency": "USD" } };
const jsonLd = { "@context": "https://schema.org", "@type": "Article", "headline": "How to use JSON-LD", "author": { "@type": "Person", "name": "Jane Doe" }, "datePublished": "2024-06-01" };
This Next.js page shows a simple About section and includes JSON-LD structured data about Jane using the next/script component.
import Script from 'next/script'; export default function AboutPage() { const jsonLd = { "@context": "https://schema.org", "@type": "Person", "name": "Jane Doe", "jobTitle": "Software Developer", "url": "https://janedoe.com" }; return ( <main> <h1>About Jane</h1> <p>Jane is a software developer who loves teaching coding.</p> <Script id="jsonld-person" type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} /> </main> ); }
Always validate your JSON-LD using tools like Google's Rich Results Test to avoid errors.
Keep JSON-LD data up to date with your page content for best SEO results.
Structured data in JSON-LD format helps search engines understand your page content.
In Next.js, use the next/script component to add JSON-LD safely.
This improves your chances of rich search results and better SEO.
Practice
Solution
Step 1: Understand JSON-LD role
JSON-LD is a format for structured data that helps search engines understand the content of a webpage.Step 2: Identify its use in Next.js
In Next.js, JSON-LD is used to improve SEO by providing clear data to search engines, not for styling or animations.Final Answer:
To help search engines understand the page content better -> Option BQuick Check:
JSON-LD purpose = SEO improvement [OK]
- Confusing JSON-LD with CSS or JavaScript animations
- Thinking JSON-LD speeds up server response
- Assuming JSON-LD changes page appearance
Solution
Step 1: Use Next.js Script component
Next.js provides theScriptcomponent fromnext/scriptto safely add scripts including JSON-LD.Step 2: Use dangerouslySetInnerHTML correctly
JSON-LD must be added as a string insidedangerouslySetInnerHTMLto avoid React escaping it.Final Answer:
<Script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }} /> -> Option CQuick Check:
Use Next.js Script + dangerouslySetInnerHTML [OK]
- Using lowercase <script> tag directly in JSX
- Not using dangerouslySetInnerHTML for JSON-LD
- Forgetting to import Script from next/script
import Script from 'next/script';
const data = {"@context": "https://schema.org", "@type": "Person", "name": "Alice"};
export default function Page() {
return (
<>
<h1>Profile</h1>
<Script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }} />
</>
);
}Solution
Step 1: Understand Script output
The Next.jsScriptcomponent renders a<script>tag with the specified type and inner HTML.Step 2: Check JSON-LD content
The JSON-LD data is stringified and inserted as raw JSON inside the script tag with typeapplication/ld+json.Final Answer:
<h1>Profile</h1><script type="application/ld+json">{"@context":"https://schema.org","@type":"Person","name":"Alice"}</script> -> Option DQuick Check:
JSON-LD script tag with stringified data [OK]
- Using wrong script type attribute
- Rendering JSON object instead of string
- Placing JSON inside curly braces in HTML output
import Script from 'next/script';
const data = {"@context": "https://schema.org", "@type": "Organization", "name": "My Company"};
export default function Home() {
return (
<Script type="application/ld+json">{JSON.stringify(data)}</Script>
);
}Solution
Step 1: Check Script usage
TheScriptcomponent requiresdangerouslySetInnerHTMLto insert raw JSON-LD content inside the script tag.Step 2: Identify the error
Passing JSON string as children renders it as text inside the script tag, which is invalid for JSON-LD.Final Answer:
Using <Script> without dangerouslySetInnerHTML causes JSON to render as text, not script content -> Option AQuick Check:
Use dangerouslySetInnerHTML for JSON-LD scripts [OK]
- Passing JSON as children instead of dangerouslySetInnerHTML
- Forgetting to stringify JSON data
- Assuming Script auto-inserts raw JSON
Solution
Step 1: Understand JSON-LD script requirements
Each JSON-LD script tag should contain a single JSON object describing one entity for clarity and correctness.Step 2: Use multiple Script components
In Next.js, you can safely add multipleScriptcomponents each with their own JSON-LD data usingdangerouslySetInnerHTML.Final Answer:
Use two <Script> components each with type="application/ld+json" and dangerouslySetInnerHTML containing separate JSON objects -> Option AQuick Check:
Multiple Script tags with separate JSON-LD objects [OK]
- Putting multiple JSON objects in one script tag without array
- Using arrays inside JSON-LD script which is invalid
- Embedding JSON-LD as JS variables instead of script content
Ready to prove it?
Pass the test and this pattern moves unknown
