Learn how to make JSON-D (application/ld+json) schemas work in Next.js and other ReactJS projects.
To render JSON-LD data in Next.js (or any React app) you need to use:
The < script > element.
The dangerouslySetInnerHTML attribute.
The JSON.stringify method (for serialization).
JSON-LD stands for JavaScript Object Notation for Linked Data. It’s a lightweight method to present Schema.org data to search engines about your website’s content.
Let’s look at an example.
Let’s say you have an HTML JSON-LD schema that is structured similarly to this:
To make this JSON-LD schema work in Next.js, start by removing the HTML <script> tags so that you’re left with a JSON-LD object.
Then assign the JSON-LD object to a variable, like this:
Now you need to serialize your schemaData variable with JSON.stringify():
Finally you add JSON.stringify(schemaData) as an object value of the dangerouslySetInnerHTML attribute inside a < script > element to render it in the browser:
If you’re confused, here’s a complete page example, based on the code in his tutorial, which should work in all Next.js/React websites: