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

docs: improve wildcard host note #8634

Merged
merged 5 commits into from Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/config/preview-options.md
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
25 changes: 25 additions & 0 deletions docs/config/server-options.md
Expand Up @@ -10,6 +10,31 @@ 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
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

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.
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

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.
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we could simplify this section if #8647 is merged.

Copy link
Member

Choose a reason for hiding this comment

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

I think it may still be helpful to explain this with #8647 merged. Others may wonder how localhost isn't printed and we have an answer here.


```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.
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

:::

## server.port

- **Type:** `number`
Expand Down
11 changes: 1 addition & 10 deletions packages/vite/src/node/logger.ts
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 @@ -220,15 +220,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