Skip to content

Commit

Permalink
feat(gatsby-source-contentful): skip expensive touchNode step when un…
Browse files Browse the repository at this point in the history
…necessary (#36134)

* feat(gatsby-source-contentful): skip expensive touchNode step when unnecessary

* update tests
  • Loading branch information
KyleAMathews authored Jul 18, 2022
1 parent 9a58b36 commit ecc7530
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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 [
Expand Down
27 changes: 17 additions & 10 deletions packages/gatsby-source-contentful/src/source-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 &&
Expand Down

0 comments on commit ecc7530

Please sign in to comment.