diff --git a/packages/core/src/helpers/index.ts b/packages/core/src/helpers/index.ts index 3a88e85162..3ba9804ac3 100644 --- a/packages/core/src/helpers/index.ts +++ b/packages/core/src/helpers/index.ts @@ -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 diff --git a/packages/core/tests/helpers.test.ts b/packages/core/tests/helpers.test.ts index 44a100a010..c547efb17e 100644 --- a/packages/core/tests/helpers.test.ts +++ b/packages/core/tests/helpers.test.ts @@ -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( @@ -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', () => {