Skip to content

Commit 4972604

Browse files
authored
fix: Use globalThis for code injection (#13132)
Since v8, all platforms and versions we support have support for `globalThis`. Since [this PR](#11351) we also use `globalThis` when reading these injectected values.
1 parent 772f945 commit 4972604

File tree

3 files changed

+7
-33
lines changed

3 files changed

+7
-33
lines changed

packages/nextjs/src/config/loaders/valueInjectionLoader.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ export default function valueInjectionLoader(this: LoaderThis<LoaderOptions>, us
1818
// We do not want to cache injected values across builds
1919
this.cacheable(false);
2020

21-
// Define some global proxy that works on server and on the browser.
22-
let injectedCode =
23-
'var _sentryCollisionFreeGlobalObject = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : {};\n';
24-
25-
Object.entries(values).forEach(([key, value]) => {
26-
injectedCode += `_sentryCollisionFreeGlobalObject["${key}"] = ${JSON.stringify(value)};\n`;
27-
});
21+
const injectedCode = Object.entries(values)
22+
.map(([key, value]) => `globalThis["${key}"] = ${JSON.stringify(value)};`)
23+
.join('\n');
2824

2925
return `${injectedCode}\n${userCode}`;
3026
}

packages/sveltekit/src/vite/injectGlobalValues.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,9 @@ export function getGlobalValueInjectionCode(globalSentryValues: GlobalSentryValu
2121
return '';
2222
}
2323

24-
const sentryGlobal = '_global';
25-
26-
const globalCode = `var ${sentryGlobal} =
27-
typeof window !== 'undefined' ?
28-
window :
29-
typeof globalThis !== 'undefined' ?
30-
globalThis :
31-
typeof global !== 'undefined' ?
32-
global :
33-
typeof self !== 'undefined' ?
34-
self :
35-
{};`;
3624
const injectedValuesCode = Object.entries(globalSentryValues)
37-
.map(([key, value]) => `${sentryGlobal}["${key}"] = ${JSON.stringify(value)};`)
25+
.map(([key, value]) => `globalThis["${key}"] = ${JSON.stringify(value)};`)
3826
.join('\n');
3927

40-
return `${globalCode}\n${injectedValuesCode}\n`;
28+
return `${injectedValuesCode}\n`;
4129
}

packages/sveltekit/test/vite/injectGlobalValues.test.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,8 @@ describe('getGlobalValueInjectionCode', () => {
99
something: 'else',
1010
__sentry_sveltekit_output_dir: '.svelte-kit/output',
1111
});
12-
expect(injectionCode).toEqual(`var _global =
13-
typeof window !== 'undefined' ?
14-
window :
15-
typeof globalThis !== 'undefined' ?
16-
globalThis :
17-
typeof global !== 'undefined' ?
18-
global :
19-
typeof self !== 'undefined' ?
20-
self :
21-
{};
22-
_global["something"] = "else";
23-
_global["__sentry_sveltekit_output_dir"] = ".svelte-kit/output";
12+
expect(injectionCode).toEqual(`globalThis["something"] = "else";
13+
globalThis["__sentry_sveltekit_output_dir"] = ".svelte-kit/output";
2414
`);
2515

2616
// Check that the code above is in fact valid and works as expected

0 commit comments

Comments
 (0)