Skip to content

Commit 9ffb1fa

Browse files
fabien0102KyleAMathews
authored andcommitted
[1.0] Fix duplicate node (#822)
* Pretty json output (easier to debug) * Don't include contentDigest into node.id It's cause duplication on content update * Deal with contestDisgest changes
1 parent 2097bff commit 9ffb1fa

5 files changed

Lines changed: 19 additions & 8 deletions

File tree

packages/gatsby-source-filesystem/src/gatsby-node.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function readFile(file, pluginOptions, cb) {
1717
// Stringify date objects.
1818
const newFile = JSON.parse(
1919
JSON.stringify({
20-
id: `${slashedFile.absolutePath} >>> ${contentDigest}`,
20+
id: `${slashedFile.absolutePath}`,
2121
contentDigest: contentDigest,
2222
children: [],
2323
parent: `___SOURCE___`,
@@ -44,7 +44,10 @@ function readFile(file, pluginOptions, cb) {
4444
})
4545
}
4646

47-
exports.sourceNodes = ({ boundActionCreators, getNode }, pluginOptions) => {
47+
exports.sourceNodes = (
48+
{ boundActionCreators, getNode, hasNodeChanged },
49+
pluginOptions
50+
) => {
4851
const { createNode, updateSourcePluginStatus } = boundActionCreators
4952
updateSourcePluginStatus({
5053
plugin: `source-filesystem --- ${pluginOptions.name}`,
@@ -66,7 +69,7 @@ exports.sourceNodes = ({ boundActionCreators, getNode }, pluginOptions) => {
6669
// console.log("Added file at", path)
6770
readFile(path, pluginOptions, (err, file) => {
6871
// Only create node if the content digest has changed.
69-
if (!getNode(file.id)) {
72+
if (!getNode(file.id) || hasNodeChanged(file.id, file.contentDigest)) {
7073
createNode(file)
7174
} else {
7275
// console.log("not creating node cause it already exists", file.id)
@@ -77,7 +80,7 @@ exports.sourceNodes = ({ boundActionCreators, getNode }, pluginOptions) => {
7780
console.log("changed file at", path)
7881
readFile(path, pluginOptions, (err, file) => {
7982
// Only create node if the content digest has changed.
80-
if (!getNode(file.id)) {
83+
if (!getNode(file.id) || hasNodeChanged(file.id, file.contentDigest)) {
8184
createNode(file)
8285
}
8386
})

packages/gatsby/lib/redux/actions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from "chalk"
33
const _ = require("lodash")
44
const { bindActionCreators } = require("redux")
55

6-
const { getNode } = require("./index")
6+
const { getNode, hasNodeChanged } = require("./index")
77

88
const { store } = require("./index")
99
import * as joiSchemas from "../joi-schemas/joi"
@@ -66,7 +66,7 @@ actions.createNode = node => {
6666
}
6767

6868
// Check if the node has already been processed.
69-
if (getNode(node.id)) {
69+
if (getNode(node.id) && !hasNodeChanged(node.id, node.contentDigest)) {
7070
console.log("NODE_ALREADY_CREATED", node.pluginName, node.id)
7171
console.log(node)
7272
console.trace()

packages/gatsby/lib/redux/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const Redux = require("redux")
22
const Promise = require("bluebird")
33
const _ = require("lodash")
44
const { composeWithDevTools } = require("remote-redux-devtools")
5-
const apiRunnerNode = require("../utils/api-runner-node")
65
const fs = require("fs")
76

87
// Reducers
@@ -49,6 +48,10 @@ const getNode = id => {
4948
return store.getState().nodes[id]
5049
}
5150
exports.getNode = getNode
51+
exports.hasNodeChanged = (id, digest) => {
52+
const node = store.getState().nodes[id]
53+
return node.contentDigest !== digest
54+
}
5255

5356
exports.loadNodeContent = node => {
5457
if (node.content) {

packages/gatsby/lib/redux/reducers/nodes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const _ = require("lodash")
33

44
const saveState = _.debounce(state => {
55
console.log("===============saving node state")
6-
fs.writeFile(`${process.cwd()}/.cache/node-data.json`, JSON.stringify(state))
6+
fs.writeFile(
7+
`${process.cwd()}/.cache/node-data.json`,
8+
JSON.stringify(state, null, 2)
9+
)
710
}, 1000)
811

912
module.exports = (state = {}, action) => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const runAPI = (plugin, api, args) => {
4040
loadNodeContent,
4141
getNodes,
4242
getNode,
43+
hasNodeChanged,
4344
getNodeAndSavePathDependency,
4445
} = require("../redux")
4546
const { boundActionCreators } = require("../redux/actions")
@@ -66,6 +67,7 @@ const runAPI = (plugin, api, args) => {
6667
store,
6768
getNodes,
6869
getNode,
70+
hasNodeChanged,
6971
getNodeAndSavePathDependency,
7072
cache,
7173
},

0 commit comments

Comments
 (0)