diff --git a/packages/gatsby-source-contentful/src/__tests__/gatsby-node.js b/packages/gatsby-source-contentful/src/__tests__/gatsby-node.js index 7505f1eb0dbc7..381cb5db9dcb5 100644 --- a/packages/gatsby-source-contentful/src/__tests__/gatsby-node.js +++ b/packages/gatsby-source-contentful/src/__tests__/gatsby-node.js @@ -640,7 +640,7 @@ describe(`gatsby-node`, () => { expect(actions.createNode).toHaveBeenCalledTimes(46) expect(actions.deleteNode).toHaveBeenCalledTimes(0) - expect(actions.touchNode).toHaveBeenCalledTimes(32) + expect(actions.touchNode).toHaveBeenCalledTimes(0) expect(reporter.info.mock.calls).toMatchInlineSnapshot(` Array [ Array [ @@ -730,7 +730,7 @@ describe(`gatsby-node`, () => { expect(actions.createNode).toHaveBeenCalledTimes(54) expect(actions.deleteNode).toHaveBeenCalledTimes(0) - expect(actions.touchNode).toHaveBeenCalledTimes(72) + expect(actions.touchNode).toHaveBeenCalledTimes(0) expect(reporter.info.mock.calls).toMatchInlineSnapshot(` Array [ Array [ @@ -883,7 +883,7 @@ describe(`gatsby-node`, () => { expect(actions.createNode).toHaveBeenCalledTimes(52) expect(actions.deleteNode).toHaveBeenCalledTimes(2) - expect(actions.touchNode).toHaveBeenCalledTimes(74) + expect(actions.touchNode).toHaveBeenCalledTimes(2) expect(reporter.info.mock.calls).toMatchInlineSnapshot(` Array [ Array [ @@ -969,7 +969,7 @@ describe(`gatsby-node`, () => { expect(actions.createNode).toHaveBeenCalledTimes(54) expect(actions.deleteNode).toHaveBeenCalledTimes(2) - expect(actions.touchNode).toHaveBeenCalledTimes(74) + expect(actions.touchNode).toHaveBeenCalledTimes(2) expect(reporter.info.mock.calls).toMatchInlineSnapshot(` Array [ Array [ diff --git a/packages/gatsby-source-contentful/src/source-nodes.js b/packages/gatsby-source-contentful/src/source-nodes.js index be473a0c50e7a..5b9927de5cf82 100644 --- a/packages/gatsby-source-contentful/src/source-nodes.js +++ b/packages/gatsby-source-contentful/src/source-nodes.js @@ -40,6 +40,7 @@ const CONTENT_DIGEST_COUNTER_SEPARATOR = `_COUNT_` * or the fallback field or the default field. */ +let isFirstSource = true export async function sourceNodes( { actions, @@ -58,16 +59,22 @@ export async function sourceNodes( actions const online = await isOnline() - getNodes().forEach(node => { - if (node.internal.owner !== `gatsby-source-contentful`) { - return - } - touchNode(node) - if (node?.fields?.localFile) { - // Prevent GraphQL type inference from crashing on this property - touchNode(getNode(node.fields.localFile)) - } - }) + // Gatsby only checks if a node has been touched on the first sourcing. + // As iterating and touching nodes can grow quite expensive on larger sites with + // 1000s of nodes, we'll skip doing this on subsequent sources. + if (isFirstSource) { + getNodes().forEach(node => { + if (node.internal.owner !== `gatsby-source-contentful`) { + return + } + touchNode(node) + if (node?.fields?.localFile) { + // Prevent GraphQL type inference from crashing on this property + touchNode(getNode(node.fields.localFile)) + } + }) + isFirstSource = false + } if ( !online &&