diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fbc109686..5a54cca532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) +* fix(sdk-trace-web): make `parseUrl()` respect document.baseURI [#3670](https://github.com/open-telemetry/opentelemetry-js/pull/3670) @domasx2 * fix(resource): make properties for async resource resolution optional [#3677](https://github.com/open-telemetry/opentelemetry-js/pull/3677) @pichlermarc * fix(resources): change fs/promises import to be node 12 compatible [#3681](https://github.com/open-telemetry/opentelemetry-js/pull/3681) @pichlermarc diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index e8abd29274..127454b0a6 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -305,7 +305,10 @@ export interface URLLike { */ export function parseUrl(url: string): URLLike { if (typeof URL === 'function') { - return new URL(url, location.href); + return new URL( + url, + typeof document !== 'undefined' ? document.baseURI : location.href + ); } const element = getUrlNormalizingAnchor(); element.href = url; diff --git a/packages/opentelemetry-sdk-trace-web/test/window/utils.test.ts b/packages/opentelemetry-sdk-trace-web/test/window/utils.test.ts index fb184c2d1b..7dcd6694b5 100644 --- a/packages/opentelemetry-sdk-trace-web/test/window/utils.test.ts +++ b/packages/opentelemetry-sdk-trace-web/test/window/utils.test.ts @@ -171,6 +171,12 @@ describe('utils', () => { assert.strictEqual(typeof url[field], 'string'); }); }); + + it('should correctly parse relative url in presence of base tag', () => { + sinon.stub(globalThis.document, 'baseURI').value('http://foobar.com'); + const url = parseUrl('foo/bar'); + assert.strictEqual(url.href, 'http://foobar.com/foo/bar'); + }); }); describe('normalizeUrl', () => {