Consider this Next.js component that adds JSON-LD structured data to the page.
import Head from 'next/head';
export default function ProductSchema() {
const jsonLd = {
"@context": "https://schema.org",
"@type": "Product",
"name": "Coffee Mug",
"image": "https://example.com/mug.jpg",
"description": "A nice coffee mug",
"sku": "12345"
};
return (
<Head>
<script type="application/ld+json">{JSON.stringify(jsonLd)}</script>
</Head>
);
}What will be the rendered output inside the <head> tag in the browser?
import Head from 'next/head'; export default function ProductSchema() { const jsonLd = { "@context": "https://schema.org", "@type": "Product", "name": "Coffee Mug", "image": "https://example.com/mug.jpg", "description": "A nice coffee mug", "sku": "12345" }; return ( <Head> <script type="application/ld+json">{JSON.stringify(jsonLd)}</script> </Head> ); }
Remember that JSON.stringify converts an object to a JSON string.
The component uses JSON.stringify to convert the JavaScript object into a JSON string. This string is then placed inside the <script> tag with type "application/ld+json". This is the correct way to embed JSON-LD structured data in the HTML head.
Which of the following Next.js component snippets correctly embeds JSON-LD structured data?
Check how JavaScript objects are converted to strings inside JSX.
Option A uses JSON.stringify to convert the object to a JSON string inside JSX curly braces, which is the correct way. Option A tries to put an object literal directly, which is invalid JSX. Option A puts the function call as a string, not evaluated. Option A uses single quotes and object literal syntax inside JSX, which is invalid.
Given this Next.js component:
import Head from 'next/head';
export default function Schema() {
const data = {
"@context": "https://schema.org",
"@type": "Person",
"name": "Alice",
"birthDate": new Date("1990-01-01")
};
return (
<Head>
<script type="application/ld+json">{JSON.stringify(data)}</script>
</Head>
);
}Why might this cause a problem when the page renders?
import Head from 'next/head'; export default function Schema() { const data = { "@context": "https://schema.org", "@type": "Person", "name": "Alice", "birthDate": new Date("1990-01-01") }; return ( <Head> <script type="application/ld+json">{JSON.stringify(data)}</script> </Head> ); }
Check how JSON.stringify handles Date objects.
JSON.stringify converts Date objects to ISO string format automatically. So the birthDate will be serialized as a string like "1990-01-01T00:00:00.000Z". This does not cause an error.
Why do developers add JSON-LD structured data inside the <Head> of Next.js pages?
Think about what structured data is used for in web pages.
JSON-LD provides structured data that search engines use to better understand the content, improving SEO and enabling rich search results. It does not add interactivity, styling, or script loading.
Given this Next.js component that updates JSON-LD based on state:
import { useState, useEffect } from 'react';
import Head from 'next/head';
export default function DynamicSchema() {
const [count, setCount] = useState(1);
useEffect(() => {
const timer = setInterval(() => setCount(c => c + 1), 1000);
return () => clearInterval(timer);
}, []);
const jsonLd = {
"@context": "https://schema.org",
"@type": "Event",
"name": `Event number ${count}`
};
return (
<>
<Head>
<script type="application/ld+json">{JSON.stringify(jsonLd)}</script>
</Head>
<button onClick={() => setCount(count + 1)}>Increment</button>
<p>Current count: {count}</p>
</>
);
}What will happen to the JSON-LD script tag content as the count changes?
import { useState, useEffect } from 'react'; import Head from 'next/head'; export default function DynamicSchema() { const [count, setCount] = useState(1); useEffect(() => { const timer = setInterval(() => setCount(c => c + 1), 1000); return () => clearInterval(timer); }, []); const jsonLd = { "@context": "https://schema.org", "@type": "Event", "name": `Event number ${count}` }; return ( <> <Head> <script type="application/ld+json">{JSON.stringify(jsonLd)}</script> </Head> <button onClick={() => setCount(count + 1)}>Increment</button> <p>Current count: {count}</p> </> ); }
Consider how React state and effects update component output.
The component uses state and effects to update the count every second and on button clicks. The JSON-LD script content is generated from the current count, so it updates dynamically in the head tag accordingly.