2

I'm generating a site with thousands of pages based on the same template using Gatsby JS. To generate all those pages I make some calls to external services to get data to fill them.

My problem is that sometimes those calls fail, but maybe only for one of 1500 pages.

Is it possible to abort the generation of the specific page that failed so it's not generated and I can redeploy the others safely without overriding the one that failed?

I tried using onCreatePage but without luck.

Ruben Aguilar
  • 1,205
  • 11
  • 19

1 Answers1

1

Here is a similar question that I answered before.

Is it possible to abort the generation of the specific page that failed so it's not generated

Yes. You can do that in gatsby-node.js:

const path = require(`path`)
exports.createPages = ({ graphql, actions }) => {
  const { createPage, deletePage } = actions
  const template = path.resolve(`src/templates/template.js`)

  return graphql(`
    // your query
  `, { limit: 10000 }).then(result => {
    if (result.errors) {
      throw result.errors
    }

    result.data.allMarkdownRemark.edges.forEach(edge => {
    // ##### Abort page generation HERE #######
    // Find a graphQL attribute that is undefined or null only when your call fails
    // I use callSuccess as an example. It could be the frontmatter or whatever

    if (edge.callSuccess != null) { // Only generate a page when the call is successful
      createPage({
        path: `${edge.node.frontmatter.slug}`,
        component: template ,
        context: {},
      })
    }
    deletePage(page); // otherwise delete page
  })
}

and I can redeploy the others safely without overriding the one that failed?

There is no easy way for this. Gatsby rebuilds all the pages with every build. I am not aware of retrieving pages of previous builds. Maybe there is a way to query your external service again and retrieve the data this way.

EliteRaceElephant
  • 7,744
  • 4
  • 47
  • 62