Social Metadata and Structured Data

Intermediate · 10 min

How Open Graph and Twitter card tags shape link previews, how JSON-LD describes a page to search engines, and the escaping bug that breaks a block.

Before thisResource Hints, Preload, Prefetch and PreconnectMeta Tags and the ViewportArticle, Section and the Outline

ProgrammingPercy · 10 min

Social Metadata and Structured Data

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by ProgrammingPercyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • When someone pastes your URL into a chat or social app, the app fetches the page and reads `<meta property="og:...">` tags from the head to build the preview card. Without them it guesses, usually badly.
  • The core set is `og:title`, `og:description`, `og:image`, `og:url` and `og:type`, plus `twitter:card` for the large-image layout. Use an absolute URL for the image.
  • Structured data describes what a page is, in a vocabulary from schema.org. JSON-LD in a `<script type="application/ld+json">` block is the usual format.
  • A JSON-LD block is data, not code. In a test it did not execute, raised no errors even when its JSON was broken, and could be read with `JSON.parse(script.textContent)`.
  • A literal `</script>` inside a JSON string closes the block early. In a test the rest of the data leaked onto the page as text; escape `<` as `\u003c` when generating JSON-LD.

Two audiences for the same metadata

The <head> of a page is read by more than browsers. Chat apps, social networks and search engines all fetch pages and read them without ever displaying them as a person would. Two kinds of tag speak to them: Open Graph tags tell social apps how to draw a preview card, and structured data tells search engines what the page is.

Open Graph and Twitter cards

When a link is pasted into a chat message, the app sends a request for that URL, reads the HTML, and looks for tags it understands. Open Graph, originally from Facebook, is the common vocabulary:

<meta property="og:title" content="Resource hints explained">
<meta property="og:description" content="Preload, prefetch and preconnect in practice.">
<meta property="og:image" content="https://example.com/img/hints.png">
<meta property="og:url" content="https://example.com/hints">
<meta property="og:type" content="article">
<meta name="twitter:card" content="summary_large_image">

Note the attributes: Open Graph uses property, Twitter’s tags use name. og:title and og:description can differ from <title> and the meta description, so you can write copy that suits sharing. og:image needs an absolute URL, because the crawler reads the tag out of context and has no page address to resolve a relative path from. twitter:card with summary_large_image asks for the large preview image layout, and many platforms fall back to Open Graph for everything else.

Two practical points. First, the crawler does not run JavaScript on most platforms, so the tags have to be in the HTML the server sends, not added later by a script; with a client-rendered app this is what server-side rendering fixes. Second, platforms cache previews, so after a change you need their debugger tool to refresh it. I could not test what any platform does with the tags; that behavior is from documentation, not from a run.

Structured data and JSON-LD

A page is text for people. For a machine, “4.5” next to some stars could be anything. Structured data labels it: this is an Article, with this author and this date; this is a Product with this price. The vocabulary lives at schema.org, and the most common format is JSON-LD, a block of JSON in a script tag:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "Resource hints explained",
  "datePublished": "2026-11-22",
  "author": { "@type": "Person", "name": "A. Writer" }
}
</script>

Search engines can use this for rich results, such as review stars, recipe cards and event listings, though using the markup does not guarantee them. Match the markup to the visible content: describing things the page does not show can get the markup ignored. The SEO basics lesson covers how it fits with titles and links, and article, section and the outline covers the visible structure the data should agree with.

A script block that is not code

Because the type is not JavaScript, the browser does not run the block. It stays in the DOM as text. I checked in headless Chromium, Firefox and WebKit: a valid block was readable with JSON.parse(document.getElementById("ok").textContent) and gave Article, no page errors were raised, and a block with broken JSON ({not json) raised none either, though JSON.parse on its text failed. The browser does not tell you your JSON-LD is wrong; a validator will. For reading and writing JSON in code see JSON and serialization.

The </script> trap

The HTML parser looks for the literal text </script> to end a script block, and it does so before any JSON is parsed. In a test, a block whose JSON string contained </script><p id="leak">... ended at that point. The block’s textContent was cut to {"headline":"a, and the remainder appeared on the page as a real <p> element, in all three browsers. If you generate JSON-LD from user-supplied text, this is both a broken-data bug and an injection hole. Serialize with JSON.stringify and replace < with < in the output. The HTML security lesson covers escaping more broadly.

What the video covers

The embed is the whole of ProgrammingPercy’s “The Open Graph Protocol - Why And How You Should Use it On Your Website” (December 2022). The chapter list covers what the Open Graph protocol is and how to use it, the Open Graph meta tags, an Open Graph website, Twitter and alternate tags, and debugging Facebook and Twitter cards. It does not cover structured data. That half is the lesson’s own, and the runner-up, a Google Search Central talk by a Google engineer (August 2022), covers what structured data is, how to add it and best practices. The video is from a small developer channel; the description says it was made while rebuilding a SvelteKit site, so some of it may be framework-specific. I could not read a transcript. The JSON-LD results above were tested in a browser; the Open Graph and rich-result behavior was not.

Where this fits

This closes the head group, after resource hints. Next the course moves on to accessibility, beginning with the accessibility tree.

Check yourself

  1. 01You share a blog post link and the preview shows no image and the wrong title. What do you add to the page?

    Open Graph tags in the `<head>`: `og:title`, `og:description` and `og:image` with an absolute URL, plus `og:url` and `og:type`. Add `twitter:card` set to `summary_large_image` for the big-image card. The crawler does not run your JavaScript, so the tags must be in the served HTML.

  2. 02Why does `og:image` need an absolute URL?

    The crawler reads the tag outside of your page, with no base URL to resolve a relative path against, so `/img/cover.png` would point nowhere. Write the full `https://example.com/img/cover.png`.

  3. 03A JSON-LD block is created by inserting a user's article title into a template. What can go wrong?

    If the title contains `</script>`, the HTML parser ends the block there, and the rest of the JSON becomes visible page content. In a test this left the block's text cut off and the remainder as an element in the page. Serialize with `JSON.stringify` and escape `<` as `\u003c` before inserting.

  4. 04Does a broken JSON-LD block show an error in the page or console?

    No. In a test it raised no page error. The browser treats the block as inert text, and only a consumer that parses it, such as a search engine's validator, will complain. Check it with a structured data testing tool.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark