Skip to content

Commit

Permalink
fix(core): ensure that keep the original URL (#3674)
Browse files Browse the repository at this point in the history
Co-authored-by: neverland <[email protected]>
  • Loading branch information
Cheese-Yu and chenjiahan authored Oct 10, 2024
1 parent 3b2827c commit 38a9a56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ export const getPublicPathFromCompiler = (
};

const urlJoin = (base: string, path: string) => {
const fullUrl = new URL(base);
fullUrl.pathname = posix.join(fullUrl.pathname, path);
return fullUrl.toString();
const [urlProtocol, baseUrl] = base.split('://');
return `${urlProtocol}://${posix.join(baseUrl, path)}`;
};

// Can be replaced with URL.canParse when we drop support for Node.js 16
Expand Down
10 changes: 10 additions & 0 deletions packages/core/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ it('normalizeUrl', () => {

describe('ensureAssetPrefix', () => {
const ASSET_PREFIX = 'https://www.example.com/static/';
const CAPITAL_ASSET_PREFIX = 'https://www.{{CDN}}.com/{{CDN_PATH}}/';

it('should handle relative url', () => {
expect(ensureAssetPrefix('foo/bar.js', ASSET_PREFIX)).toBe(
Expand Down Expand Up @@ -157,6 +158,15 @@ describe('ensureAssetPrefix', () => {
expect(ensureAssetPrefix('//foo.com/bar.js', '/')).toBe('//foo.com/bar.js');
expect(ensureAssetPrefix('/bar.js', '//foo.com')).toBe('//foo.com/bar.js');
});

it('should keep the original URL', () => {
expect(ensureAssetPrefix('foo/bar.js', CAPITAL_ASSET_PREFIX)).toBe(
'https://www.{{CDN}}.com/{{CDN_PATH}}/foo/bar.js',
);
expect(ensureAssetPrefix('/foo/bar.js', CAPITAL_ASSET_PREFIX)).toBe(
'https://www.{{CDN}}.com/{{CDN_PATH}}/foo/bar.js',
);
});
});

describe('getCommonParentPath', () => {
Expand Down

0 comments on commit 38a9a56

Please sign in to comment.