Skip to content

Commit

Permalink
feat(gatsby): Cache resolved nodes in develop (#11522)
Browse files Browse the repository at this point in the history
feat(gatsby): Cache resolved nodes in develop
  • Loading branch information
stefanprobst authored and freiksenet committed Feb 7, 2019
1 parent b493ba4 commit 55e3425
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/src/redux/reducers/index.js
Expand Up @@ -24,6 +24,7 @@ module.exports = {
program: require(`./program`),
nodes: getNodesReducer(),
nodesByType: require(`./nodes-by-type`),
resolvedNodesCache: require(`./resolved-nodes`),
nodesTouched: require(`./nodes-touched`),
lastAction: require(`./last-action`),
plugins: require(`./plugins`),
Expand Down
18 changes: 18 additions & 0 deletions packages/gatsby/src/redux/reducers/resolved-nodes.js
@@ -0,0 +1,18 @@
module.exports = (state = new Map(), action) => {
switch (action.type) {
case `DELETE_CACHE`:
case `CREATE_NODE`:
case `DELETE_NODE`:
case `DELETE_NODES`:
return new Map()

case `SET_RESOLVED_NODES`: {
const { key, nodes } = action.payload
state.set(key, nodes)
return state
}

default:
return state
}
}
16 changes: 10 additions & 6 deletions packages/gatsby/src/redux/run-sift.js
Expand Up @@ -5,8 +5,8 @@ const prepareRegex = require(`../utils/prepare-regex`)
const Promise = require(`bluebird`)
const { trackInlineObjectsInRootNode } = require(`../db/node-tracking`)
const { getNode, getNodesByType } = require(`../db/nodes`)
const { store } = require(`.`)

const resolvedNodesCache = new Map()
const enhancedNodeCache = new Map()
const enhancedNodePromiseCache = new Map()
const enhancedNodeCacheId = ({ node, args }) =>
Expand Down Expand Up @@ -169,6 +169,7 @@ function resolveRecursive(node, siftFieldsObj, gqFields) {
}

function resolveNodes(nodes, typeName, firstOnly, fieldsToSift, gqlFields) {
const { resolvedNodesCache } = store.getState()
const nodesCacheKey = JSON.stringify({
// typeName + count being the same is a pretty good
// indication that the nodes are the same.
Expand All @@ -177,10 +178,7 @@ function resolveNodes(nodes, typeName, firstOnly, fieldsToSift, gqlFields) {
nodesLength: nodes.length,
...fieldsToSift,
})
if (
process.env.NODE_ENV === `production` &&
resolvedNodesCache.has(nodesCacheKey)
) {
if (resolvedNodesCache.has(nodesCacheKey)) {
return Promise.resolve(resolvedNodesCache.get(nodesCacheKey))
} else {
return Promise.all(
Expand Down Expand Up @@ -208,7 +206,13 @@ function resolveNodes(nodes, typeName, firstOnly, fieldsToSift, gqlFields) {
return enhancedNodeGenerationPromise
})
).then(resolvedNodes => {
resolvedNodesCache.set(nodesCacheKey, resolvedNodes)
store.dispatch({
type: `SET_RESOLVED_NODES`,
payload: {
key: nodesCacheKey,
nodes: resolvedNodes,
},
})
return resolvedNodes
})
}
Expand Down

0 comments on commit 55e3425

Please sign in to comment.