Skip to content

Commit

Permalink
fix(builder-shared): The http://0.0.0.0:port can't visit in windows (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
GiveMe-A-Name committed Apr 17, 2023
1 parent 37b56a5 commit b46fbcb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .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}`
11 changes: 10 additions & 1 deletion packages/builder/builder-shared/src/apply/output.ts
Expand Up @@ -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);
Expand Down
Expand Up @@ -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 {
Expand All @@ -22,7 +22,7 @@ export default {
The script URL will be:

```js
<script defer src="//${ip}:8080/static/js/main.js"></script>
<script defer src="//localhost:8080/static/js/main.js"></script>
```

If `assetPrefix` is set to `false` or not set, `/` is used as the default value.
Expand Down
Expand Up @@ -9,7 +9,7 @@

#### Boolean 类型

如果设置 `assetPrefix``true`,Builder 会自动计算出 `//ip:port/` 作为 URL 前缀:
如果设置 `assetPrefix``true`,Builder 会使用 `//localhost:port/` 作为 URL 前缀:

```js
export default {
Expand All @@ -22,7 +22,7 @@ export default {
对应 JS 文件在浏览器中加载的地址如下:

```js
<script defer src="//${ip}:8080/static/js/main.js"></script>
<script defer src="//localhost:8080/static/js/main.js"></script>
```

如果设置 `assetPrefix``false` 或不设置,则默认使用 `/` 作为访问前缀。
Expand Down
2 changes: 1 addition & 1 deletion 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');
Expand Down

0 comments on commit b46fbcb

Please sign in to comment.