Next.js · Junior · Question 71 of 75

How do you add JSON-LD structured data to a page?

<script
  type="application/ld+json"
  dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd).replace(/</g, '\\u003c') }}
/>

Explanation

Escaping < prevents XSS through the injected JSON. Types from schema-dts help keep the data valid.

Further reading

Problem with a question

Next.js · Junior · question 71

Test yourself

Take the Next.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz