Skip to content

Commit

Permalink
Allow port 0 in next dev and next start
Browse files Browse the repository at this point in the history
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
wbinnssmith and styfle committed Aug 31, 2022
1 parent 29582c8 commit cf11c1d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
17 changes: 14 additions & 3 deletions packages/next/cli/next-dev.ts
Expand Up @@ -75,9 +75,20 @@ const nextDev: cliCommand = (argv) => {
)
}
}
const allowRetry = !args['--port']
let port: number =
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000

const hasPortFlag = Object.hasOwnProperty.call(args, '--port')
const allowRetry = !hasPortFlag
let port: number
if (hasPortFlag) {
port = args['--port']
} else {
const parsed = process.env.PORT && parseInt(process.env.PORT)
if (typeof parsed === 'number' && !Number.isNaN(parsed)) {
port = parsed
} else {
port = 3000
}
}

// we allow the server to use a random port while testing
// instead of attempting to find a random port and then hope
Expand Down
13 changes: 11 additions & 2 deletions packages/next/cli/next-start.ts
Expand Up @@ -52,8 +52,17 @@ const nextStart: cliCommand = (argv) => {
}

const dir = getProjectDir(args._[0])
let port: number =
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000
let port: number
if (Object.hasOwnProperty.call(args, '--port')) {
port = args['--port']
} else {
const parsed = process.env.PORT && parseInt(process.env.PORT)
if (typeof parsed === 'number' && !Number.isNaN(parsed)) {
port = parsed
} else {
port = 3000
}
}
const host = args['--hostname'] || '0.0.0.0'

if (process.env.__NEXT_FORCED_PORT) {
Expand Down
28 changes: 28 additions & 0 deletions test/integration/cli/test/index.test.js
Expand Up @@ -178,6 +178,34 @@ describe('CLI Usage', () => {
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('--port 0', async () => {
const output = await runNextCommandDev([dir, '--port', '0'], true)
const matches = /on 0.0.0.0:(\d+)/.exec(output)
expect(matches).not.toBe(null)

const port = parseInt(matches[1])
// Regression test: port 0 was interpreted as if no port had been
// provided, falling back to 3000.
expect(port).not.toBe(3000)

expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('PORT=0', async () => {
const output = await runNextCommandDev([dir], true, {
env: { PORT: 0 },
})
const matches = /on 0.0.0.0:(\d+)/.exec(output)
expect(matches).not.toBe(null)

const port = parseInt(matches[1])
// Regression test: port 0 was interpreted as if no port had been
// provided, falling back to 3000.
expect(port).not.toBe(3000)

expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test("NODE_OPTIONS='--inspect'", async () => {
// this test checks that --inspect works by launching a single debugger for the main Next.js process,
// not for its subprocesses
Expand Down

0 comments on commit cf11c1d

Please sign in to comment.