Skip to content

Commit 016e242

Browse files
authored
šŸ› [RUM-11848] Guard window/location access for non-browser environments (#4674)
1 parent 42dc96b commit 016e242

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

ā€Žpackages/core/src/browser/cookie.tsā€Ž

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
generateUUID,
77
} from '../tools/utils/stringUtils'
88
import { buildUrl } from '../tools/utils/urlPolyfill'
9+
import { globalObject } from '../tools/globalObject'
910

1011
export interface CookieOptions {
1112
secure?: boolean
@@ -75,7 +76,14 @@ export function deleteCookie(name: string, options?: CookieOptions) {
7576
* https://web.dev/same-site-same-origin/#site
7677
*/
7778
let getCurrentSiteCache: string | undefined
78-
export function getCurrentSite(hostname = location.hostname, referrer = document.referrer): string | undefined {
79+
export function getCurrentSite(
80+
hostname = globalObject.location?.hostname ?? '',
81+
referrer = typeof document !== 'undefined' ? document.referrer : ''
82+
): string | undefined {
83+
// Cookie probing requires document.cookie, which is not available in Web Workers or Node.js
84+
if (typeof document === 'undefined') {
85+
return undefined
86+
}
7987
if (getCurrentSiteCache === undefined) {
8088
const defaultHostName = getCookieDefaultHostName(hostname, referrer)
8189
if (defaultHostName) {

ā€Žpackages/core/src/domain/context/storeContextManager.tsā€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export function storeContextManager(
1616
productKey: string,
1717
customerDataType: CustomerDataType
1818
) {
19+
// window and localStorage are not available in Web Workers or Node.js environments
20+
if (typeof window === 'undefined' || typeof localStorage === 'undefined') {
21+
return
22+
}
23+
1924
const storageKey = buildStorageKey(productKey, customerDataType)
2025

2126
storageListeners.push(

ā€Žpackages/core/src/tools/utils/urlPolyfill.tsā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { globalObject } from '../globalObject'
22

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

77
export function isValidUrl(url: string) {

0 commit comments

Comments
Ā (0)