From 38a9a568486aab348e83c06462605909b0f14c26 Mon Sep 17 00:00:00 2001 From: CheeseYu Date: Thu, 10 Oct 2024 21:40:39 +0800 Subject: [PATCH] fix(core): ensure that keep the original URL (#3674) Co-authored-by: neverland --- packages/core/src/helpers/index.ts | 5 ++--- packages/core/tests/helpers.test.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) 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', () => {