Skip to content

Commit

Permalink
fix: dev.assetPrefix: true not work with server.base (#3789)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 22, 2024
1 parent 4bb0584 commit e90b5d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
25 changes: 25 additions & 0 deletions e2e/cases/server/base-url/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ test('server.base when dev', async ({ page }) => {
await rsbuild.close();
});

test('server.base with dev.assetPrefix: true', async ({ page }) => {
const rsbuild = await dev({
cwd: __dirname,
page,
rsbuildConfig: {
server: {
base: '/base',
},
dev: {
assetPrefix: true,
},
},
});

// should visit base url correctly
await page.goto(`http://localhost:${rsbuild.port}/base`);
await expect(page.locator('#test')).toHaveText('Hello Rsbuild!');

// should visit public dir correctly with base prefix
await page.goto(`http://localhost:${rsbuild.port}/base/aaa.txt`);
expect(await page.content()).toContain('aaaa');

await rsbuild.close();
});

test('server.base when build & preview', async ({ page }) => {
const { logs, restore } = proxyConsole('log');

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const getPublicPathFromCompiler = (
return DEFAULT_ASSET_PREFIX;
};

const urlJoin = (base: string, path: string) => {
export const urlJoin = (base: string, path: string) => {
const [urlProtocol, baseUrl] = base.split('://');
return `${urlProtocol}://${posix.join(baseUrl, path)}`;
};
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/plugins/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DEFAULT_DEV_HOST,
DEFAULT_PORT,
} from '../constants';
import { formatPublicPath, getFilename } from '../helpers';
import { formatPublicPath, getFilename, urlJoin } from '../helpers';
import { getCssExtractPlugin } from '../pluginHelper';
import { replacePortPlaceholder } from '../server/open';
import type {
Expand All @@ -24,7 +24,7 @@ function getPublicPath({
config: NormalizedEnvironmentConfig;
context: RsbuildContext;
}) {
const { dev, output } = config;
const { dev, output, server } = config;

let publicPath = DEFAULT_ASSET_PREFIX;
const port = context.devServer?.port || DEFAULT_PORT;
Expand All @@ -38,6 +38,7 @@ function getPublicPath({
} else if (dev.assetPrefix === true) {
const protocol = context.devServer?.https ? 'https' : 'http';
const hostname = context.devServer?.hostname || DEFAULT_DEV_HOST;

if (hostname === DEFAULT_DEV_HOST) {
const localHostname = 'localhost';
// If user not specify the hostname, it would use 0.0.0.0
Expand All @@ -48,6 +49,10 @@ function getPublicPath({
} else {
publicPath = `${protocol}://${hostname}:<port>/`;
}

if (server.base && server.base !== '/') {
publicPath = urlJoin(publicPath, server.base);
}
}

return formatPublicPath(replacePortPlaceholder(publicPath, port));
Expand Down

0 comments on commit e90b5d1

Please sign in to comment.