Skip to content

Commit 87be028

Browse files
committed
get location data from global object instead of window to account for service workers
1 parent ec0f1d7 commit 87be028

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SampleContext, TransactionContext } from '@sentry/types';
33
import {
44
dynamicRequire,
55
extractNodeRequestData,
6+
getGlobalObject,
67
hasTracingEnabled,
78
isInstanceOf,
89
isNodeEnv,
@@ -123,9 +124,14 @@ function getDefaultSampleContext(): SampleContext {
123124

124125
// we must be in browser-js (or some derivative thereof)
125126
else {
126-
// we take a copy of the location object rather than just a reference to it in case there's a navigation in the
127-
// instant between when the transaction starts and when the sampler is called
128-
defaultSampleContext.location = { ...window.location };
127+
// we use `getGlobalObject()` rather than `window` since service workers also have a `location` property on `self`
128+
const globalObject = getGlobalObject();
129+
130+
if ('location' in globalObject) {
131+
// we take a copy of the location object rather than just a reference to it in case there's a navigation or
132+
// redirect in the instant between when the transaction starts and when the sampler is called
133+
defaultSampleContext.location = { ...globalObject.location };
134+
}
129135
}
130136

131137
return defaultSampleContext;

0 commit comments

Comments
 (0)