Repository: gatsbyjs/gatsby
File: packages/gatsby-source-drupal/src/gatsby-node.js (JavaScript)

Code before:
    let lastFetched = 0
    if (
      store.getState().status.plugins &&
      store.getState().status.plugins[`gatsby-source-drupal`] &&
      store.getState().status.plugins[`gatsby-source-drupal`].lastFetched
    ) {
      lastFetched = store.getState().status.plugins[`gatsby-source-drupal`]
        .lastFetched
    }

    const drupalFetchIncrementalActivity = reporter.activityTimer(
      `Fetch incremental changes from Drupal`
    )
    let requireFullRebuild = false

    drupalFetchIncrementalActivity.start()

    try {
      // Hit fastbuilds endpoint with the lastFetched date.
      const data = await axios.get(
        `${baseUrl}/gatsby-fastbuilds/sync/${lastFetched}`,
        {
          auth: basicAuth,
          headers,
          params,
        }
      )

      if (data.data.status === -1) {
        reporter.info(`Unable to pull incremental data changes from Drupal`)
        setPluginStatus({ lastFetched: data.data.timestamp })
        requireFullRebuild = true
      } else {
        // Touch nodes so they are not garbage collected by Gatsby.
        const existingNodes = getNodes().filter(
          n => n.internal.owner === `gatsby-source-drupal`
        )
        existingNodes.forEach(n => touchNode({ nodeId: n.id }))

        // Process sync data from Drupal.
        let nodesToSync = data.data.entities
        for (const nodeSyncData of nodesToSync) {
          if (nodeSyncData.action === `delete`) {
            actions.deleteNode({ node: getNode(createNodeId(nodeSyncData.id)) })
          } else {
            // The data could be a single Drupal entity or an array of Drupal
            // entities to update.
            let nodesToUpdate = nodeSyncData.data
            if (!Array.isArray(nodeSyncData.data)) {
              nodesToUpdate = [nodeSyncData.data]
            }

Review comment (bug):
Should teh `lastFetched` indeed be updated if the fetch failed? Or does the `-1` status code mean there simply isn't anything to update?

Code after:
    let lastFetched =
      store.getState().status.plugins?.[`gatsby-source-drupal`]?.lastFetched ??
      0

    const drupalFetchIncrementalActivity = reporter.activityTimer(
      `Fetch incremental changes from Drupal`
    )
    let requireFullRebuild = false

    drupalFetchIncrementalActivity.start()

    try {
      // Hit fastbuilds endpoint with the lastFetched date.
      const data = await axios.get(
        `${baseUrl}/gatsby-fastbuilds/sync/${lastFetched}`,
        {
          auth: basicAuth,
          headers,
          params,
        }
      )

      if (data.data.status === -1) {
        // The incremental data is expired or this is the first fetch.
        reporter.info(`Unable to pull incremental data changes from Drupal`)
        setPluginStatus({ lastFetched: data.data.timestamp })
        requireFullRebuild = true
      } else {
        // Touch nodes so they are not garbage collected by Gatsby.
        getNodes().forEach(node => {
          if (node.internal.owner === `gatsby-source-drupal`) {
            touchNode({ nodeId: node.id })
          }
        })

        // Process sync data from Drupal.
        let nodesToSync = data.data.entities
        for (const nodeSyncData of nodesToSync) {
          if (nodeSyncData.action === `delete`) {
            actions.deleteNode({ node: getNode(createNodeId(nodeSyncData.id)) })
          } else {
            // The data could be a single Drupal entity or an array of Drupal
            // entities to update.
            let nodesToUpdate = nodeSyncData.data
            if (!Array.isArray(nodeSyncData.data)) {
              nodesToUpdate = [nodeSyncData.data]
            }

            for (const nodeToUpdate of nodesToUpdate) {
              await handleWebhookUpdate(
                {