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

Fix IPv6 hostname formatting in CLI #43491

Merged
merged 17 commits into from
Jan 10, 2023
Merged
4 changes: 3 additions & 1 deletion packages/next/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import path from 'path'
import type { NextConfig } from '../types'
import type { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared'
import { isIPv6 } from 'net'

let isTurboSession = false
let sessionStopHandled = false
Expand Down Expand Up @@ -419,7 +420,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
startServer(devServerOptions)
.then(async (app) => {
const appUrl = `http://${app.hostname}:${app.port}`
startedDevelopmentServer(appUrl, `${host || '0.0.0.0'}:${app.port}`)
const hostname = host ? (isIPv6(host) ? `[${host}]` : host) : '0.0.0.0'
startedDevelopmentServer(appUrl, `${hostname}:${app.port}`)
// Start preflight after server is listening and ignore errors:
preflight().catch(() => {})
// Finalize server bootup:
Expand Down
4 changes: 3 additions & 1 deletion packages/next/cli/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Log from '../build/output/log'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { cliCommand } from '../lib/commands'
import { isIPv6 } from 'net'

const nextStart: cliCommand = (argv) => {
const validArgs: arg.Spec = {
Expand Down Expand Up @@ -80,7 +81,8 @@ const nextStart: cliCommand = (argv) => {
})
.then(async (app) => {
const appUrl = `http://${app.hostname}:${app.port}`
Log.ready(`started server on ${host}:${app.port}, url: ${appUrl}`)
const hostname = isIPv6(host) ? `[${host}]` : host
Log.ready(`started server on ${hostname}:${app.port}, url: ${appUrl}`)
await app.prepare()
})
.catch((err) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/next/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextServerOptions, NextServer, RequestHandler } from '../next'
import { warn } from '../../build/output/log'
import http from 'http'
import next from '../next'
import { isIPv6 } from 'net'

interface StartServerOptions extends NextServerOptions {
allowRetry?: boolean
Expand Down Expand Up @@ -52,6 +53,10 @@ export function startServer(opts: StartServerOptions) {
const hostname =
!opts.hostname || opts.hostname === '0.0.0.0'
? 'localhost'
: isIPv6(opts.hostname)
? opts.hostname === '::'
? `[${opts.hostname}1]`
: `[${opts.hostname}]`
: opts.hostname

const app = next({
Expand Down