Skip to content

Commit 18ad56f

Browse files
domasx2dyladan
andauthored
fix(sdk-trace-web): make parseUrl respect document.baseURI (#3670)
* fix parseUrl to handle baseurl override for relative urls * tests * lint fixes * add changelog entry --------- Co-authored-by: Daniel Dyla <[email protected]>
1 parent 52facfa commit 18ad56f

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
1515

1616
### :bug: (Bug Fix)
1717

18+
* fix(sdk-trace-web): make `parseUrl()` respect document.baseURI [#3670](https://github.com/open-telemetry/opentelemetry-js/pull/3670) @domasx2
1819
* fix(resource): make properties for async resource resolution optional [#3677](https://github.com/open-telemetry/opentelemetry-js/pull/3677) @pichlermarc
1920
* fix(resources): change fs/promises import to be node 12 compatible [#3681](https://github.com/open-telemetry/opentelemetry-js/pull/3681) @pichlermarc
2021

packages/opentelemetry-sdk-trace-web/src/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ export interface URLLike {
305305
*/
306306
export function parseUrl(url: string): URLLike {
307307
if (typeof URL === 'function') {
308-
return new URL(url, location.href);
308+
return new URL(
309+
url,
310+
typeof document !== 'undefined' ? document.baseURI : location.href
311+
);
309312
}
310313
const element = getUrlNormalizingAnchor();
311314
element.href = url;

packages/opentelemetry-sdk-trace-web/test/window/utils.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ describe('utils', () => {
171171
assert.strictEqual(typeof url[field], 'string');
172172
});
173173
});
174+
175+
it('should correctly parse relative url in presence of base tag', () => {
176+
sinon.stub(globalThis.document, 'baseURI').value('http://foobar.com');
177+
const url = parseUrl('foo/bar');
178+
assert.strictEqual(url.href, 'http://foobar.com/foo/bar');
179+
});
174180
});
175181

176182
describe('normalizeUrl', () => {

0 commit comments

Comments
 (0)