Skip to content

Commit

Permalink
Delay server start message until it's listening (#15929)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Timer authored Aug 6, 2020
1 parent 817d558 commit 2d42b8e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/next/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ const nextDev: cliCommand = (argv) => {
const port = args['--port'] || 3000
const appUrl = `http://${args['--hostname'] || 'localhost'}:${port}`

startedDevelopmentServer(appUrl)

startServer(
{ dir, dev: true, isNextDevCommand: true },
port,
args['--hostname']
)
.then(async (app) => {
startedDevelopmentServer(appUrl)
await app.prepare()
})
.catch((err) => {
Expand Down
42 changes: 41 additions & 1 deletion test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-env jest */

import { runNextCommand, runNextCommandDev, findPort } from 'next-test-utils'
import {
runNextCommand,
runNextCommandDev,
findPort,
launchApp,
} from 'next-test-utils'
import { join } from 'path'
import pkg from 'next/package'
import http from 'http'

jest.setTimeout(1000 * 60 * 5)

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

test('-p conflict', async () => {
const port = await findPort()

let app = http.createServer((_, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('OK')
})
await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
app.on('error', reject)
app.on('listening', () => resolve())
app.listen(port)
})
let stdout = '',
stderr = ''
await launchApp(dir, port, {
stdout: true,
stderr: true,
onStdout(msg) {
stdout += msg
},
onStderr(msg) {
stderr += msg
},
})
await new Promise((resolve) => app.close(resolve))
expect(stderr).toMatch('already in use')
expect(stdout).not.toMatch('ready')
expect(stdout).not.toMatch('started')
expect(stdout).not.toMatch(`${port}`)
expect(stdout).toBeFalsy()
})

test('--hostname', async () => {
const port = await findPort()
const output = await runNextCommandDev(
Expand Down

0 comments on commit 2d42b8e

Please sign in to comment.