Skip to content

Commit

Permalink
Use localhost in CLI output when binding to 0.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chulkilee committed Jan 20, 2021
1 parent d6bbe27 commit 22c49a7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/output/index.ts
Expand Up @@ -5,8 +5,8 @@ import createStore from 'next/dist/compiled/unistore'
import formatWebpackMessages from '../../client/dev/error-overlay/format-webpack-messages'
import { OutputState, store as consoleStore } from './store'

export function startedDevelopmentServer(appUrl: string) {
consoleStore.setState({ appUrl })
export function startedDevelopmentServer(appUrl: string, bindAddr: string) {
consoleStore.setState({ appUrl, bindAddr })
}

let previousClient: import('webpack').Compiler | null = null
Expand Down
14 changes: 9 additions & 5 deletions packages/next/build/output/store.ts
Expand Up @@ -4,8 +4,8 @@ import stripAnsi from 'next/dist/compiled/strip-ansi'
import * as Log from './log'

export type OutputState =
| { bootstrap: true; appUrl: string | null }
| ({ bootstrap: false; appUrl: string | null } & (
| { bootstrap: true; appUrl: string | null; bindAddr: string | null }
| ({ bootstrap: false; appUrl: string | null; bindAddr: string | null } & (
| { loading: true }
| {
loading: false
Expand All @@ -15,9 +15,13 @@ export type OutputState =
}
))

export const store = createStore<OutputState>({ appUrl: null, bootstrap: true })
export const store = createStore<OutputState>({
appUrl: null,
bindAddr: null,
bootstrap: true,
})

let lastStore: OutputState = { appUrl: null, bootstrap: true }
let lastStore: OutputState = { appUrl: null, bindAddr: null, bootstrap: true }
function hasStoreChanged(nextStore: OutputState) {
if (
([
Expand All @@ -40,7 +44,7 @@ store.subscribe((state) => {

if (state.bootstrap) {
if (state.appUrl) {
Log.ready(`started server on ${state.appUrl}`)
Log.ready(`started server on ${state.bindAddr}, url: ${state.appUrl}`)
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/cli/next-dev.ts
Expand Up @@ -107,11 +107,11 @@ const nextDev: cliCommand = (argv) => {

const port = args['--port'] || 3000
const host = args['--hostname'] || '0.0.0.0'
const appUrl = `http://${host}:${port}`
const appUrl = `http://${host === '0.0.0.0' ? 'localhost' : host}:${port}`

startServer({ dir, dev: true, isNextDevCommand: true }, port, host)
.then(async (app) => {
startedDevelopmentServer(appUrl)
startedDevelopmentServer(appUrl, `${host}:${port}`)
// Start preflight after server is listening and ignore errors:
preflight().catch(() => {})
// Finalize server bootup:
Expand Down
3 changes: 2 additions & 1 deletion packages/next/cli/next-start.ts
Expand Up @@ -51,9 +51,10 @@ const nextStart: cliCommand = (argv) => {
const dir = resolve(args._[0] || '.')
const port = args['--port'] || 3000
const host = args['--hostname'] || '0.0.0.0'
const appUrl = `http://${host === '0.0.0.0' ? 'localhost' : host}:${port}`
startServer({ dir }, port, host)
.then(async (app) => {
Log.ready(`started server on http://${host}:${port}`)
Log.ready(`started server on ${host}:${port}, url: ${appUrl}`)
await app.prepare()
})
.catch((err) => {
Expand Down
15 changes: 10 additions & 5 deletions test/integration/cli/test/index.test.js
Expand Up @@ -204,7 +204,8 @@ describe('CLI Usage', () => {
test('--port', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '--port', port], true)
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`on 0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test("NODE_OPTIONS='--inspect'", async () => {
Expand All @@ -214,13 +215,15 @@ describe('CLI Usage', () => {
const output = await runNextCommandDev([dir, '--port', port], true, {
env: { NODE_OPTIONS: '--inspect' },
})
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`on 0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('-p', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '-p', port], true)
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`on 0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('-p conflict', async () => {
Expand Down Expand Up @@ -262,7 +265,8 @@ describe('CLI Usage', () => {
[dir, '--hostname', '0.0.0.0', '--port', port],
true
)
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`on 0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('-H', async () => {
Expand All @@ -271,7 +275,8 @@ describe('CLI Usage', () => {
[dir, '-H', '0.0.0.0', '--port', port],
true
)
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`on 0.0.0.0:${port}`))
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('should warn when unknown argument provided', async () => {
Expand Down

0 comments on commit 22c49a7

Please sign in to comment.