Astro has no built-in draft flag for content collections, so you add one: put draft: z.boolean().default(false) in the collection schema, then filter with getCollection('blog', ({ data }) => !data.draft). To preview drafts locally but keep them out of the live site, filter only when import.meta.env.PROD is true, and route every page through one helper.
Key Takeaways
- Drafts are a schema field plus a filter; nothing else is needed.
- Default
drafttofalseso existing posts stay published.- Filter in a single helper, so the index, post pages, tags and RSS all agree.
- A draft filtered out of
getStaticPaths()gets no page, so it never reaches the sitemap.- The Markdown file is still in your Git repository; drafts are hidden, not private.
Add the field to the schema
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
description: z.string(),
pubDate: z.coerce.date(),
draft: z.boolean().default(false),
}),
});
export const collections = { blog };
Mark a post as a draft in its frontmatter:
---
title: "Notes on our redesign"
description: "Work in progress."
pubDate: 2026-11-10
draft: true
---
Filter drafts in one helper
Put the rule in one place and import it everywhere you list or build posts:
// src/lib/posts.ts
import { getCollection } from 'astro:content';
export async function getPosts() {
return getCollection('blog', ({ data }) =>
import.meta.env.PROD ? !data.draft : true,
);
}
import.meta.env.PROD is true during astro build and false during astro dev, so drafts show while you write and disappear from the deployed site. If you never want drafts in dev either, use ({ data }) => !data.draft.
Use the helper in the post route:
---
// src/pages/blog/[...slug].astro
import { render } from 'astro:content';
import { getPosts } from '../../lib/posts';
export async function getStaticPaths() {
const posts = await getPosts();
return posts.map((post) => ({ params: { slug: post.id }, props: { post } }));
}
const { post } = Astro.props;
const { Content } = await render(post);
---
{post.data.draft && <p class="draft-badge">Draft: not visible on the live site</p>}
<h1>{post.data.title}</h1>
<Content />
The badge only ever renders in dev, because drafts are filtered out of production builds before a page is created.
Check every place that lists posts
A draft that leaks usually leaks through a page that calls getCollection() directly instead of the helper. Search for direct calls:
# Terminal
grep -rn "getCollection('blog'" src/
Replace each with getPosts(), including the RSS endpoint, tag pages, related posts and any homepage “latest posts” section. The RSS docs show the feed endpoint, and our tags and categories guide builds tag pages from the same list.
The sitemap integration reads the pages Astro actually built. A draft with no page is simply absent, so there is nothing to exclude in @astrojs/sitemap.
Drafts are not secret
The Markdown file is still in your repository, and in a public repository anyone can read it. Keep anything confidential out of public repos until it is ready. If you build preview deployments for review, note that they run astro build too, so drafts are hidden there as well; to show them, gate on your own variable instead:
// src/lib/posts.ts
import { getCollection } from 'astro:content';
const showDrafts = import.meta.env.PUBLIC_SHOW_DRAFTS === 'true';
export async function getPosts() {
return getCollection('blog', ({ data }) => showDrafts || !data.draft);
}
Set PUBLIC_SHOW_DRAFTS=true only in the preview environment’s settings. For publishing on a future date rather than by hand, see scheduled posts in Astro.
Many blog themes in our Blog & Editorial category already include a draft field; check src/content.config.ts before adding your own. The content collections docs and the official content collections guide cover schemas in depth.
FAQ
Does Astro support draft posts?
Not as a built-in content collection option. Add a draft boolean to your schema and filter it out with the getCollection() filter callback. It takes a few lines and works for Markdown and MDX.
How do I show Astro drafts only in development?
Filter with import.meta.env.PROD ? !data.draft : true. During astro dev that returns every post; during astro build it removes drafts.
Will Astro draft posts appear in the sitemap?
No, as long as every route that builds post pages uses the draft filter. The sitemap only lists pages that were built.
Why is my draft post still on the live site?
Some page calls getCollection('blog') without the filter, or the build ran with your preview variable set. Search the project for direct getCollection calls and switch them to the shared helper.