Skip to content

Commit

Permalink
disambiguate onCleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Nov 18, 2024
1 parent 5476f49 commit ded0ec1
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export interface Options {
* The HTTP Server that Next.js is running behind
*/
httpServer?: HTTPServer
/**
* Allows scheduling callback to run before the server shuts down.
* (only available in `next start` or in custom servers)
*/
onCleanup?: (cb: () => Promise<void>) => void
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export async function createHotReloaderTurbopack(
memoryLimit: opts.nextConfig.experimental.turbo?.memoryLimit,
}
)
opts.onCleanup(() => project.onExit())
opts.onDevServerCleanup?.(() => project.onExit())
const entrypointsSubscription = project.entrypointsSubscribe()

const currentWrittenEntrypoints: Map<EntryKey, WrittenEndpoint> = new Map()
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ async function initializeImpl(opts: {
bundlerService: DevBundlerService | undefined
startServerSpan: Span | undefined
quiet?: boolean
onCleanup: (listener: () => Promise<void>) => void
}) {
const type = process.env.__NEXT_PRIVATE_RENDER_WORKER
if (type) {
Expand Down
5 changes: 2 additions & 3 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function initialize(opts: {
dir: string
port: number
dev: boolean
onCleanup: (listener: () => Promise<void>) => void
onDevServerCleanup: ((listener: () => Promise<void>) => void) | undefined
server?: import('http').Server
minimalMode?: boolean
hostname?: string
Expand Down Expand Up @@ -145,7 +145,7 @@ export async function initialize(opts: {
isCustomServer: opts.customServer,
turbo: !!process.env.TURBOPACK,
port: opts.port,
onCleanup: opts.onCleanup,
onDevServerCleanup: opts.onDevServerCleanup,
resetFetch,
})
)
Expand Down Expand Up @@ -626,7 +626,6 @@ export async function initialize(opts: {
bundlerService: devBundlerService,
startServerSpan: opts.startServerSpan,
quiet: opts.quiet,
onCleanup: opts.onCleanup,
}
renderServerOpts.serverFields.routerServerHandler = requestHandlerImpl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export type SetupOpts = {
>
nextConfig: NextConfigComplete
port: number
onCleanup: (listener: () => Promise<void>) => void
onDevServerCleanup: ((listener: () => Promise<void>) => void) | undefined
resetFetch: () => void
}

Expand Down
11 changes: 7 additions & 4 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function getRequestHandlers({
dir,
port,
isDev,
onCleanup,
onDevServerCleanup,
server,
hostname,
minimalMode,
Expand All @@ -64,7 +64,7 @@ export async function getRequestHandlers({
dir: string
port: number
isDev: boolean
onCleanup: (listener: () => Promise<void>) => void
onDevServerCleanup: ((listener: () => Promise<void>) => void) | undefined
server?: import('http').Server
hostname?: string
minimalMode?: boolean
Expand All @@ -76,7 +76,7 @@ export async function getRequestHandlers({
dir,
port,
hostname,
onCleanup,
onDevServerCleanup,
dev: isDev,
minimalMode,
server,
Expand Down Expand Up @@ -333,7 +333,7 @@ export async function startServer(
dir,
port,
isDev,
onCleanup,
onDevServerCleanup: isDev ? onCleanup : undefined,
server,
hostname,
minimalMode,
Expand All @@ -344,6 +344,9 @@ export async function startServer(
upgradeHandler = initResult[1]
const nextServer = initResult[2]

// NOTE: `NextServer` also has a method called `onCleanup`,
// (used e.g. for implementing `waitUntil` in `next start`)
// and this is what actually makes those cleanups execute
onCleanup(() => nextServer.close())

const startServerProcessDuration =
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const getServerImpl = async () => {
export type NextServerOptions = Omit<
ServerOptions | DevServerOptions,
// This is assigned in this server abstraction.
'conf'
'conf' | 'onCleanup'
> &
Partial<Pick<ServerOptions | DevServerOptions, 'conf'>>

Expand Down Expand Up @@ -287,7 +287,9 @@ class NextCustomServer extends NextServer {
dir: this.options.dir!,
port: this.options.port || 3000,
isDev: !!this.options.dev,
onCleanup: this.onCleanup.bind(this),
onDevServerCleanup: this.options.dev
? this.onCleanup.bind(this)
: undefined,
hostname: this.options.hostname || 'localhost',
minimalMode: this.options.minimalMode,
quiet: this.options.quiet,
Expand Down

0 comments on commit ded0ec1

Please sign in to comment.