Skip to content

Commit 1364453

Browse files
pvdzgatsbybot
andauthored
chore(gatsby): convert loadNodeContent to async and cache it (#25195)
* chore(gatsby): convert func with promise to async * No need to wrap this in a promise anymore * Need to await because using the resulting value locally Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
1 parent 50854fb commit 1364453

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

packages/gatsby/src/db/nodes.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,32 @@ interface NodeStore {
2323
/**
2424
* Get content for a node from the plugin that created it.
2525
*
26-
* @param {Object} node
27-
* @returns {promise}
26+
* @param {IGatsbyNode} node
27+
* @returns {Promise<string>}
2828
*/
29-
function loadNodeContent(node) {
29+
async function loadNodeContent(node) {
3030
if (_.isString(node.internal.content)) {
31-
return Promise.resolve(node.internal.content)
32-
} else {
33-
return new Promise(resolve => {
34-
// Load plugin's loader function
35-
const plugin = store
36-
.getState()
37-
.flattenedPlugins.find(plug => plug.name === node.internal.owner)
38-
const { loadNodeContent } = require(plugin.resolve)
39-
if (!loadNodeContent) {
40-
throw new Error(
41-
`Could not find function loadNodeContent for plugin ${plugin.name}`
42-
)
43-
}
44-
45-
return loadNodeContent(node).then(content => {
46-
// TODO update node's content field here.
47-
resolve(content)
48-
})
49-
})
31+
return node.internal.content
5032
}
33+
34+
// Load plugin's loader function
35+
const plugin = store
36+
.getState()
37+
.flattenedPlugins.find(plug => plug.name === node.internal.owner)
38+
39+
const { loadNodeContent } = require(plugin.resolve)
40+
41+
if (!loadNodeContent) {
42+
throw new Error(
43+
`Could not find function loadNodeContent for plugin ${plugin.name}`
44+
)
45+
}
46+
47+
const content = await loadNodeContent(node)
48+
49+
node.internal.content = content
50+
51+
return content
5152
}
5253

5354
module.exports = {

0 commit comments

Comments
 (0)