Skip to content

Commit c03e562

Browse files
authored
fix(gatsby): lower memory pressure in SSR (#30793)
* fix(gatsby): lower memory pressure in SSR * Remove redundant worker pool
1 parent 6263c56 commit c03e562

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

packages/gatsby/src/commands/build.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import db from "../db"
1919
import { store } from "../redux"
2020
import * as appDataUtil from "../utils/app-data"
2121
import { flush as flushPendingPageDataWrites } from "../utils/page-data"
22-
import * as WorkerPool from "../utils/worker/pool"
2322
import {
2423
structureWebpackErrors,
2524
reportWebpackWarnings,
@@ -79,7 +78,7 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
7978
const buildSpan = buildActivity.span
8079
buildSpan.setTag(`directory`, program.directory)
8180

82-
const { gatsbyNodeGraphQLFunction } = await bootstrap({
81+
const { gatsbyNodeGraphQLFunction, workerPool } = await bootstrap({
8382
program,
8483
parentSpan: buildSpan,
8584
})
@@ -137,8 +136,6 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
137136
buildActivityTimer.end()
138137
}
139138

140-
const workerPool = WorkerPool.create()
141-
142139
const webpackCompilationHash = stats.hash
143140
if (
144141
webpackCompilationHash !== store.getState().webpackCompilationHash ||

packages/gatsby/src/utils/worker/render-html.ts

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -312,34 +312,38 @@ export const renderHTMLProd = async ({
312312
}
313313
}
314314

315-
await Bluebird.map(paths, async pagePath => {
316-
try {
317-
const pageData = await readPageData(publicDir, pagePath)
318-
const resourcesForTemplate = await getResourcesForTemplate(pageData)
319-
320-
const { html, unsafeBuiltinsUsage } = htmlComponentRenderer.default({
321-
pagePath,
322-
pageData,
323-
...resourcesForTemplate,
324-
})
325-
326-
if (unsafeBuiltinsUsage.length > 0) {
327-
unsafeBuiltinsUsageByPagePath[pagePath] = unsafeBuiltinsUsage
328-
}
329-
330-
return fs.outputFile(getPageHtmlFilePath(publicDir, pagePath), html)
331-
} catch (e) {
332-
if (e.unsafeBuiltinsUsage && e.unsafeBuiltinsUsage.length > 0) {
333-
unsafeBuiltinsUsageByPagePath[pagePath] = e.unsafeBuiltinsUsage
334-
}
335-
// add some context to error so we can display more helpful message
336-
e.context = {
337-
path: pagePath,
338-
unsafeBuiltinsUsageByPagePath,
315+
await Bluebird.map(
316+
paths,
317+
async pagePath => {
318+
try {
319+
const pageData = await readPageData(publicDir, pagePath)
320+
const resourcesForTemplate = await getResourcesForTemplate(pageData)
321+
322+
const { html, unsafeBuiltinsUsage } = htmlComponentRenderer.default({
323+
pagePath,
324+
pageData,
325+
...resourcesForTemplate,
326+
})
327+
328+
if (unsafeBuiltinsUsage.length > 0) {
329+
unsafeBuiltinsUsageByPagePath[pagePath] = unsafeBuiltinsUsage
330+
}
331+
332+
return fs.outputFile(getPageHtmlFilePath(publicDir, pagePath), html)
333+
} catch (e) {
334+
if (e.unsafeBuiltinsUsage && e.unsafeBuiltinsUsage.length > 0) {
335+
unsafeBuiltinsUsageByPagePath[pagePath] = e.unsafeBuiltinsUsage
336+
}
337+
// add some context to error so we can display more helpful message
338+
e.context = {
339+
path: pagePath,
340+
unsafeBuiltinsUsageByPagePath,
341+
}
342+
throw e
339343
}
340-
throw e
341-
}
342-
})
344+
},
345+
{ concurrency: 2 }
346+
)
343347

344348
return { unsafeBuiltinsUsageByPagePath }
345349
}
@@ -372,18 +376,25 @@ export const renderHTMLDev = async ({
372376
lastSessionId = sessionId
373377
}
374378

375-
return Bluebird.map(paths, async pagePath => {
376-
try {
377-
const htmlString = htmlComponentRenderer.default({
378-
pagePath,
379-
})
380-
return fs.outputFile(getPageHtmlFilePath(outputDir, pagePath), htmlString)
381-
} catch (e) {
382-
// add some context to error so we can display more helpful message
383-
e.context = {
384-
path: pagePath,
379+
return Bluebird.map(
380+
paths,
381+
async pagePath => {
382+
try {
383+
const htmlString = htmlComponentRenderer.default({
384+
pagePath,
385+
})
386+
return fs.outputFile(
387+
getPageHtmlFilePath(outputDir, pagePath),
388+
htmlString
389+
)
390+
} catch (e) {
391+
// add some context to error so we can display more helpful message
392+
e.context = {
393+
path: pagePath,
394+
}
395+
throw e
385396
}
386-
throw e
387-
}
388-
})
397+
},
398+
{ concurrency: 2 }
399+
)
389400
}

0 commit comments

Comments
 (0)