Skip to content

Commit

Permalink
improved network url in (dev) cli (#72634)
Browse files Browse the repository at this point in the history
Continues: #57882 with added test
and rebased history

Closes: #57882

---------

Co-authored-by: Ward Werbrouck <[email protected]>
  • Loading branch information
ijjk and kletse authored Nov 12, 2024
1 parent 0ee45fe commit b888c42
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
36 changes: 36 additions & 0 deletions packages/next/src/lib/get-network-host.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os from 'os'

function getNetworkHosts(family: 'IPv4' | 'IPv6'): string[] {
const interfaces = os.networkInterfaces()
const hosts: string[] = []

Object.keys(interfaces).forEach((key) => {
interfaces[key]
?.filter((networkInterface) => {
switch (networkInterface.family) {
case 'IPv6':
return (
family === 'IPv6' &&
networkInterface.scopeid === 0 &&
networkInterface.address !== '::1'
)
case 'IPv4':
return family === 'IPv4' && networkInterface.address !== '127.0.0.1'
default:
return false
}
})
.forEach((networkInterface) => {
if (networkInterface.address) {
hosts.push(networkInterface.address)
}
})
})

return hosts
}

export function getNetworkHost(family: 'IPv4' | 'IPv6'): string | null {
const hosts = getNetworkHosts(family)
return hosts[0] ?? null
}
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/app-info-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function logStartInfo({
if (networkUrl) {
Log.bootstrap(`- Network: ${networkUrl}`)
}
if (envInfo?.length) Log.bootstrap(`- Environments: ${envInfo.join(', ')}`)
if (envInfo?.length) Log.bootstrap(` - Environments: ${envInfo.join(', ')}`)

if (expFeatureInfo?.length) {
Log.bootstrap(`- Experiments (use with caution):`)
Expand Down
17 changes: 12 additions & 5 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getNetworkHost } from '../../lib/get-network-host'

if (performance.getEntriesByName('next-start').length === 0) {
performance.mark('next-start')
}
Expand Down Expand Up @@ -29,6 +31,7 @@ import { getStartServerInfo, logStartInfo } from './app-info-log'
import { validateTurboNextConfig } from '../../lib/turbopack-warning'
import { type Span, trace, flushAllTraces } from '../../trace'
import { isPostpone } from './router-utils/is-postpone'
import { isIPv6 } from './is-ipv6'

const debug = setupDebug('next:start-server')
let startServerSpan: Span | undefined
Expand Down Expand Up @@ -234,12 +237,16 @@ export async function startServer(

port = typeof addr === 'object' ? addr?.port || port : port

const networkUrl = hostname
? `${selfSignedCertificate ? 'https' : 'http'}://${actualHostname}:${port}`
const networkHostname =
hostname ?? getNetworkHost(isIPv6(actualHostname) ? 'IPv6' : 'IPv4')

const protocol = selfSignedCertificate ? 'https' : 'http'

const networkUrl = networkHostname
? `${protocol}://${formatHostname(networkHostname)}:${port}`
: null
const appUrl = `${
selfSignedCertificate ? 'https' : 'http'
}://${formattedHostname}:${port}`

const appUrl = `${protocol}://${formattedHostname}:${port}`

if (nodeDebugType) {
const formattedDebugAddress = getFormattedDebugAddress()
Expand Down
8 changes: 8 additions & 0 deletions test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ describe('CLI Usage', () => {
)
try {
await check(() => output, new RegExp(`http://localhost:${port}`))
await check(
() => output,
/Network:\s*http:\/\/[\d]{1,}\.[\d]{1,}\.[\d]{1,}/
)
} finally {
await killApp(app)
}
Expand All @@ -495,6 +499,10 @@ describe('CLI Usage', () => {
)
try {
await check(() => output, new RegExp(`http://localhost:${port}`))
await check(
() => output,
/Network:\s*http:\/\/[\d]{1,}\.[\d]{1,}\.[\d]{1,}/
)
} finally {
await killApp(app)
}
Expand Down

0 comments on commit b888c42

Please sign in to comment.