📖 Scenario: You are building a simple Next.js page for a local bakery. You want to add structured data using JSON-LD to help search engines understand your business details better.
🎯 Goal: Create a Next.js page that includes JSON-LD structured data for the bakery using a <script> tag inside the <head>. The structured data should describe the bakery's name, address, and opening hours.
📋 What You'll Learn
Create a Next.js page component named BakeryPage
Add a JSON-LD object describing the bakery with these exact details:
name: "Sweet Treats Bakery"
address: "123 Candy Lane, Sugar City, CA 90210"
openingHours: "Mo-Fr 08:00-18:00"
Insert the JSON-LD inside a <script type=\"application/ld+json\"> tag in the <head> using Next.js Head component
💡 Why This Matters
🌍 Real World
Structured data helps search engines understand your website content better, improving SEO and enabling rich search results.
💼 Career
Knowing how to add JSON-LD in Next.js is useful for frontend developers working on SEO optimization and enhancing website visibility.
Progress0 / 4 steps
1
Create the bakery data object
Create a constant called bakeryData with the following JSON-LD structure as a JavaScript object: @context set to "https://schema.org", @type set to "Bakery", name set to "Sweet Treats Bakery", address set to "123 Candy Lane, Sugar City, CA 90210", and openingHours set to "Mo-Fr 08:00-18:00".
NextJS
Hint
Use a JavaScript object with keys exactly as shown. Use double quotes for JSON keys and string values.
2
Import Next.js Head component
Add an import statement at the top of the file to import Head from next/head.
NextJS
Hint
Use the exact import syntax: import Head from "next/head";
3
Create the BakeryPage component with JSON-LD script
Create a React functional component called BakeryPage that returns a fragment containing a <Head> component. Inside <Head>, add a <script> tag with type="application/ld+json" and set its content to the stringified bakeryData object using JSON.stringify(bakeryData).
NextJS
Hint
Use dangerouslySetInnerHTML to insert JSON-LD inside the <script> tag in React.
4
Add a simple page heading inside the component
Inside the BakeryPage component's fragment, below the <Head> tag, add an <h1> element with the text "Welcome to Sweet Treats Bakery".
NextJS
Hint
Place the <h1> inside the fragment but outside the <Head> tag.
Practice
(1/5)
1. What is the main purpose of adding JSON-LD structured data in a Next.js page?
easy
A. To improve server response time
B. To help search engines understand the page content better
C. To add interactive animations to the page
D. To style the page with custom CSS
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 B
Quick Check:
JSON-LD purpose = SEO improvement [OK]
Hint: JSON-LD is for SEO, not styling or animations [OK]
Common Mistakes:
Confusing JSON-LD with CSS or JavaScript animations
Thinking JSON-LD speeds up server response
Assuming JSON-LD changes page appearance
2. Which is the correct way to include JSON-LD structured data in a Next.js component?
easy
A.
B.
C.
D.
Solution
Step 1: Use Next.js Script component
Next.js provides the Script component from next/script to safely add scripts including JSON-LD.
Step 2: Use dangerouslySetInnerHTML correctly
JSON-LD must be added as a string inside dangerouslySetInnerHTML to avoid React escaping it.
Final Answer:
<Script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }} /> -> Option C
Quick Check:
Use Next.js Script + dangerouslySetInnerHTML [OK]
Hint: Use Next.js Script with dangerouslySetInnerHTML for JSON-LD [OK]
Common Mistakes:
Using lowercase <script> tag directly in JSX
Not using dangerouslySetInnerHTML for JSON-LD
Forgetting to import Script from next/script
3. Given this Next.js code snippet, what will be the rendered output in the HTML?