diff --git a/packages/gatsby/src/redux/index.js b/packages/gatsby/src/redux/index.js index d2c3c09cff1a2..d6401fdb478c8 100644 --- a/packages/gatsby/src/redux/index.js +++ b/packages/gatsby/src/redux/index.js @@ -12,15 +12,34 @@ const emitter = mitt() // Reducers const reducers = require(`./reducers`) +// Parent node tracking +const rootNodeMap = new WeakMap() +const addParentToSubObjects = (data, parentId) => { + if (_.isPlainObject(data) || _.isArray(data)) { + _.each(data, o => addParentToSubObjects(o, parentId)) + rootNodeMap.set(data, parentId) + } +} + +const trackSubObjectsToRootNodeId = node => { + _.each(node, (v, k) => { + // Ignore the node internal object. + if (k === `internal`) { + return + } + addParentToSubObjects(v, node.parent) + }) +} +exports.trackSubObjectsToRootNodeId = trackSubObjectsToRootNodeId + // Read from cache the old node data. let initialState = {} -const rootNodeMap = new WeakMap() try { initialState = JSON.parse( fs.readFileSync(`${process.cwd()}/.cache/redux-state.json`) ) - _.each(initialState.nodes, (id, node) => { + _.each(initialState.nodes, node => { trackSubObjectsToRootNodeId(node) }) } catch (e) { @@ -121,24 +140,6 @@ exports.getNodeAndSavePathDependency = (id, path) => { exports.getRootNodeId = node => rootNodeMap.get(node) -const addParentToSubObjects = (data, parentId) => { - if (_.isPlainObject(data) || _.isArray(data)) { - _.each(data, o => addParentToSubObjects(o, parentId)) - rootNodeMap.set(data, parentId) - } -} - -const trackSubObjectsToRootNodeId = node => { - _.each(node, (v, k) => { - // Ignore the node internal object. - if (k === `internal`) { - return - } - addParentToSubObjects(v, node.parent) - }) -} -exports.trackSubObjectsToRootNodeId = trackSubObjectsToRootNodeId - // Start plugin runner which listens to the store // and invokes Gatsby API based on actions. require(`./plugin-runner`)