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

fix(builder-shared): The http://0.0.0.0:port can't visit in windows #3443

Merged
merged 7 commits into from Apr 17, 2023
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