Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up promises after resolving #52656

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,14 +1014,20 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
}

protected prepared: boolean = false
protected preparedPromise: Promise<void> | null = null
/**
* Runs async initialization of server.
* It is idempotent, won't fire underlying initialization more than once.
*/
public async prepare(): Promise<void> {
if (this.prepared) return

if (this.preparedPromise === null) {
this.preparedPromise = this.prepareImpl()
this.preparedPromise = this.prepareImpl().then(() => {
this.prepared = true
this.preparedPromise = null
})
}
return this.preparedPromise
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ export class DefaultRouteMatcherManager implements RouteMatcherManager {
}

private waitTillReadyPromise?: Promise<void>
public waitTillReady(): Promise<void> {
return this.waitTillReadyPromise ?? Promise.resolve()
public async waitTillReady(): Promise<void> {
if (this.waitTillReadyPromise) {
await this.waitTillReadyPromise
delete this.waitTillReadyPromise
}
}

private previousMatchers: ReadonlyArray<RouteMatcher> = []
Expand Down
11 changes: 8 additions & 3 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const SYMBOL_LOAD_CONFIG = Symbol('next.load_config')
export class NextServer {
private serverPromise?: Promise<Server>
private server?: Server
private reqHandler?: NodeRequestHandler
private reqHandlerPromise?: Promise<NodeRequestHandler>
private preparedAssetPrefix?: string

Expand Down Expand Up @@ -217,14 +218,18 @@ export class NextServer {
}

private async getServerRequestHandler() {
if (this.reqHandler) return this.reqHandler

// Memoize request handler creation
if (!this.reqHandlerPromise) {
this.reqHandlerPromise = this.getServer().then((server) =>
getTracer().wrap(
this.reqHandlerPromise = this.getServer().then((server) => {
this.reqHandler = getTracer().wrap(
NextServerSpan.getServerRequestHandler,
server.getRequestHandler().bind(server)
)
)
delete this.reqHandlerPromise
return this.reqHandler
})
}
return this.reqHandlerPromise
}
Expand Down