>_Blogging Programmer~/notes since 2016
blogging-programmer › web-development › astro-publishing-scheduled-posts-on-their-day-not-their-hour.md
date: 2026-11-20 topics: [Web Development, Developer Workflow] read: 4 min words: 859

Astro: Publishing Scheduled Posts on Their Day, Not Their Hour

A few days after finishing the series about moving this blog to Astro, I had my first small production bug. A post scheduled for 3 November did not appear on 3 November. Nothing had crashed, the build was green, and the site was up. The post was simply not there.

The cause turned out to be one comparison, and it is a good example of how scheduling works differently on a static site.

How Scheduling Works Without a Server

On WordPress, a scheduled post is a database row with a future date. When the date arrives, WordPress changes the status and the next visitor sees the post. There is always a running server that can make that decision.

A static site has no running server. Every page is an HTML file that was generated at build time. If a post is not in the build, it does not exist on the site.

So scheduling on a static site means two things:

  1. The build must leave out posts whose date is still in the future.
  2. Something must rebuild the site when the date arrives.

The second part is a GitHub Actions workflow with a nightly schedule:

on:
  push:
    branches: [main]
  schedule:
    - cron: '5 0 * * *'   # every night at 00:05 UTC
  workflow_dispatch:

The first part is a filter in the function that loads posts.

The Original Filter

My first version looked like this:

export async function getPosts() {
  const now = new Date();
  const all = await getCollection('posts', ({ data }) => !data.draft && data.date <= now);
  return all.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
}

It reads well: show every post that is not a draft and whose date is not in the future. It also passed every test I did, because I tested with past and far-future dates.

Why the Post Was Missing

The missing post had this front matter:

date: "2026-11-03T09:00:00"

Most of my posts carry a time of 09:00, a habit from my WordPress days. Now follow the nightly build:

  • The build runs at 00:05 UTC on 3 November.
  • now is 2026-11-03T00:05.
  • The post’s date is 2026-11-03T09:00.
  • 09:00 is later than 00:05, so the filter drops the post.

The next build was at 00:05 on 4 November, so the post would only have appeared a day late. Every scheduled post with a time of day would have had the same delay, and I would probably have blamed time zones before finding the real reason.

The bug was not in the code as much as in the assumption behind it. The filter compared exact moments, but I think of scheduling in days. When I write “3 November” in the editor, I mean “on that day”, not “at 09:00 or later”.

The Fix: Compare With the End of Today

The simplest way to express “on that day” is to compare with the end of the current day instead of the current moment:

/** A post goes live on its date, whatever the time of day. */
export async function getPosts() {
  const endOfToday = new Date();
  endOfToday.setUTCHours(23, 59, 59, 999);
  const all = await getCollection('posts', ({ data }) => !data.draft && data.date <= endOfToday);
  return all.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
}

Now the 00:05 build includes everything dated today, regardless of the time written in the front matter.

I used setUTCHours rather than setHours on purpose. The dates in my posts have no time zone, so JavaScript reads them in the build machine’s local time, and GitHub’s build servers run in UTC. Doing the comparison in UTC keeps both sides in the same frame. If I ever build on a machine with a different time zone, the safest option is to set TZ=UTC for the build.

Things I Considered and Rejected

Moving the nightly build to a later hour. Running the build at 09:05 would have fixed this one post, but any post with a later time would still be delayed, and posts would appear in the middle of the day for readers in my part of the world.

Running the build every hour. This would make the exact time meaningful, but it means 24 builds a day to publish two posts a week. It also makes my host fetch a new build after every run.

Stripping the time from all dates. That would work, but it changes 400 files to fix one comparison, and the time is still useful for ordering two posts on the same day.

Changing the meaning of the filter was the smallest change that matched how I actually think about publishing.

A Side Effect Worth Knowing

With this rule, a post dated today but later in the day appears as soon as any build runs. If I push a change at 08:00, a post dated today at 17:00 goes live at 08:00. For a personal blog, that is exactly what I want. For a site where an article must not appear before an embargo time, it would be wrong, and an hourly build with an exact comparison would be the right design.

Lessons

  • On a static site, “published” means “included in the last build”. Scheduling is always the combination of a filter and a build trigger.
  • When a date comparison looks obviously right, check what time each side carries. A date that looks like a day is often a timestamp.
  • Do the comparison in UTC when the build machine’s time zone is outside your control.
  • Test schedules with dates that fall on today, not just the past and the far future.

The fix was one line, and I found the bug only because I was watching for that specific post. That is a reminder to keep an eye on the first few scheduled posts after any change to how a site is built.

Ctrl J