From dddcc57d96762a9e2aa592bf5cae6ad1c9c82002 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 30 Aug 2023 13:15:44 +0200 Subject: [PATCH] fix: use ipv4 for windows and wsl default host --- src/_utils.ts | 11 +++++++++++ src/listen.ts | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/_utils.ts b/src/_utils.ts index 7fb082e..004c8e7 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -2,6 +2,7 @@ import { networkInterfaces } from "node:os"; import { relative } from "pathe"; import { colors } from "consola/utils"; import { ListenOptions } from "./types"; +import { isWsl } from "./lib/wsl"; export function getNetworkInterfaces(includeIPV6?: boolean): string[] { const addrs = new Set(); @@ -70,6 +71,16 @@ export function generateURL( ); } +export function getDefaultHost(preferPublic?: boolean) { + // Prefer IPV4 stack for Windows and WSL to avoid performance issues + if (process.platform === "win32" || isWsl()) { + return preferPublic ? "0.0.0.0" : "127.0.0.1"; + } + // For local, use "localhost" to be developer friendly and allow loopback customization} + // For public, use "" to listen on all NIC interfaces (IPV4 and IPV6) + return preferPublic ? "" : "localhost"; +} + export function getPublicURL( listhenOptions: ListenOptions, baseURL?: string, diff --git a/src/listen.ts b/src/listen.ts index 46cffd7..1dff586 100644 --- a/src/listen.ts +++ b/src/listen.ts @@ -27,6 +27,7 @@ import { isAnyhost, getPublicURL, generateURL, + getDefaultHost, } from "./_utils"; import { resolveCertificate } from "./_cert"; @@ -49,7 +50,7 @@ export async function listen( name: "", https: false, port: process.env.PORT || 3000, - hostname: _hostname ?? (_public ? "" : "localhost"), + hostname: _hostname ?? getDefaultHost(_public), showURL: true, baseURL: "/", open: false,