Updated 2026-11-15

Eleventy JSON-LD: structured data from computed data

Eleventy writes nothing into your <head> that you don't put there, and that includes structured data. Google recommends JSON-LD for it (Google Search Central: structured data). In Eleventy the cleanest way is to build the data as a plain JavaScript object in computed data, where every page's front matter and the global data are available, and print it once in your base layout. This guide gives working code for posts, breadcrumbs and the home page, tested on Eleventy 3.1. Which types fit which pages is covered in JSON-LD for Astro and Docusaurus; the data is the same in Eleventy.

1. A filter that escapes for a script tag

JSON.stringify gives valid JSON, but a title containing </script> would still end the tag early. Add a filter that escapes <, > and & as Unicode escapes, which JSON parsers read back as the same characters:

export default function (eleventyConfig) {
  // JSON for a <script> tag: escape < > & so "</script>" in a title can't end it early
  eleventyConfig.addFilter("jsonLd", (data) =>
    JSON.stringify(data)
      .replace(/</g, "\\u003c")
      .replace(/>/g, "\\u003e")
      .replace(/&/g, "\\u0026")
  );
}

The escaped output contains no HTML special characters, so it's safe to print with | safe. Don't skip the filter and print | dump instead: Nunjucks then HTML-escapes the quotes as &quot; and the block is no longer JSON.

2. Build the data in global computed data

A file named _data/eleventyComputed.js adds computed data to every page (computed data). It runs after the rest of the data cascade, so it can read title, description, tags and page. This version reads the site name and URL from _data/metadata.json ({"title": "…", "url": "https://example.com"}), the same file the Eleventy SEO guide uses:

export default {
  schema: (data) => {
    const { metadata, page } = data;
    if (!page?.url) return null; // permalink: false pages
    const abs = (path) => new URL(path, metadata.url).href;
    const graph = [];

    // Home page: the site itself
    if (page.url === "/") {
      graph.push({ "@type": "WebSite", name: metadata.title, url: abs("/") });
    }

    // Posts: an article with dates, description and image
    if (data.tags?.includes("posts")) {
      const post = {
        "@type": "BlogPosting",
        headline: data.title,
        datePublished: page.date.toISOString(),
        dateModified: (data.updated ? new Date(data.updated) : page.date).toISOString(),
        mainEntityOfPage: abs(page.url),
      };
      if (data.description) post.description = data.description;
      if (data.author) post.author = { "@type": "Person", name: data.author };
      if (data.image) post.image = abs(data.image);
      graph.push(post);
    }

    // Every page below the home page: a breadcrumb trail from the URL folders
    if (page.url !== "/") {
      const parts = page.url.split("/").filter(Boolean);
      const items = [{ "@type": "ListItem", position: 1, name: metadata.title, item: abs("/") }];
      parts.forEach((part, i) => {
        const last = i === parts.length - 1;
        const item = {
          "@type": "ListItem",
          position: i + 2,
          name: last ? data.title : (data.crumbs?.[part] ?? part),
        };
        if (!last) item.item = abs("/" + parts.slice(0, i + 1).join("/") + "/");
        items.push(item);
      });
      graph.push({ "@type": "BreadcrumbList", itemListElement: items });
    }

    return graph.length ? { "@context": "https://schema.org", "@graph": graph } : null;
  },
};

Notes on what it does:

3. Print it in the base layout

In _includes/base.njk, inside <head>:

{% if schema %}<script type="application/ld+json">{{ schema | jsonLd | safe }}</script>{% endif %}

In a Liquid layout it's {{ schema | jsonLd }} inside the same {% if schema %}; Liquid doesn't escape output by default. On a post, the result looks like this (shortened):

<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"BlogPosting","headline":"Why \"quotes\" \u0026 \u003c/script\u003e break hand-written JSON","datePublished":"2026-11-10T00:00:00.000Z",…},{"@type":"BreadcrumbList",…}]}</script>

4. Check the build

List built pages with no JSON-LD at all:

npx @11ty/eleventy && grep -rL --include='*.html' 'application/ld+json' _site

Then check a few pages of each layout. The free structured data checker takes a URL or pasted markup and checks each block against the properties Google requires and recommends. Google's Rich Results Test shows which rich results a page qualifies for. The free site checker samples pages across a live site and shows which have no structured data, no description or no share image.

Common mistakes

Automating it with Headlark

Computed data covers the page types you write it for. On a site with hundreds of different pages, Headlark's build step runs after Eleventy and writes what each page is missing from its content: one to three JSON-LD blocks with only properties whose values appear on the page, plus missing descriptions, Open Graph tags and alt text. A type you already emit is kept as it is and never added twice, so it works alongside the code above.

Try it

Try it free for 14 days on one site and up to 500 pages, no card needed. Add your site, copy its secret key, and your next build ships with SEO.