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 " 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:
- One block with
@graphholds every type for the page, so there's a single place to check. - Posts are pages tagged
posts, as in the eleventy-base-blog starter. Set the tag in a directory data file (blog/blog.json:{"tags": ["posts"]}) and change the check if your collection has another name. - Dates come from
page.date(front matterdate, else the file's creation date) as full ISO 8601. Setdatein front matter: file dates change when a site is cloned on a build server.updatedis an optional front matter key for the modified date. - Breadcrumbs follow the URL folders. Folder names are used as labels unless you give a nicer one in
crumbs, e.g."crumbs": {"blog": "Blog"}in the same directory data file. Every intermediate URL must be a real page; if/blog/doesn't exist, leave that level out. The last item, the page itself, leaves outitem, which Google accepts. - Optional fields (description, author, image) are only added when the front matter has them, so a post never claims an empty author.
- URLs are absolute, built from
metadata.url. Relative URLs in JSON-LD are a common reason Google ignores an image. - A template that needs something different can set its own
eleventyComputed.schemain front matter or a.11tydata.jsfile; data closer to the template wins.
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
- Hand-written JSON around
{{ title }}. A quote in a title or a trailing comma makes the whole block invalid, and Google ignores a block it can't parse. | dumpwithout| safe, or| safewithout escaping. The first breaks the JSON, the second lets a title end the script tag.- Dates formatted for people, like "November 10, 2026". Use ISO 8601.
- Two plugins or layouts each writing a
BlogPostingfor the same page. Search your_includesforld+jsonbefore adding this. - Markup for content that isn't on the page. Authors, ratings and prices must also be visible.
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.