Skip to content

Commit

Permalink
fix(gatsby): lower memory pressure in SSR (#30793) (#30851)
Browse files Browse the repository at this point in the history
* fix(gatsby): lower memory pressure in SSR

* Remove redundant worker pool

(cherry picked from commit c03e562)

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
  • Loading branch information
GatsbyJS Bot and vladar authored Apr 13, 2021
1 parent 96805d5 commit f561724
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
5 changes: 1 addition & 4 deletions packages/gatsby/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,7 +78,7 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
const buildSpan = buildActivity.span
buildSpan.setTag(`directory`, program.directory)

const { gatsbyNodeGraphQLFunction } = await bootstrap({
const { gatsbyNodeGraphQLFunction, workerPool } = await bootstrap({
program,
parentSpan: buildSpan,
})
Expand Down Expand Up @@ -137,8 +136,6 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
buildActivityTimer.end()
}

const workerPool = WorkerPool.create()

const webpackCompilationHash = stats.hash
if (
webpackCompilationHash !== store.getState().webpackCompilationHash ||
Expand Down
91 changes: 51 additions & 40 deletions packages/gatsby/src/utils/worker/render-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 }
)
}

0 comments on commit f561724

Please sign in to comment.