Yo Man!

Draft post support in Gatsby how to

November 23, 2019

GatsbyJS together with Gatsby starter blog represent a marvellous publishing tool. It does definetely lack though the out-of-the-box capability of managing and filtering-out blog post with an user defined draft status.

Checking out over the internet you’ll find various approaches to overcome this limitation.

the one suggested by Janosh Riebesell is my favourite. It is pretty straightforward and it consists only in adding a single line of code that leverages on NODE_ENV environmental variable to gatsby-config.js config file:

ignore: process.env.NODE_ENV === `production` && [`**/draft-*`]

Basically it rules out from production build all those filenames with a draft- prefix.

Here you’ll find an excerpt of an edited gatsby-config.js:

module.exports = {

  ...

  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/content/blog`,
        name: `blog`,
        ignore: process.env.NODE_ENV === `production` && [`**/draft-*`]
      },
    },

As a reference, you’ll find the original Janosh Riebesell post here with additional info.


Written with ❤️ by Luca Anceschi
Follow him on Twitter