diff --git a/packages/gatsby/src/redux/actions.js b/packages/gatsby/src/redux/actions.js index 31c5fa83f2c25..55050dcc79806 100644 --- a/packages/gatsby/src/redux/actions.js +++ b/packages/gatsby/src/redux/actions.js @@ -11,7 +11,7 @@ const { joinPath } = require(`../utils/path`) const { getNode, hasNodeChanged, - trackSubObjectsToRootNodeId, + trackInlineObjectsInRootNode, } = require(`./index`) const { store } = require(`./index`) import * as joiSchemas from "../joi-schemas/joi" @@ -484,7 +484,7 @@ actions.createNode = (node: any, plugin?: Plugin, traceId?: string) => { ) } - trackSubObjectsToRootNodeId(node) + trackInlineObjectsInRootNode(node) const oldNode = getNode(node.id) diff --git a/packages/gatsby/src/redux/index.js b/packages/gatsby/src/redux/index.js index d6401fdb478c8..64d6fce0d09ce 100644 --- a/packages/gatsby/src/redux/index.js +++ b/packages/gatsby/src/redux/index.js @@ -12,25 +12,45 @@ const emitter = mitt() // Reducers const reducers = require(`./reducers`) -// Parent node tracking +// Root node tracking + +/** + * Map containing links between inline objects or arrays + * and Node that contains them + * @type {Object.<(Object|Array),string>} + */ const rootNodeMap = new WeakMap() -const addParentToSubObjects = (data, parentId) => { + +/** + * Add link between passed data and Node. This function shouldn't be used + * directly. Use higher level `trackInlineObjectsInRootNode` + * @see trackInlineObjectsInRootNode + * @param {(Object|Array)} data Inline object or array + * @param {string} nodeId Id of node that contains data passed in first parameter + */ +const addRootNodeToInlineObject = (data, nodeId) => { if (_.isPlainObject(data) || _.isArray(data)) { - _.each(data, o => addParentToSubObjects(o, parentId)) - rootNodeMap.set(data, parentId) + _.each(data, o => addRootNodeToInlineObject(o, nodeId)) + rootNodeMap.set(data, nodeId) } } -const trackSubObjectsToRootNodeId = node => { +/** + * Adds link between inline objects/arrays contained in Node object + * and that Node object. + * @param {Node} node Root Node + */ +const trackInlineObjectsInRootNode = node => { _.each(node, (v, k) => { // Ignore the node internal object. if (k === `internal`) { return } - addParentToSubObjects(v, node.parent) + addRootNodeToInlineObject(v, node.id) }) + return node } -exports.trackSubObjectsToRootNodeId = trackSubObjectsToRootNodeId +exports.trackInlineObjectsInRootNode = trackInlineObjectsInRootNode // Read from cache the old node data. let initialState = {} @@ -40,7 +60,7 @@ try { ) _.each(initialState.nodes, node => { - trackSubObjectsToRootNodeId(node) + trackInlineObjectsInRootNode(node) }) } catch (e) { // ignore errors. diff --git a/packages/gatsby/src/schema/run-sift.js b/packages/gatsby/src/schema/run-sift.js index 9b3119630975a..b6f498913cc9d 100644 --- a/packages/gatsby/src/schema/run-sift.js +++ b/packages/gatsby/src/schema/run-sift.js @@ -5,6 +5,7 @@ const { connectionFromArray } = require(`graphql-skip-limit`) const { createPageDependency } = require(`../redux/actions/add-page-dependency`) const prepareRegex = require(`./prepare-regex`) const Promise = require(`bluebird`) +const { trackInlineObjectsInRootNode } = require(`../redux`) function awaitSiftField(fields, node, k) { const field = fields[k] @@ -109,6 +110,7 @@ module.exports = ({ return Promise.all( nodes.map(node => resolveRecursive(node, fieldsToSift, type.getFields())) ).then(myNodes => { + myNodes = myNodes.map(trackInlineObjectsInRootNode) if (!connection) { const index = _.isEmpty(siftArgs) ? 0