From 3aa1481e2a623639696288c31f7c1a5c8af10a63 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Tue, 13 Feb 2018 18:36:19 +0100 Subject: [PATCH] Fix tracking parent nodes when loading redux state from cache fixes #2929 --- packages/gatsby/src/redux/index.js | 41 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 20 deletions(-) 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`)