Skip to content

Commit fada27e

Browse files
authored
fix: open page with the correct host (#3954)
1 parent 81faeac commit fada27e

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

packages/core/src/server/helper.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,10 @@ const isLoopbackHost = (host: string) => {
350350
return loopbackHosts.includes(host);
351351
};
352352

353-
const getHostInUrl = (host: string) => {
353+
export const getHostInUrl = (host: string): string => {
354+
if (host === DEFAULT_DEV_HOST) {
355+
return 'localhost';
356+
}
354357
if (net.isIPv6(host)) {
355358
return host === '::' ? '[::1]' : `[${host}]`;
356359
}

packages/core/src/server/open.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { STATIC_PATH } from '../constants';
44
import { canParse, castArray } from '../helpers';
55
import { logger } from '../logger';
66
import type { NormalizedConfig, Routes } from '../types';
7+
import { getHostInUrl } from './helper';
78

89
const execAsync = promisify(exec);
910

@@ -139,7 +140,6 @@ export async function open({
139140
clearCache?: boolean;
140141
}): Promise<void> {
141142
const { targets, before } = normalizeOpenConfig(config);
142-
const host = config.server.host || 'localhost';
143143

144144
// Skip open in codesandbox. After being bundled, the `open` package will
145145
// try to call system xdg-open, which will cause an error on codesandbox.
@@ -155,6 +155,7 @@ export async function open({
155155

156156
const urls: string[] = [];
157157
const protocol = https ? 'https' : 'http';
158+
const host = getHostInUrl(config.server.host);
158159
const baseUrl = `${protocol}://${host}:${port}`;
159160

160161
if (!targets.length) {

website/docs/en/config/server/open.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Open =
2323

2424
`server.open` can be set to the following values.
2525

26-
- Open the project's default preview page, equivalent to `http://localhost:<port>`:
26+
- Open the project's default preview page, which defaults to `http://localhost:<port>`. If [server.host](/config/server/host) is configured, it defaults to `http://<host>:<port>`.
2727

2828
```js
2929
export default {

website/docs/zh/config/server/open.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Open =
2323

2424
`server.open` 可以设置为如下的值。
2525

26-
- 打开项目的默认页面,等价于 `http://localhost:<port>`
26+
- 打开项目的默认页面,默认为 `http://localhost:<port>`。如果配置了 [server.host](/config/server/host),则默认为 `http://<host>:<port>`
2727

2828
```js
2929
export default {

0 commit comments

Comments
 (0)