Skip to content

Commit

Permalink
add experimental option for shared worker pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 10, 2021
1 parent cba88fd commit 614bf80
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 161 deletions.
32 changes: 20 additions & 12 deletions packages/next/build/index.ts
Expand Up @@ -91,8 +91,6 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { NextConfigComplete } from '../server/config-shared'

const staticWorker = require.resolve('./worker')

export type SsgRoute = {
initialRevalidateSeconds: number | false
srcRoute: string | null
Expand Down Expand Up @@ -671,6 +669,10 @@ export default async function build(
) as BuildManifest

const timeout = config.experimental.staticPageGenerationTimeout || 0
const sharedPool = config.experimental.sharedPool || false
const staticWorker = sharedPool
? require.resolve('./worker')
: require.resolve('./utils')
let infoPrinted = false
const staticWorkers = new Worker(staticWorker, {
timeout: timeout * 1000,
Expand Down Expand Up @@ -705,12 +707,14 @@ export default async function build(
},
numWorkers: config.experimental.cpus,
enableWorkerThreads: config.experimental.workerThreads,
exposedMethods: [
'hasCustomGetInitialProps',
'isPageStatic',
'getNamedExports',
'exportPage',
],
exposedMethods: sharedPool
? [
'hasCustomGetInitialProps',
'isPageStatic',
'getNamedExports',
'exportPage',
]
: ['hasCustomGetInitialProps', 'isPageStatic', 'getNamedExports'],
}) as Worker &
Pick<
typeof import('./worker'),
Expand Down Expand Up @@ -1107,10 +1111,14 @@ export default async function build(
pages: combinedPages,
outdir: path.join(distDir, 'export'),
statusMessage: 'Generating static pages',
exportPageWorker: staticWorkers.exportPage.bind(staticWorkers),
endWorker: async () => {
await staticWorkers.end()
},
exportPageWorker: sharedPool
? staticWorkers.exportPage.bind(staticWorkers)
: undefined,
endWorker: sharedPool
? async () => {
await staticWorkers.end()
}
: undefined,
}
const exportConfig: any = {
...config,
Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/config-shared.ts
Expand Up @@ -88,6 +88,7 @@ export type NextConfig = { [key: string]: any } & {
swcMinify?: boolean
swcLoader?: boolean
cpus?: number
sharedPool?: boolean
plugins?: boolean
profiling?: boolean
isrFlushToDisk?: boolean
Expand Down Expand Up @@ -165,6 +166,7 @@ export const defaultConfig: NextConfig = {
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1
),
sharedPool: false,
plugins: false,
profiling: false,
isrFlushToDisk: true,
Expand Down

0 comments on commit 614bf80

Please sign in to comment.