Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Overuse of getGlobalObject<T>()? #5760

timfish started this conversation in RFCs
Discussion options

getGlobalObject seems to be overused throughout the SDKs. Why not use window in @sentry/browser and global in @sentry/node? What context/knowledge am I missing?

getGlobalObject was clearly designed to help get the global object when the code might be run on different runtimes (ie. frontend and backend) but those usages are minimal.

In many cases, it appears to be used to help extend the global type like this:

const global = getGlobalObject<
Window & {
__BUILD_MANIFEST?: {
sortedPages?: string[];
};
}
>();
const pageRoutes = (global.__BUILD_MANIFEST || {}).sortedPages;

But you can extend the global via declare global so the types work and simply use window:

declare global {
 const __BUILD_MANIFEST: { sortedPages?: string[] } | undefined;
}
const pageRoutes = (window.__BUILD_MANIFEST || {}).sortedPages; 

Do we avoid using declare global for the below global properties because this leaks into the global type for consumers of the SDKs?

interface SentryGlobal {
Sentry?: {
Integrations?: Integration[];
};
SENTRY_ENVIRONMENT?: string;
SENTRY_DSN?: string;
SENTRY_RELEASE?: {
id?: string;
};
__SENTRY__: {
globalEventProcessors: any;
hub: any;
logger: any;
};
}
You must be logged in to vote

Replies: 1 comment

Comment options

As per a @kamilogorek knowledge bomb on slack:

getGlobalObject seems to be overused throughout the SDKs. Why not use window in @sentry/browser and global in @sentry/node? What context/knowledge am I missing?

  • There is no window in WebWorker
  • There used to be no window in jest/phantomjs
  • There is no window in some obscure "compile-to" native environments
  • Also some of the code that uses getGlobalObject is shared across all SDKs, so we cannot choose for browser/node, the util has to do it
  • eg. getMainCarrier in hub uses it, and its shared for node/browser
  • And our main logger uses it as well
  • So effectively it has to be there. And if its there, why not use it everywhere instead of sometimes global in node, sometimes window in browser, sometimes getGlobalObject. Unification here is better, as you can easily grep for global API usages
  • It wont make bundle larger, as its there. And as for performance, its a single op for existence check, 0 overhead basically
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
RFCs
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /