Skip to content

Commit

Permalink
Fix tracking parent nodes when loading redux state from cache (#4013)
Browse files Browse the repository at this point in the history
fixes #2929
  • Loading branch information
pieh authored and KyleAMathews committed Feb 13, 2018
1 parent 932fb92 commit 20299db
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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`)

0 comments on commit 20299db

Please sign in to comment.