Skip to content

Commit

Permalink
fix: the http://0.0.0.0:port cant visit in windows, we shouldnt set p…
Browse files Browse the repository at this point in the history
…ublicPath as //0.0.0.0:${port}/
  • Loading branch information
GiveMe-A-Name committed Apr 14, 2023
1 parent 6eff0fd commit 4911ea3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/blue-kiwis-raise.md
Original file line number Diff line number Diff line change
@@ -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}`
8 changes: 7 additions & 1 deletion packages/builder/builder-shared/src/apply/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ 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) {
// If user not specify the hostname, it would use DEFAULT_DEV_HOST(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 = `//${hostname}:${port}/`;
}
}

return addTrailingSlash(publicPath);
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/asset-prefix/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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';

declare const page: Page;
Expand All @@ -18,7 +17,7 @@ describe('asset prefix', () => {
path.join(appDir, 'dist/html/main/index.html'),
'utf-8',
);
expect(HTML.includes(`//${DEFAULT_DEV_HOST}:3333/static/js/`)).toBeTruthy();
expect(HTML.includes(`/static/js/`)).toBeTruthy();

killApp(app);
});
Expand All @@ -27,7 +26,7 @@ describe('asset prefix', () => {
const appDir = path.resolve(fixtures, 'dev-asset-prefix');

const app = await launchApp(appDir);
const expected = `//${DEFAULT_DEV_HOST}:3333`;
const expected = '';

const mainJs = readFileSync(
path.join(appDir, 'dist/static/js/main.js'),
Expand All @@ -38,7 +37,7 @@ describe('asset prefix', () => {
mainJs.includes(`window.__assetPrefix__ = '${expected}';`),
).toBeTruthy();

await page.goto(`http:${expected}`);
await page.goto(`http://localhost:3333`);

const assetPrefix = await page.evaluate(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down

0 comments on commit 4911ea3

Please sign in to comment.