Skip to content

Commit 0d952b5

Browse files
Add Resources and APM support
1 parent 2fcb2ee commit 0d952b5

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

packages/core/src/tools/globalObject.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T
1010
export type GlobalObject = XOR<Window, WorkerGlobalScope>
1111

1212
export function getGlobalObject<T = typeof globalThis>(): T {
13+
// Salesforce recommends to asign self to global: https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-js.html#third-party-library-sets-up-on-global-window-object
14+
if (typeof self === 'object') {
15+
return self as unknown as T
16+
}
17+
1318
return globalThis as unknown as T
1419
}
1520

@@ -21,3 +26,13 @@ export function getGlobalObject<T = typeof globalThis>(): T {
2126
export const globalObject = getGlobalObject<GlobalObject>()
2227

2328
export const isWorkerEnvironment = 'WorkerGlobalScope' in globalObject
29+
30+
export function getGlobalLocation<T extends { href: string } = Location>(): T | undefined {
31+
return (globalObject as unknown as { location?: T }).location
32+
}
33+
34+
// In Salesforce the location property must be used with accessors: https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-limitations.html#:~:text=The%20location%20property%20must%20be%20used%20with%20these%20accessors.
35+
// If not we crash. This way we mantain current behaviour while fallbacking safely in Salesforce.
36+
export function getGlobalLocationHref(): string {
37+
return getGlobalLocation()!.href
38+
}

packages/core/src/tools/utils/urlPolyfill.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { globalObject } from '../globalObject'
1+
import { getGlobalLocationHref, globalObject } from '../globalObject'
22

33
export function normalizeUrl(url: string) {
4-
return buildUrl(url, location.href).href
4+
return buildUrl(url, getGlobalLocationHref()).href
55
}
66

77
export function isValidUrl(url: string) {

packages/rum-core/src/domain/contexts/urlContexts.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
createValueHistory,
66
HookNames,
77
DISCARDED,
8-
mockable,
98
buildUrl,
9+
getGlobalLocationHref,
1010
} from '@datadog/browser-core'
1111
import type { LocationChange } from '../../browser/locationChangeObservable'
1212
import type { LifeCycle } from '../lifeCycle'
@@ -41,7 +41,7 @@ export function startUrlContexts(
4141
let previousViewUrl: string | undefined
4242

4343
lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_CREATED, ({ startClocks, url }) => {
44-
const locationHref = getLocationHref()
44+
const locationHref = getGlobalLocationHref()
4545
const viewUrl = url !== undefined ? buildUrl(url, locationHref).href : locationHref
4646

4747
urlContextHistory.add(
@@ -104,12 +104,3 @@ export function startUrlContexts(
104104
},
105105
}
106106
}
107-
108-
// Preserves existing global location behavior but in Salesforce access through explicit window as documented: https://developer.salesforce.com/docs/platform/lightning-components-security/guide/lws-limitations.html#:~:text=The%20location%20property,window.location
109-
function getLocationHref() {
110-
try {
111-
return mockable(location).href
112-
} catch {
113-
return mockable(window.location).href
114-
}
115-
}

0 commit comments

Comments
 (0)