How to schedule blog articles with GatsbyJS<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to schedule blog articles with GatsbyJS

Posted on: October 5, 2021

Coming from writing my blog articles on WordPress, I was rapidly disappointed that I couldn't write down a blog article in advance. Indeed, I could write the article without pushing to the master branch or leverage Git by creating a branch for each article and merge when ready to publish. However, I wanted to be able to write an article, set a date, maybe do some modification here and there, and forget about it. I wanted the article to be deployed automatically.

Each blog article has a header portion called frontmatter. The frontmatter has a date which is the date of the article. Thus, we can program some logic to only display articles before or equal to the frontmatter date.

The filtering needs to appear everywhere we have a list of articles. In my case, I have a list by year and a list that paginate all blog articles chronologically. The former GraphQL looks like this:

1export const query = graphql`
2 query BlogsInYear($yearStart: Date!, $yearEnd: Date!, $currentDate: Date!) {
3 allMdx(
4 sort: { fields: frontmatter___date, order: DESC }
5 filter: {
6 frontmatter: {
7 date: { gte: $yearStart, lt: $yearEnd, lte: $currentDate }
8 }
9 }
10 ) {
11 nodes {
12 frontmatter {
13 date(formatString: "MMMM D, YYYY")
14 title
15 categories
16 }
17 id
18 fields {
19 slug
20 }
21 }
22 }
23 }

And the chronological pagingation:

1export const query = graphql`
2 query TopXBlogArticles($limit: Int!, $skip: Int!, $currentDate: Date!) {
3 allMdx(
4 sort: { fields: frontmatter___date, order: DESC }
5 limit: $limit
6 skip: $skip
7 filter: { frontmatter: { date: { lte: $currentDate } } }
8 ) {
9 nodes {
10 frontmatter {
11 date(formatString: "MMMM D, YYYY")
12 title
13 categories
14 }
15 id
16 fields {
17 slug
18 }
19 }
20 }
21 }

As you can see, in both cases, we pass the $currentDate. Here is the twist if you are new to GatsbyJS: the GraphQL is not dynamic in the client environment but when we build the whole website. Inside the gatsby-node.js, in the createPages function we are parsing the current date. Here is the code for the chronological per year page

1const currentDate = new Date();
2const formattedCurrentDate = currentDate.toISOString().split("T")[0];
4const year =, 4);
5if (!years.has(year)) {
6 const yearStart = year + "-01-01";
7 const yearEnd = year + 1 + "-01-01";
8 createPage({
9 path: URL_PER_YEAR.replace("{year}", year),
10 component: path.resolve(`./src/templates/BlogsByYear.tsx`),
11 context: {
12 yearStart: yearStart,
13 yearEnd: yearEnd,
14 year,
15 totalPages: totalPageCount,
16 currentDate: formattedCurrentDate,
17 },
18 });
19 years.set(year, year);

The last part is that once the code is sent in Github, there is a task to build the website and publish it. But the problem is that if I have an article with a release date in one week, the build needs to be executed on that specific day to have the code in the gatsby-node.js to be executed to pass the currentDate to the GraphQL query.

Thus, we need to alter our build script to trigger a build when the code or article changes and every day. It means that inside the .github/workflows/main.yml the on property changes to

2 push:
3 branches: [master]
4 schedule:
5 - cron: '0 8 * * *'

The schedule uses a cron that executes every day at 8 UTC (which is during the night in the USA the build. So, in short, the whole website is built every day. The following picture shows how the Actions tab looks, and you can see many Scheduled tasks and one commit by me (MrDesjardins), which is when I write and push the MardownX file.

github actions daily

To conclude, the possibility to schedule blog articles with GatsbyJS is possible. Like most features, it is not out-of-the-box but not a very excessive demanding workflow to implement.