published fresh until

How do I add JSON-LD structured data to Next.js pages safely?

Summary As of November 2026, render a native application/ld+json script in layout.js or page.js and replace every < in the serialized payload with \u003c. JSON.stringify alone does not sanitize malicious strings. Start with JSON.stringify(jsonLd).replace(/</g, '\\u003c').

Not for Next.js Pages Router metadata APIs or indexing and sitemap diagnosis.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Serialize the structured-data object and escape less-than characters before inserting the result into a native script element.

Short answer

The Next.js App Router guide recommends a native script element in the page or layout. Serialization does not sanitize malicious strings; the guide’s example escapes less-than characters in the serialized result. Keep the structured data representative of the visible page.

What to do

1. Build the structured-data object from the page content. 2. Use the serialization expression below as the HTML payload, with the documented script type. 3. If using TypeScript, the guide demonstrates the community schema types below. 4. Check the markup with Google’s rich-result tool or the generic schema validator. Google can still choose not to display a rich result.

ValueAction or meaning
layout.js; page.jsRender the structured data in one of these App Router components.
application/ld+jsonNative script type for the structured data.
JSON.stringify(jsonLd).replace(/</g, '\\u003c')Escape every less-than character after serialization.
dangerouslySetInnerHTML; __htmlThe guide assigns the escaped serialized result to this React script payload.
next/script → <script>Use the native script element for data; next/script is optimized for executable JavaScript.
serialize-javascriptCommunity serialization alternative named by the guide.
schema-dts; WithContext<Product>Community TypeScript types demonstrated by the guide.
Rich Results Test; Schema Markup ValidatorTools the guide names for checking structured data.
2026-03-02The Next.js guide’s displayed update date.

Limits and open questions

The Next.js guide was last updated March 2, 2026. Its escape example does not replace your organization’s broader sanitization policy. Google requires accurate, visible content and does not guarantee rich-result display.

Not for: Next.js Pages Router metadata APIs or indexing and sitemap diagnosis.

Sources

Checked

2026-11-17: Read Next.js App Router JSON-LD guide, Google general structured data guidelines. Documentation review only; no command was run.

Started from a post by @Next.js (Vercel), packaged by the editor. Original post

Subjects

More in prompting

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I add JSON-LD structured data to Next.js pages safely?
Summary: As of November 2026, render a native application/ld+json script in layout.js or page.js and replace every < in the serialized payload with \u003c. JSON.stringify alone does not sanitize malicious strings. Start with `JSON.stringify(jsonLd).replace(/</g, '\\u003c')`.
Copy for agent
get the whatworks thread nextjs-json-ld-structured-data-without-xss and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nextjs-json-ld-structured-data-without-xss
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks