Skip to content

Commit 7dfd52d

Browse files
KyleAMathewswardpeetpieh
authored
feat(gatsby): Exclude the internal/fields objects for greater consistency creating contentDigest (#33671)
* feat(gatsby-core-utils): Exclude the internal object for greater consistency I was puzzled why a site was having more nodes than expected by marked as changed on each data update. I use jest-diff to show the diff between the nodes and as it turns out, it was all trivial differences in the `internal` object. Since the `internal` object is managed by the framework and source/transformer plugins typically don't ensure that the `internal` object hasn't changed between updates — we should just exclude it when generating the digest. Making this change resulted in ~20 less nodes being changed which reduced the number of invalidated queries from ~40k to ~6k, dramatically speeding up the incremental builds. I tested whether the destructuring would slow things down any by running the ~700k nodes on this site through both the old and new version of the site. They were essentially the same — ~16.5s vs. ~16.9s with the new algorithm being faster, presumably as we don't now have to hash the internal object. I expect this will result in fewer changed nodes in a lot of plugins. * Update create-content-digest.ts * Only hash as node if node & update snapshots * Only remove autogenerated fields from internal object * Move node-specific createContentDigest code to api-runner-node * revert snapshot changes * update snapshots * Update packages/gatsby/src/utils/api-runner-node.js Co-authored-by: Michal Piechowiak <[email protected]> Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 0cd90d9 commit 7dfd52d

File tree

8 files changed

+38
-13
lines changed

8 files changed

+38
-13
lines changed

integration-tests/cache-resilience/plugins/source-and-transformers-child-nodes/source-changed/scenario.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ const nodesTest = ({
5757
- \\"foo\\": \\"run-1\\",
5858
\\"id\\": \\"parent_parentChangeForTransformer\\",
5959
\\"internal\\": Object {
60-
- \\"contentDigest\\": \\"9d6d458358c77dbe8f4247752ebe41f0\\",
61-
+ \\"contentDigest\\": \\"3021b9f76357d1cffb3c40fabc9e08fb\\",
60+
- \\"contentDigest\\": \\"dc3d69faa290879f166b8cf2a459e123\\",
61+
+ \\"contentDigest\\": \\"5b2b719234b7823a8a7743409714b7a7\\",
6262
\\"owner\\": \\"source-and-transformers-child-nodes/source-changed/gatsby-source\\",
6363
\\"type\\": \\"Parent_ParentChangeForTransformer\\",
6464
},
@@ -75,8 +75,8 @@ const nodesTest = ({
7575
+ \\"foo\\": undefined,
7676
\\"id\\": \\"parent_parentChangeForTransformer >>> Child\\",
7777
\\"internal\\": Object {
78-
- \\"contentDigest\\": \\"603e50c1fe96279688538ab046d1d70a\\",
79-
+ \\"contentDigest\\": \\"f784f9722081b56fee8ca34708299a37\\",
78+
- \\"contentDigest\\": \\"7ec9e8b4f83ebca01990608f961b1a5d\\",
79+
+ \\"contentDigest\\": \\"4cecf7ebaf0c19b22eef8835c725c2e0\\",
8080
\\"owner\\": \\"source-and-transformers-child-nodes/source-changed/gatsby-transformer\\",
8181
\\"type\\": \\"ChildOfParent_ParentChangeForTransformer\\",
8282
},

integration-tests/cache-resilience/plugins/source-and-transformers-child-nodes/transformer-added/scenario.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const nodesTest = ({
5555
\\"foo\\": \\"run-1\\",
5656
\\"id\\": \\"parent_childAdditionForTransformer\\",
5757
\\"internal\\": Object {
58-
\\"contentDigest\\": \\"f85e860f002547e9da9e893e3e44e162\\",
58+
\\"contentDigest\\": \\"f99c0539a6bb6061b072fe782a8e441f\\",
5959
\\"owner\\": \\"source-and-transformers-child-nodes/transformer-added/gatsby-source\\",
6060
\\"type\\": \\"Parent_ChildAdditionForTransformer\\",
6161
},

integration-tests/cache-resilience/plugins/source-and-transformers-child-nodes/transformer-changed/scenario.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ const nodesTest = ({
7878
+ \\"foo\\": \\"baz\\",
7979
\\"id\\": \\"parent_childChangeForTransformer >>> Child\\",
8080
\\"internal\\": Object {
81-
- \\"contentDigest\\": \\"25ad44d8db6f7cee17e248d3b4c94538\\",
82-
+ \\"contentDigest\\": \\"3c57fc662e03b2b443b47575f7e82ec0\\",
81+
- \\"contentDigest\\": \\"04b03439f5e1d249d999e6763e23f306\\",
82+
+ \\"contentDigest\\": \\"66875f4e1170801ce795d9d863a12b2a\\",
8383
\\"owner\\": \\"source-and-transformers-child-nodes/transformer-changed/gatsby-transformer\\",
8484
\\"type\\": \\"ChildOfParent_ChildChangeForTransformer\\",
8585
},

integration-tests/cache-resilience/plugins/source-and-transformers-child-nodes/transformer-removed/scenario.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const nodesTest = ({
8585
\\"foo\\": \\"run-1\\",
8686
\\"id\\": \\"parent_childDeletionForTransformer\\",
8787
\\"internal\\": Object {
88-
\\"contentDigest\\": \\"872081fdfb66891ee6ccdcd13716a5ce\\",
88+
\\"contentDigest\\": \\"6140f27e9454bda710ae6da2cda698f0\\",
8989
\\"owner\\": \\"source-and-transformers-child-nodes/transformer-removed/gatsby-source\\",
9090
\\"type\\": \\"Parent_ChildDeletionForTransformer\\",
9191
},

integration-tests/cache-resilience/plugins/source-and-transformers-node-fields/source-changed/scenario.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ const nodesTest = ({
4949
- \\"foo\\": \\"run-1\\",
5050
\\"id\\": \\"parent_parentChangeForFields\\",
5151
\\"internal\\": Object {
52-
- \\"contentDigest\\": \\"e88540d53597617cf99d612601037013\\",
53-
+ \\"contentDigest\\": \\"3b78e62e87d3f1d8e92d274aa8dbe548\\",
52+
- \\"contentDigest\\": \\"bde8140cd815ea2bb9e7ae2b45330d08\\",
53+
+ \\"contentDigest\\": \\"76dc03ca64a18a99d1581b9e7dd224ef\\",
5454
\\"fieldOwners\\": Object {
5555
\\"bar\\": \\"source-and-transformers-node-fields/source-changed/gatsby-transformer\\",
5656
\\"foo\\": \\"source-and-transformers-node-fields/source-changed/gatsby-transformer\\",

integration-tests/cache-resilience/plugins/source-and-transformers-node-fields/transformer-added/scenario.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const nodesTest = ({
5050
\\"foo\\": \\"run-1\\",
5151
\\"id\\": \\"parent_childAdditionForFields\\",
5252
\\"internal\\": Object {
53-
\\"contentDigest\\": \\"bdf44fdce30b104b4f290d66c2dc3ca1\\",
53+
\\"contentDigest\\": \\"b6d6df9ccdfc5388cde03e3140520cf5\\",
5454
+ \\"fieldOwners\\": Object {
5555
+ \\"foo1\\": \\"source-and-transformers-node-fields/transformer-added/gatsby-transformer\\",
5656
+ },

integration-tests/cache-resilience/plugins/source-and-transformers-node-fields/transformer-changed/scenario.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const nodesTest = ({
6767
\\"foo\\": \\"run-1\\",
6868
\\"id\\": \\"parent_childChangeForFields\\",
6969
\\"internal\\": Object {
70-
\\"contentDigest\\": \\"fb9e9b9c26522bceaa1f51c537b2aff2\\",
70+
\\"contentDigest\\": \\"c6c5c686a949351323ba4a04b585840a\\",
7171
\\"fieldOwners\\": Object {
7272
- \\"foo1\\": \\"source-and-transformers-node-fields/transformer-changed/gatsby-transformer\\",
7373
+ \\"foo2\\": \\"source-and-transformers-node-fields/transformer-changed/gatsby-transformer\\",

packages/gatsby/src/utils/api-runner-node.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const { codeFrameColumns } = require(`@babel/code-frame`)
1313
const fs = require(`fs-extra`)
1414
const { getCache } = require(`./get-cache`)
1515
import { createNodeId } from "./create-node-id"
16-
const { createContentDigest } = require(`gatsby-core-utils`)
16+
const {
17+
createContentDigest: _createContentDigest,
18+
} = require(`gatsby-core-utils`)
1719
import {
1820
buildObjectType,
1921
buildUnionType,
@@ -32,6 +34,29 @@ const { trackBuildError, decorateEvent } = require(`gatsby-telemetry`)
3234
import errorParser from "./api-runner-error-parser"
3335
import { wrapNode, wrapNodes } from "./detect-node-mutations"
3436

37+
// Override createContentDigest to remove autogenerated data from nodes to
38+
// ensure consistent digests.
39+
function createContentDigest(node) {
40+
if (!node?.internal?.type) {
41+
// this doesn't look like a node, so let's just pass it as-is
42+
return _createContentDigest(node)
43+
}
44+
return _createContentDigest({
45+
...node,
46+
internal: {
47+
...node.internal,
48+
// Remove auto-generated fields that'd prevent
49+
// creating a consistent contentDigest.
50+
contentDigest: undefined,
51+
owner: undefined,
52+
fieldOwners: undefined,
53+
ignoreType: undefined,
54+
counter: undefined,
55+
},
56+
fields: undefined,
57+
})
58+
}
59+
3560
if (!process.env.BLUEBIRD_DEBUG && !process.env.BLUEBIRD_LONG_STACK_TRACES) {
3661
// Unless specified - disable longStackTraces
3762
// as this have severe perf penalty ( http://bluebirdjs.com/docs/api/promise.longstacktraces.html )

0 commit comments

Comments
 (0)