Skip to content

Commit

Permalink
Fix issue where NextServer.prepare wouldn't be called when deployed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jankaifer committed Apr 4, 2023
1 parent eba1626 commit f621830
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
15 changes: 13 additions & 2 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
res: BaseNextResponse,
parsedUrl?: NextUrlWithParsedQuery
): Promise<void> {
await this.prepare()
const method = req.method.toUpperCase()
return getTracer().trace(
BaseServerSpan.handleRequest,
Expand Down Expand Up @@ -968,8 +969,18 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
}

// Backwards compatibility
public async prepare(): Promise<void> {}
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.preparedPromise === null) {
this.preparedPromise = this.prepareImpl()
}
return this.preparedPromise
}
protected async prepareImpl(): Promise<void> {}

// Backwards compatibility
protected async close(): Promise<void> {}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ export default class DevServer extends Server {
}
}

async prepare(): Promise<void> {
protected async prepareImpl(): Promise<void> {
setGlobal('distDir', this.distDir)
setGlobal('phase', PHASE_DEVELOPMENT_SERVER)

Expand Down Expand Up @@ -907,7 +907,7 @@ export default class DevServer extends Server {
telemetry,
})
}
await super.prepare()
await super.prepareImpl()
await this.addExportPathMapRoutes()
await this.hotReloader?.start()
await this.startWatcher()
Expand Down
7 changes: 5 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ export default class NextNodeServer extends BaseServer {
setHttpClientAndAgentOptions(this.nextConfig)
}

public async prepare() {
await super.prepare()
protected async prepareImpl() {
await super.prepareImpl()
if (
!this.serverOptions.dev &&
this.nextConfig.experimental.instrumentationHook
Expand Down Expand Up @@ -1639,6 +1639,9 @@ export default class NextNodeServer extends BaseServer {
}

public getRequestHandler(): NodeRequestHandler {
// This is just optimization to fire prepare as soon as possible
// It will be properly awaited later
void this.prepare()
const handler = super.getRequestHandler()
return async (req, res, parsedUrl) => {
return handler(this.normalizeReq(req), this.normalizeRes(res), parsedUrl)
Expand Down
18 changes: 13 additions & 5 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ export class NextServer {

async prepare() {
const server = await this.getServer()
return server.prepare()

// We shouldn't prepare the server in production,
// because this code won't be executed when deployed
if (this.options.dev) {
await server.prepare()
}
}

async close() {
Expand All @@ -135,12 +140,15 @@ export class NextServer {
}

private async createServer(options: DevServerOptions): Promise<Server> {
let ServerImplementation: typeof Server
if (options.dev) {
const DevServer = require('./dev/next-dev-server').default
return new DevServer(options)
ServerImplementation = require('./dev/next-dev-server').default
} else {
ServerImplementation = await getServerImpl()
}
const ServerImplementation = await getServerImpl()
return new ServerImplementation(options)
const server = new ServerImplementation(options)

return server
}

private async loadConfig() {
Expand Down

0 comments on commit f621830

Please sign in to comment.