Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved network url in (dev) cli #72634

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(', ')}`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋🏼 Noticed this in logs, is this alignment change intentional?

> next build

   ▲ Next.js 15.0.4-canary.23
    - Environments: .env.production, .env # <----- Extra space indent intended?
   - Experiments (use with caution):
     · ppr

   Creating an optimized production build ...
 ✓ Compiled successfully
   Skipping validation of types
   Skipping linting
 ⚠ Using edge runtime on a page currently disables static generation for that page
 ✓ Collecting page data 
 ✓ Generating static pages (25/25)
 ✓ Collecting build traces    
 ✓ Finalizing page optimization    

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not, good catch!


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
Loading