Skip to content

Commit 2d42b8e

Browse files
authored
Delay server start message until it's listening (#15929)
Fixes #15928 --- This would cause us to print the message too early and open the browser to a server that wasn't started yet. This waits until we're listening, but before the app is ready fully.
1 parent 817d558 commit 2d42b8e

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/next/cli/next-dev.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@ const nextDev: cliCommand = (argv) => {
5959
const port = args['--port'] || 3000
6060
const appUrl = `http://${args['--hostname'] || 'localhost'}:${port}`
6161

62-
startedDevelopmentServer(appUrl)
63-
6462
startServer(
6563
{ dir, dev: true, isNextDevCommand: true },
6664
port,
6765
args['--hostname']
6866
)
6967
.then(async (app) => {
68+
startedDevelopmentServer(appUrl)
7069
await app.prepare()
7170
})
7271
.catch((err) => {

test/integration/cli/test/index.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
/* eslint-env jest */
22

3-
import { runNextCommand, runNextCommandDev, findPort } from 'next-test-utils'
3+
import {
4+
runNextCommand,
5+
runNextCommandDev,
6+
findPort,
7+
launchApp,
8+
} from 'next-test-utils'
49
import { join } from 'path'
510
import pkg from 'next/package'
11+
import http from 'http'
12+
613
jest.setTimeout(1000 * 60 * 5)
714

815
const dir = join(__dirname, '..')
@@ -117,6 +124,39 @@ describe('CLI Usage', () => {
117124
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
118125
})
119126

127+
test('-p conflict', async () => {
128+
const port = await findPort()
129+
130+
let app = http.createServer((_, res) => {
131+
res.writeHead(200, { 'Content-Type': 'text/plain' })
132+
res.end('OK')
133+
})
134+
await new Promise((resolve, reject) => {
135+
// This code catches EADDRINUSE error if the port is already in use
136+
app.on('error', reject)
137+
app.on('listening', () => resolve())
138+
app.listen(port)
139+
})
140+
let stdout = '',
141+
stderr = ''
142+
await launchApp(dir, port, {
143+
stdout: true,
144+
stderr: true,
145+
onStdout(msg) {
146+
stdout += msg
147+
},
148+
onStderr(msg) {
149+
stderr += msg
150+
},
151+
})
152+
await new Promise((resolve) => app.close(resolve))
153+
expect(stderr).toMatch('already in use')
154+
expect(stdout).not.toMatch('ready')
155+
expect(stdout).not.toMatch('started')
156+
expect(stdout).not.toMatch(`${port}`)
157+
expect(stdout).toBeFalsy()
158+
})
159+
120160
test('--hostname', async () => {
121161
const port = await findPort()
122162
const output = await runNextCommandDev(

0 commit comments

Comments
 (0)