Skip to content

Commit

Permalink
Lazily init getStaticPathsWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Nov 24, 2021
1 parent a371447 commit 99b1213
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,43 @@ export default class DevServer extends Server {
protected sortedRoutes?: string[]
private addedUpgradeListener = false

protected staticPathsWorker: import('jest-worker').Worker & {
protected staticPathsWorker?: import('jest-worker').Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
}

private getStaticPathsWorker(): import('jest-worker').Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
} {
if (this.staticPathsWorker) {
return this.staticPathsWorker
}
this.staticPathsWorker = new Worker(
require.resolve('./static-paths-worker'),
{
maxRetries: 1,
numWorkers: this.nextConfig.experimental.cpus,
enableWorkerThreads: this.nextConfig.experimental.workerThreads,
forkOptions: {
env: {
...process.env,
// discard --inspect/--inspect-brk flags from process.env.NODE_OPTIONS. Otherwise multiple Node.js debuggers
// would be started if user launch Next.js in debugging mode. The number of debuggers is linked to
// the number of workers Next.js tries to launch. The only worker users are interested in debugging
// is the main Next.js one
NODE_OPTIONS: getNodeOptionsWithoutInspect(),
},
},
}
) as Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
}

this.staticPathsWorker.getStdout().pipe(process.stdout)
this.staticPathsWorker.getStderr().pipe(process.stderr)

return this.staticPathsWorker
}

constructor(
options: ServerConstructor & {
conf: NextConfig
Expand Down Expand Up @@ -132,29 +165,6 @@ export default class DevServer extends Server {

this.isCustomServer = !options.isNextDevCommand
this.pagesDir = findPagesDir(this.dir)
this.staticPathsWorker = new Worker(
require.resolve('./static-paths-worker'),
{
maxRetries: 1,
numWorkers: this.nextConfig.experimental.cpus,
enableWorkerThreads: this.nextConfig.experimental.workerThreads,
forkOptions: {
env: {
...process.env,
// discard --inspect/--inspect-brk flags from process.env.NODE_OPTIONS. Otherwise multiple Node.js debuggers
// would be started if user launch Next.js in debugging mode. The number of debuggers is linked to
// the number of workers Next.js tries to launch. The only worker users are interested in debugging
// is the main Next.js one
NODE_OPTIONS: getNodeOptionsWithoutInspect(),
},
},
}
) as Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
}

this.staticPathsWorker.getStdout().pipe(process.stdout)
this.staticPathsWorker.getStderr().pipe(process.stderr)
}

protected readBuildId(): string {
Expand Down Expand Up @@ -401,7 +411,7 @@ export default class DevServer extends Server {

protected async close(): Promise<void> {
await this.stopWatcher()
await this.staticPathsWorker.end()
await this.getStaticPathsWorker().end()
if (this.hotReloader) {
await this.hotReloader.stop()
}
Expand Down Expand Up @@ -841,7 +851,7 @@ export default class DevServer extends Server {
} = this.nextConfig
const { locales, defaultLocale } = this.nextConfig.i18n || {}

const paths = await this.staticPathsWorker.loadStaticPaths(
const paths = await this.getStaticPathsWorker().loadStaticPaths(
this.distDir,
pathname,
!this.renderOpts.dev && this._isLikeServerless,
Expand Down

0 comments on commit 99b1213

Please sign in to comment.