Skip to content

Commit

Permalink
Merge branch 'canary' into align-readable-stream-impl
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Nov 11, 2021
2 parents 2f17d00 + 4551571 commit a6b6eaf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Next.js can be deployed to any hosting provider that supports Node.js. Make sure
```json
{
"scripts": {
"dev": "next",
"dev": "next dev",
"build": "next build",
"start": "next start"
}
Expand Down
8 changes: 6 additions & 2 deletions packages/next/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const nextDev: cliCommand = (argv) => {
)
}
}

const allowRetry = !args['--port']
let port: number =
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000

Expand All @@ -88,7 +88,11 @@ const nextDev: cliCommand = (argv) => {
// some set-ups that rely on listening on other interfaces
const host = args['--hostname']

startServer({ dir, dev: true, isNextDevCommand: true }, port, host)
startServer(
{ dir, dev: true, isNextDevCommand: true, allowRetry },
port,
host
)
.then(async ({ app, actualPort }) => {
const appUrl = `http://${
!host || host === '0.0.0.0' ? 'localhost' : host
Expand Down
20 changes: 18 additions & 2 deletions packages/next/server/lib/start-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import http from 'http'
import next from '../next'
import { warn } from '../../build/output/log'

export default async function start(
serverOptions: any,
Expand All @@ -18,8 +19,23 @@ export default async function start(
requestHandler = app.getRequestHandler()

await new Promise<void>((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
srv.on('error', reject)
let retryCount = 0
srv.on('error', (err: NodeJS.ErrnoException) => {
// This code catches EADDRINUSE error if the port is already in use
if (
err.code === 'EADDRINUSE' &&
serverOptions.allowRetry &&
port &&
retryCount < 10
) {
warn(`Port ${port} is in use, trying ${port + 1} instead.`)
port += 1
retryCount += 1
srv.listen(port, hostname)
} else {
reject(err)
}
})
srv.on('listening', () => resolve())
srv.listen(port, hostname)
})
Expand Down
1 change: 0 additions & 1 deletion test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ describe('i18n Support', () => {
expect(res.status).toBe(404)
const $ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
console.log(props)
expect($('#not-found').text().length > 0).toBe(true)
expect(props).toEqual({
is404: true,
Expand Down

0 comments on commit a6b6eaf

Please sign in to comment.