From b46fbcbedcf3ef09f30ef505da66feb959190c5b Mon Sep 17 00:00:00 2001
From: qixuan <58852732+GiveMe-A-Name@users.noreply.github.com>
Date: Mon, 17 Apr 2023 10:19:03 +0800
Subject: [PATCH] fix(builder-shared): The http://0.0.0.0:port can't visit in
windows (#3443)
* fix: the http://0.0.0.0:port cant visit in windows, we shouldnt set publicPath as //0.0.0.0:${port}/
* fix: save hostname when assetPrefix set true
* fix: remove console info
* perf: set the localHostname as localhost
---
.changeset/blue-kiwis-raise.md | 6 ++++++
packages/builder/builder-shared/src/apply/output.ts | 11 ++++++++++-
.../builder-doc/docs/en/config/dev/assetPrefix.md | 4 ++--
.../builder-doc/docs/zh/config/dev/assetPrefix.md | 4 ++--
tests/integration/asset-prefix/test/index.test.ts | 2 +-
5 files changed, 21 insertions(+), 6 deletions(-)
create mode 100644 .changeset/blue-kiwis-raise.md
diff --git a/.changeset/blue-kiwis-raise.md b/.changeset/blue-kiwis-raise.md
new file mode 100644
index 00000000000..e933330145b
--- /dev/null
+++ b/.changeset/blue-kiwis-raise.md
@@ -0,0 +1,6 @@
+---
+'@modern-js/builder-shared': patch
+---
+
+fix: The http://0.0.0.0:port can't visit in windows, we shouldn't set publicPath as `//0.0.0.0:${port}/`;
+fix: 在 windows 里不能正常访问 http://0.0.0.0:port,我们不应该将 publicPath 设置成 `//0.0.0.0:${port}`
diff --git a/packages/builder/builder-shared/src/apply/output.ts b/packages/builder/builder-shared/src/apply/output.ts
index 5c854d76183..5438c8cbba0 100644
--- a/packages/builder/builder-shared/src/apply/output.ts
+++ b/packages/builder/builder-shared/src/apply/output.ts
@@ -82,7 +82,16 @@ function getPublicPath({
} else if (dev.assetPrefix === true) {
const hostname = context.devServer?.hostname || DEFAULT_DEV_HOST;
const port = context.devServer?.port || DEFAULT_PORT;
- publicPath = `//${hostname}:${port}/`;
+ if (hostname === DEFAULT_DEV_HOST) {
+ const localHostname = `localhost`;
+ // If user not specify the hostname, it would use 0.0.0.0
+ // The http://0.0.0.0:port can't visit in windows, so we shouldn't set publicPath as `//0.0.0.0:${port}/`;
+ // Relative to docs:
+ // - https://github.com/quarkusio/quarkus/issues/12246
+ publicPath = `//${localHostname}:${port}/`;
+ } else {
+ publicPath = `//${hostname}:${port}/`;
+ }
}
return addTrailingSlash(publicPath);
diff --git a/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md b/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md
index 5c37bdb60f3..37dd6b7f9f2 100644
--- a/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md
+++ b/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md
@@ -9,7 +9,7 @@ This config is only used in the development environment. In the production envir
#### Boolean Type
-If `assetPrefix` is set to `true`, the URL prefix will be `//ip:port/`:
+If `assetPrefix` is set to `true`, the URL prefix will be `//localhost:port/`:
```js
export default {
@@ -22,7 +22,7 @@ export default {
The script URL will be:
```js
-
+
```
If `assetPrefix` is set to `false` or not set, `/` is used as the default value.
diff --git a/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md b/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md
index a7f5e2d2f69..50e09a311e0 100644
--- a/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md
+++ b/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md
@@ -9,7 +9,7 @@
#### Boolean 类型
-如果设置 `assetPrefix` 为 `true`,Builder 会自动计算出 `//ip:port/` 作为 URL 前缀:
+如果设置 `assetPrefix` 为 `true`,Builder 会使用 `//localhost:port/` 作为 URL 前缀:
```js
export default {
@@ -22,7 +22,7 @@ export default {
对应 JS 文件在浏览器中加载的地址如下:
```js
-
+
```
如果设置 `assetPrefix` 为 `false` 或不设置,则默认使用 `/` 作为访问前缀。
diff --git a/tests/integration/asset-prefix/test/index.test.ts b/tests/integration/asset-prefix/test/index.test.ts
index 5928551f07c..e61a2aa3181 100644
--- a/tests/integration/asset-prefix/test/index.test.ts
+++ b/tests/integration/asset-prefix/test/index.test.ts
@@ -1,9 +1,9 @@
import path from 'path';
import { readFileSync } from 'fs';
import { Page } from 'puppeteer';
-import { DEFAULT_DEV_HOST } from '@modern-js/utils';
import { launchApp, killApp } from '../../../utils/modernTestUtils';
+const DEFAULT_DEV_HOST = 'localhost';
declare const page: Page;
const fixtures = path.resolve(__dirname, '../fixtures');