Skip to content

Commit

Permalink
docs: improve wildcard host note (#8634)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
sapphi-red and bluwy authored Jun 18, 2022
1 parent 5ecb5c6 commit 9a1c1ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
7 changes: 7 additions & 0 deletions docs/config/preview-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
24 changes: 24 additions & 0 deletions docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ 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.

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.

```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 (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.

:::

## server.port

- **Type:** `number`
Expand Down
11 changes: 1 addition & 10 deletions packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -219,15 +219,6 @@ function printServerUrls(
})
}

if (!hostname.host || wildcardHosts.has(hostname.host)) {
notes.push({
label: 'Note',
message: colors.dim(
'You are using a wildcard host. Ports might be overridden.'
)
})
}

const length = Math.max(
...[...urls, ...notes].map(({ label }) => label.length)
)
Expand Down

0 comments on commit 9a1c1ae

Please sign in to comment.