From 7fdaa9f1a95916fc2ed7a68ad9455e37e51881ec Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 17 Jun 2022 18:08:22 +0900 Subject: [PATCH 1/5] docs: improve wildcard host note --- docs/config/server-options.md | 7 +++++++ packages/vite/src/node/logger.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/config/server-options.md b/docs/config/server-options.md index a3f82aa0f12b79..b04ebbecbfedc2 100644 --- a/docs/config/server-options.md +++ b/docs/config/server-options.md @@ -10,6 +10,13 @@ Set this to `0.0.0.0` or `true` to listen on all addresses, including LAN and pu This can be set via the CLI using `--host 0.0.0.0` or `--host`. +::: tip NOTE + +When wildcard hosts (cf. `0.0.0.0`) is used, other servers might respond instead of Vite. +This is because servers listening on explicit hosts take priority over those listening on wildcard hosts. + +::: + ## server.port - **Type:** `number` diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index 0518d86b271ac7..6959459c10f363 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -224,7 +224,7 @@ function printServerUrls( notes.push({ label: 'Note', message: colors.dim( - 'You are using a wildcard host. Ports might be overridden.' + 'Other server might respond instead. More information: https://vitejs.dev/config/server-options.html#server-host' ) }) } From ed3d40b28e67082f5aed9e02956e8b0b304c210e Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sat, 18 Jun 2022 01:24:28 +0900 Subject: [PATCH 2/5] feat: remove wildcard host note --- packages/vite/src/node/logger.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index 6959459c10f363..fd7a12201d038c 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -8,7 +8,7 @@ import type { RollupError } from 'rollup' import type { CommonServerOptions } from './http' import type { Hostname } from './utils' import { resolveHostname } from './utils' -import { loopbackHosts, wildcardHosts } from './constants' +import { loopbackHosts } from './constants' import type { ResolvedConfig } from '.' export type LogType = 'error' | 'warn' | 'info' @@ -220,15 +220,6 @@ function printServerUrls( }) } - if (!hostname.host || wildcardHosts.has(hostname.host)) { - notes.push({ - label: 'Note', - message: colors.dim( - 'Other server might respond instead. More information: https://vitejs.dev/config/server-options.html#server-host' - ) - }) - } - const length = Math.max( ...[...urls, ...notes].map(({ label }) => label.length) ) From 95d2e2e7855a49d48a30d7549a779d2469f39a17 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sat, 18 Jun 2022 21:58:25 +0900 Subject: [PATCH 3/5] docs: add localhost hijack issues --- docs/config/server-options.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/config/server-options.md b/docs/config/server-options.md index b04ebbecbfedc2..12ce2d30b0dc05 100644 --- a/docs/config/server-options.md +++ b/docs/config/server-options.md @@ -12,8 +12,26 @@ This can be set via the CLI using `--host 0.0.0.0` or `--host`. ::: tip NOTE -When wildcard hosts (cf. `0.0.0.0`) is used, other servers might respond instead of Vite. -This is because servers listening on explicit hosts take priority over those listening on wildcard hosts. +There are cases when other servers might respond instead of Vite. + +The first case is when `localhost` is used. Node.js below v17 reorders the result of resolved address by default. This means the resolved address might differ from the original result from DNS. When accessing `localhost`, browsers use DNS to resolve the address and that address might differ from the address which Vite is listening. + +You could set [`dns.setDefaultResultOrder('verbatim')`](https://nodejs.org/docs/latest-v18.x/api/dns.html#dnssetdefaultresultorderorder) to disable the reordering behavior. Or you could set `server.host` to `127.0.0.1` explicitly. + +```js +// vite.config.js +import { defineConfig } from 'vite' +import dns from 'dns' + +dns.setDefaultResultOrder('verbatim') + +export default defineConfig({ + // omit +}) +``` + +The second case is when wildcard hosts (cf. `0.0.0.0`) is used. +This is because servers listening on non-wildcard hosts take priority over those listening on wildcard hosts. ::: From fed36a0351b4fcbf87af88f8182026db6354662f Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sat, 18 Jun 2022 22:01:18 +0900 Subject: [PATCH 4/5] docs: add link from preview.host note --- docs/config/preview-options.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/config/preview-options.md b/docs/config/preview-options.md index cf068bf74327ce..be4205ce525dcd 100644 --- a/docs/config/preview-options.md +++ b/docs/config/preview-options.md @@ -10,6 +10,13 @@ Set this to `0.0.0.0` or `true` to listen on all addresses, including LAN and pu This can be set via the CLI using `--host 0.0.0.0` or `--host`. +::: tip NOTE + +There are cases when other servers might respond instead of Vite. +See [`server.host`](./server-options#server-host) for more details. + +::: + ## preview.port - **Type:** `number` From 0e8e49842c8aa28eed4a37de58182c27d0597815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 18 Jun 2022 22:58:29 +0900 Subject: [PATCH 5/5] chore: thank you blu Co-authored-by: Bjorn Lu --- docs/config/server-options.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/config/server-options.md b/docs/config/server-options.md index 12ce2d30b0dc05..5cb221ee9f9fca 100644 --- a/docs/config/server-options.md +++ b/docs/config/server-options.md @@ -14,7 +14,7 @@ This can be set via the CLI using `--host 0.0.0.0` or `--host`. There are cases when other servers might respond instead of Vite. -The first case is when `localhost` is used. Node.js below v17 reorders the result of resolved address by default. This means the resolved address might differ from the original result from DNS. When accessing `localhost`, browsers use DNS to resolve the address and that address might differ from the address which Vite is listening. +The first case is when `localhost` is used. Node.js below v17 reorders the result of DNS-resolved address by default. When accessing `localhost`, browsers use DNS to resolve the address and that address might differ from the address which Vite is listening. You could set [`dns.setDefaultResultOrder('verbatim')`](https://nodejs.org/docs/latest-v18.x/api/dns.html#dnssetdefaultresultorderorder) to disable the reordering behavior. Or you could set `server.host` to `127.0.0.1` explicitly. @@ -30,8 +30,7 @@ export default defineConfig({ }) ``` -The second case is when wildcard hosts (cf. `0.0.0.0`) is used. -This is because servers listening on non-wildcard hosts take priority over those listening on wildcard hosts. +The second case is when wildcard hosts (e.g. `0.0.0.0`) is used. This is because servers listening on non-wildcard hosts take priority over those listening on wildcard hosts. :::