Unverified Commit f6218305 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Fix issue where NextServer.prepare wouldn't be called when deployed (#47899)

parent eba1626b
......@@ -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,
......@@ -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> {}
......
......@@ -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)
......@@ -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()
......
......@@ -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
......@@ -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)
......
......@@ -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() {
......@@ -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() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment