diff --git a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js index d78350f887821..6c7f720510cb4 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js @@ -113,6 +113,7 @@ exports.onPostBuild = async ( } await Promise.all([ + ensureEmittingFileNodesFinished, buildHeadersProgram(pluginData, pluginOptions), createSiteConfig(pluginData, pluginOptions), createRedirects(pluginData, redirects, rewrites), @@ -156,18 +157,28 @@ const pluginOptionsSchema = function ({ Joi }) { exports.pluginOptionsSchema = pluginOptionsSchema -exports.onPostBootstrap = async ({ getNodesByType }) => { - /** - * Emit via IPC absolute paths to files that should be stored - */ - const fileNodes = getNodesByType(`File`) +/** + * We emit File Nodes via IPC and we need to make sure build doesn't finish before all of + * messages were sent. + */ +let ensureEmittingFileNodesFinished +exports.onPreBootstrap = ({ emitter, getNodesByType }) => { + emitter.on(`API_FINISHED`, action => { + if (action.payload.apiName !== `sourceNodes`) { + return + } - // TODO: This is missing the cacheLocations .cache/caches + .cache/caches-lmdb - let fileNodesEmitted - for (const file of fileNodes) { - fileNodesEmitted = emitFileNodes({ - path: file.absolutePath, - }) - } - await fileNodesEmitted + async function doEmitFileNodes() { + const fileNodes = getNodesByType(`File`) + + // TODO: This is missing the cacheLocations .cache/caches + .cache/caches-lmdb + for (const file of fileNodes) { + await emitFileNodes({ + path: file.absolutePath, + }) + } + } + + ensureEmittingFileNodesFinished = doEmitFileNodes() + }) } diff --git a/packages/gatsby/src/utils/source-nodes.ts b/packages/gatsby/src/utils/source-nodes.ts index 3ec189a4cfe24..322127231d5cc 100644 --- a/packages/gatsby/src/utils/source-nodes.ts +++ b/packages/gatsby/src/utils/source-nodes.ts @@ -112,4 +112,6 @@ export default async ({ warnForPluginsWithoutNodes(state, nodes) deleteStaleNodes(state, nodes) + + store.dispatch(actions.apiFinished({ apiName: `sourceNodes` })) }