🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
SEO••By Komal Nakrani

Astro Article Schema: BlogPosting JSON-LD for Posts

Add Astro article schema to blog posts: a BlogPosting JSON-LD component fed by your content collection, the fields Google uses, safe escaping and validation.

#Astro#SEO#Article Schema#JSON-LD

To add Article schema in Astro, render a <script type="application/ld+json"> in your blog post layout with set:html={JSON.stringify(schema)}, where schema is a BlogPosting object built from the post’s frontmatter: headline, datePublished, dateModified, author, image and the page URL. Because the data comes from your content collection, every post gets correct, consistent structured data with no manual JSON.

Key Takeaways

  • Use BlogPosting for blog posts and Article or NewsArticle for other article types.
  • Google has no required properties for Article markup, but headline, dates, author and image matter most.
  • Render JSON-LD with set:html and escape < so post content cannot break out of the script tag.
  • Validate with Google’s Rich Results Test and the Schema Markup Validator.

The component

---
// src/components/ArticleSchema.astro
interface Props {
  title: string;
  description: string;
  pubDate: Date;
  updatedDate?: Date;
  author: { name: string; url?: string };
  image?: string;
}

const { title, description, pubDate, updatedDate, author, image } = Astro.props;
const url = new URL(Astro.url.pathname, Astro.site).href;

const schema = {
  '@context': 'https://schema.org',
  '@type': 'BlogPosting',
  headline: title,
  description,
  datePublished: pubDate.toISOString(),
  dateModified: (updatedDate ?? pubDate).toISOString(),
  author: { '@type': 'Person', name: author.name, ...(author.url && { url: author.url }) },
  ...(image && { image: [new URL(image, Astro.site).href] }),
  mainEntityOfPage: { '@type': 'WebPage', '@id': url },
  publisher: { '@type': 'Organization', name: 'Example Studio', url: Astro.site?.href },
};

const json = JSON.stringify(schema).replace(/</g, '\\u003c');
---
<script type="application/ld+json" set:html={json} />

set:html writes the string without HTML-escaping it, which JSON-LD needs. Replacing < with < keeps a title containing </script> from closing the tag early, and JSON parsers read < as < again. Astro does not process scripts with a type other than a JavaScript module, so the JSON stays inline and untouched.

Use it in the post layout

---
// src/layouts/BlogPost.astro
import type { CollectionEntry } from 'astro:content';
import ArticleSchema from '../components/ArticleSchema.astro';

interface Props {
  post: CollectionEntry<'blog'>;
}

const { post } = Astro.props;
const { title, description, pubDate, updatedDate, image } = post.data;
---
<html lang="en">
  <head>
    <title>{title}</title>
    <ArticleSchema
      title={title}
      description={description}
      pubDate={pubDate}
      updatedDate={updatedDate}
      author={{ name: 'Sam Rivera', url: 'https://www.example.com/about/' }}
      image={image}
    />
  </head>
  <body><slot /></body>
</html>

If you have several writers, store authors in a collection and use reference('authors') in the post schema, then pass the resolved author’s name and profile URL. An author url that points to a real profile page helps search engines connect the article to a person.

Which fields matter

Property Notes
headline The post title; keep it close to the visible <h1>
datePublished / dateModified ISO 8601 with time zone; update dateModified only for real edits
author Person or Organization with name, ideally url
image Absolute URLs; Google recommends several aspect ratios at least 1200 px wide
mainEntityOfPage Ties the article to its URL

The values must match what is visible on the page. A dateModified newer than the date you show, or an author not named on the page, can stop Google from using the markup.

Validate it

After deploying, run the page through Google’s Rich Results Test and the Schema Markup Validator. Locally, build and check that each post has exactly one block:

# Terminal
npm run build && grep -c 'application/ld+json' dist/blog/*/index.html

To combine Article with Organization, WebSite and BreadcrumbList in one @graph, see our JSON-LD guide. The content docs show where post frontmatter lives in a theme from our catalog, and the meta tags component guide covers the rest of the head.

FAQ

Should I use Article or BlogPosting schema for an Astro blog?

BlogPosting is a subtype of Article meant for blog posts, and Google treats both the same for article features. Use BlogPosting on posts and Article for guides or documentation-style pages if you prefer.

Does Article schema give a rich result in Google?

It helps Google understand the title, dates, author and images, which can be used in Top stories, Discover and image previews. It does not guarantee a special result for every page.

Can I generate Article schema for MDX posts too?

Yes. The component only reads frontmatter, so it works for Markdown and MDX entries in the same collection.

Why use set:html for JSON-LD in Astro?

Astro escapes expressions in HTML by default, which would turn quotes into entities and break the JSON. set:html inserts the JSON string as is.