Scheduling posts on a static site

Leo Sjöberg • August 26, 2024

My blog is a static website, built using Jigsaw, and deployed onto Netlify. Recently, I’ve started publishing a lot more posts. However, I don’t write these posts at exactly that cadence. Instead, I tend to write my blog posts when I get motivated, and end up writing several, and then scheduling them.

But with my blog being a static site, there’s no magic way to just have it dynamically update when my blog post is scheduled to go live - so how do I automatically publish my scheduled posts?

Filtering out future posts

This is pretty straightforward. My blog posts are written in markdown, using frontmatter for metadata. Post metadata looks like this:

1---
2extends: _layouts.post
3section: content
4title: "JSON data types in Postgres: json vs jsonb"
5date: 2024-07-23T09:00:00+0100
6categories: [postgres, database]
7description: "Learn when to use json and jsonb data types in Postgres"
8featured: true
9draft: false
10---

My config.production.php has a filter applied to my posts collection:

1'filter' => fn ($page) => !$page->draft && !$page->isFuture(),

This ensures I don’t include any blog posts that aren’t finished yet, or which are scheduled to publish in the future.

Automatically rebuilding after a post is published

Since we can’t dynamically render the site, the solution to this is just to keep re-rendering. In my case, that means running a scheduled GitHub Action which calls Netlify’s build endpoint. My configuration simply makes a POST request using cURL:

1name: Schedule Netlify Build
2on:
3 schedule:
4 - cron: "0 */2 * * *"
5jobs:
6 build:
7 name: Call Netlify build hook
8 runs-on: ubuntu-latest
9 steps:
10 - name: Curl request
11 env:
12 NETLIFY_BUILD_HOOK: ${{ secrets.NETLIFY_BUILD_HOOK }}
13 run: curl -X POST -d {} $NETLIFY_BUILD_HOOK

This will run every other hour, and tell Netlify to rebuild my site. It’s a little wasteful, but it’s simple, and allows me to have scheduled posts with minimal infrastructure.