Skip to content

Commit

Permalink
only create cleanupListeners if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Nov 21, 2024
1 parent d263989 commit 6955e5d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export class NextServer implements NextWrapperServer {
/** The wrapper server used for `import next from "next" (in a custom server)` */
class NextCustomServer implements NextWrapperServer {
private didWebSocketSetup: boolean = false
protected cleanupListeners = new AsyncCallbackSet()
protected cleanupListeners?: AsyncCallbackSet

protected init?: {
requestHandler: WorkerRequestHandler
Expand Down Expand Up @@ -346,13 +346,17 @@ class NextCustomServer implements NextWrapperServer {
const { getRequestHandlers } =
require('./lib/start-server') as typeof import('./lib/start-server')

let onDevServerCleanup: AsyncCallbackSet['add'] | undefined
if (this.options.dev) {
this.cleanupListeners = new AsyncCallbackSet()
onDevServerCleanup = this.cleanupListeners.add.bind(this.cleanupListeners)
}

const initResult = await getRequestHandlers({
dir: this.options.dir!,
port: this.options.port || 3000,
isDev: !!this.options.dev,
onDevServerCleanup: this.options.dev
? this.cleanupListeners.add.bind(this.cleanupListeners)
: undefined,
onDevServerCleanup,
hostname: this.options.hostname || 'localhost',
minimalMode: this.options.minimalMode,
quiet: this.options.quiet,
Expand Down Expand Up @@ -449,7 +453,7 @@ class NextCustomServer implements NextWrapperServer {
async close() {
await Promise.allSettled([
this.init?.renderServer.close(),
this.cleanupListeners.runAll(),
this.cleanupListeners?.runAll(),
])
}
}
Expand Down

0 comments on commit 6955e5d

Please sign in to comment.