From f5617243a454e9655717e0adca50066bdcf643e4 Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Tue, 13 Apr 2021 12:06:34 -0400 Subject: [PATCH] fix(gatsby): lower memory pressure in SSR (#30793) (#30851) * fix(gatsby): lower memory pressure in SSR * Remove redundant worker pool (cherry picked from commit c03e562e3b7493c1903e710f80c24857b01267b6) Co-authored-by: Vladimir Razuvaev --- packages/gatsby/src/commands/build.ts | 5 +- .../gatsby/src/utils/worker/render-html.ts | 91 +++++++++++-------- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/packages/gatsby/src/commands/build.ts b/packages/gatsby/src/commands/build.ts index 30ab3b6f370de..3d99a04f8b517 100644 --- a/packages/gatsby/src/commands/build.ts +++ b/packages/gatsby/src/commands/build.ts @@ -19,7 +19,6 @@ import db from "../db" import { store } from "../redux" import * as appDataUtil from "../utils/app-data" import { flush as flushPendingPageDataWrites } from "../utils/page-data" -import * as WorkerPool from "../utils/worker/pool" import { structureWebpackErrors, reportWebpackWarnings, @@ -79,7 +78,7 @@ module.exports = async function build(program: IBuildArgs): Promise { const buildSpan = buildActivity.span buildSpan.setTag(`directory`, program.directory) - const { gatsbyNodeGraphQLFunction } = await bootstrap({ + const { gatsbyNodeGraphQLFunction, workerPool } = await bootstrap({ program, parentSpan: buildSpan, }) @@ -137,8 +136,6 @@ module.exports = async function build(program: IBuildArgs): Promise { buildActivityTimer.end() } - const workerPool = WorkerPool.create() - const webpackCompilationHash = stats.hash if ( webpackCompilationHash !== store.getState().webpackCompilationHash || diff --git a/packages/gatsby/src/utils/worker/render-html.ts b/packages/gatsby/src/utils/worker/render-html.ts index 32521efe3f5ac..6ceb59f0af362 100644 --- a/packages/gatsby/src/utils/worker/render-html.ts +++ b/packages/gatsby/src/utils/worker/render-html.ts @@ -312,34 +312,38 @@ export const renderHTMLProd = async ({ } } - await Bluebird.map(paths, async pagePath => { - try { - const pageData = await readPageData(publicDir, pagePath) - const resourcesForTemplate = await getResourcesForTemplate(pageData) - - const { html, unsafeBuiltinsUsage } = htmlComponentRenderer.default({ - pagePath, - pageData, - ...resourcesForTemplate, - }) - - if (unsafeBuiltinsUsage.length > 0) { - unsafeBuiltinsUsageByPagePath[pagePath] = unsafeBuiltinsUsage - } - - return fs.outputFile(getPageHtmlFilePath(publicDir, pagePath), html) - } catch (e) { - if (e.unsafeBuiltinsUsage && e.unsafeBuiltinsUsage.length > 0) { - unsafeBuiltinsUsageByPagePath[pagePath] = e.unsafeBuiltinsUsage - } - // add some context to error so we can display more helpful message - e.context = { - path: pagePath, - unsafeBuiltinsUsageByPagePath, + await Bluebird.map( + paths, + async pagePath => { + try { + const pageData = await readPageData(publicDir, pagePath) + const resourcesForTemplate = await getResourcesForTemplate(pageData) + + const { html, unsafeBuiltinsUsage } = htmlComponentRenderer.default({ + pagePath, + pageData, + ...resourcesForTemplate, + }) + + if (unsafeBuiltinsUsage.length > 0) { + unsafeBuiltinsUsageByPagePath[pagePath] = unsafeBuiltinsUsage + } + + return fs.outputFile(getPageHtmlFilePath(publicDir, pagePath), html) + } catch (e) { + if (e.unsafeBuiltinsUsage && e.unsafeBuiltinsUsage.length > 0) { + unsafeBuiltinsUsageByPagePath[pagePath] = e.unsafeBuiltinsUsage + } + // add some context to error so we can display more helpful message + e.context = { + path: pagePath, + unsafeBuiltinsUsageByPagePath, + } + throw e } - throw e - } - }) + }, + { concurrency: 2 } + ) return { unsafeBuiltinsUsageByPagePath } } @@ -372,18 +376,25 @@ export const renderHTMLDev = async ({ lastSessionId = sessionId } - return Bluebird.map(paths, async pagePath => { - try { - const htmlString = htmlComponentRenderer.default({ - pagePath, - }) - return fs.outputFile(getPageHtmlFilePath(outputDir, pagePath), htmlString) - } catch (e) { - // add some context to error so we can display more helpful message - e.context = { - path: pagePath, + return Bluebird.map( + paths, + async pagePath => { + try { + const htmlString = htmlComponentRenderer.default({ + pagePath, + }) + return fs.outputFile( + getPageHtmlFilePath(outputDir, pagePath), + htmlString + ) + } catch (e) { + // add some context to error so we can display more helpful message + e.context = { + path: pagePath, + } + throw e } - throw e - } - }) + }, + { concurrency: 2 } + ) }