Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Define a JSON-LD Schema in Next.js
📖 Scenario: You are building a simple Next.js website for a local bakery. You want to help search engines understand your site better by adding structured data using JSON-LD schema.
🎯 Goal: Create a JSON-LD schema object describing the bakery and add it to the Next.js page using a <script> tag with the correct type.
📋 What You'll Learn
Create a JavaScript object with bakery details following schema.org's Bakery type
Add a variable for bakery name, address, and opening hours
Convert the object to a JSON string
Insert the JSON-LD inside a <script type="application/ld+json"> tag in the Next.js page
💡 Why This Matters
🌍 Real World
Adding JSON-LD schema helps search engines understand your website content better, improving SEO and rich search results.
💼 Career
Web developers often add structured data to websites to improve visibility and accessibility in search engines.
Progress0 / 4 steps
1
Create the bakery data object
Create a constant called bakerySchema that is an object with these exact properties: @context set to "https://schema.org", @type set to "Bakery", name set to "Sweet Treats Bakery", and address set to an object with streetAddress as "123 Cookie Lane", addressLocality as "Caketown", and postalCode as "12345".
NextJS
Hint
Use an object literal with nested objects for the address.
2
Add opening hours to the bakery schema
Add a property called openingHours to the bakerySchema object. Set it to an array with these exact strings: "Mo-Fr 08:00-18:00" and "Sa 09:00-14:00".
NextJS
Hint
Use an array of strings for the openingHours property.
3
Convert the bakery schema object to a JSON string
Create a constant called jsonLd and set it to the JSON string version of bakerySchema using JSON.stringify().
NextJS
Hint
Use JSON.stringify() to convert the object to a string.
4
Add the JSON-LD script tag to the Next.js page
In the default exported React component, return a fragment containing a <script> tag with type="application/ld+json" and the JSON-LD string inside using dangerouslySetInnerHTML with the key __html set to jsonLd.
NextJS
Hint
Use a React fragment and the dangerouslySetInnerHTML prop to insert the JSON-LD string.
Practice
(1/5)
1. What is the main purpose of schema definition in a Next.js project?
easy
A. To manage server-side rendering
B. To describe the shape and rules of your data
C. To style the user interface components
D. To handle routing between pages
Solution
Step 1: Understand schema definition role
Schema definition is about specifying how data should look and behave.
Step 2: Identify its main use in Next.js
It helps validate data to catch errors early before using it in the app.
Final Answer:
To describe the shape and rules of your data -> Option B
Quick Check:
Schema = Data shape and rules [OK]
Hint: Schema defines data shape and validation rules [OK]
Common Mistakes:
Confusing schema with UI styling
Thinking schema manages routing
Assuming schema handles rendering
2. Which of the following is the correct way to define a simple string schema using the zod library in Next.js?
easy
A. const schema = z.string();
B. const schema = z.string;
C. const schema = z.String();
D. const schema = new z.string();
Solution
Step 1: Recall zod syntax for string schema
In zod, string schema is created by calling z.string() as a function.
Step 2: Check each option's syntax
const schema = z.string(); uses z.string() correctly. Others miss parentheses or use wrong casing or new keyword.
Final Answer:
const schema = z.string(); -> Option A
Quick Check:
z.string() is correct syntax [OK]
Hint: Use parentheses to call z.string() function [OK]
'id' uses z.number without parentheses, which is incorrect syntax.
Step 2: Verify other properties and object structure
Other properties are correct; object schema is correctly defined as an object, not array.
Final Answer:
Missing parentheses after z.number for 'id' -> Option D
Quick Check:
z.number() needs parentheses [OK]
Hint: Always call zod types as functions with () [OK]
Common Mistakes:
Forgetting parentheses after z.number or z.string
Thinking schema must be an array
Missing commas between properties
5. You want to define a schema for a user profile in Next.js using zod where the 'email' field is optional but if present must be a valid email string. Which schema definition is correct?
hard
A. const userProfileSchema = z.object({ email: z.string().email().optional() });
B. const userProfileSchema = z.object({ email: z.optional(z.string().email) });
C. const userProfileSchema = z.object({ email: z.string().optional().email() });
D. const userProfileSchema = z.object({ email: z.string().email() || undefined });
Solution
Step 1: Understand optional email schema in zod
To make a field optional but validate if present, use .optional() after .email().