Draft and preview flags for blog posts

Seedling: See all stagesPlanted Last tended

What it is

Two front matter flags for blog posts on my portfolio site: draft hides a post everywhere, and preview shows it on my machine only, in the real layout, before anyone else can see it.

Status: seedling. Planned and filed as an issue in my portfolio repo, not built yet.

Why I'm building it

The only way I could see a finished post was to push it live or to modify its date. That meant typos, broken images and layout bugs showed up in production first or irritation to me. I wanted a safe place to read a post exactly as it will look, before anyone else can.

There are two different stages of "not ready":

  • Draft: I'm still writing it. I don't want to see it rendered anywhere yet.
  • Preview: the writing is done and I want to review it in the real layout, with real fonts, real images, the index and tags, but only on my own machine or a setup of my choosing. I don't want anyone else to see it yet. ( I may need to add a shared secret to the URL for a team preview, but that's a separate feature.)

One flag can't express both, so I added two.

Stack

  • Astro 5 content collections
  • Zod (frontmatter schema)
  • Vitest (unit tests for the visibility rule)
  • Playwright (checking the pages that do and don't exist)

How it works, in plain words

Frontmatter On my machine (astro dev) Live site (astro build)
draft: true Hidden Hidden
preview: true Shown, with a preview banner Hidden
neither Shown Published

If both flags are set, draft wins. Publishing is just deleting the flag.

What I learned

Each of these has a card in the RolodexRolodex
This page is a living index of terms, concepts, and ideas I've encountered and actually understood. It's not a glossary of definitions I've copied; each entry is written in my own words once someth...
.

1. The schema is the contract

Content collections validate every post's frontmatter with Zod. Adding draft: z.boolean().default(false) means old posts that never had the field still work, because the default fills it in.

Lesson: schema defaults let you add new fields without touching every existing file.

2. import.meta.env.PROD is decided at build time

Astro replaces import.meta.env.PROD with a literal true or false when it builds. It's false under astro dev and true under astro build. So the live site isn't hiding previews at runtime. The preview pages are never generated at all.

Lesson: build-time checks are safer than runtime checks for keeping things private, because there's nothing to leak.

3. getStaticPaths decides which pages exist

In a static Astro site, a page only exists if getStaticPaths returns it. Filtering posts there means hidden posts have no URL in production. Guessing the slug just gives a 404.

4. One helper, one source of truth

The post route, blog index, tags, RSS and sitemap all list posts. If each one filtered on its own, I'd eventually forget one and leak a draft into the RSS feed. So every page calls a single getVisiblePosts() helper.

Lesson: when the same rule applies in many places, put it in one function. That's a single source of truth.

5. Design for testing

The helper isVisible(data, isProd) takes isProd as an argument that defaults to import.meta.env.PROD. That lets a Vitest test pass true or false directly and check all four flag combinations without running a build.

Lesson: inject the thing you'd otherwise have to fake.

What I'd tell a junior dev

  • Name the stages in your workflow before writing code. The two-flag design came from noticing that draft and preview are different jobs.
  • Use the framework's documented pattern first. The Astro docs already show a dev-only draft filter, and I extended it instead of inventing my own.
  • Ship then polish. Deploy previews on Netlify and a pre-publish checklist are follow-ups, not blockers.

Check yourself to see if you understand

  1. A post has draft: true and preview: true. Does it show in dev? Why not?
  2. Why is filtering in getStaticPaths safer than hiding a post with CSS or a runtime if?
  3. What breaks if the RSS feed calls getCollection('blog') directly instead of the helper?
  4. Why does isVisible take isProd as a parameter?