browser-sdk
browser-sdk copied to clipboard
Do not produce side-effects
NPM package is producing side-effects, injecting itself in window.DD_RUM
at module level:
https://github.com/DataDog/browser-sdk/blob/main/packages/rum/src/entries/main.ts#L39
Just by importing this library it changes global namespace, which is something not desired when using a module system.
Is your feature request related to a problem? Please describe.
I have a big micro-frontend application where each part of the application was handling the initialization of v4 from this NPM package. I'm migrating it to a single global initialization using v5.
I could disable the initialization of the v4 in a central place while I gradually clean up, but, due to this issue, just by these parts of the application importing v4 it already messes up my v5 initialization. I'll have to first do a huge cleanup, losing monitoring in the process, only then to enable my global v5 init.
Describe the solution you'd like
I'd like this NPM package to not change the global scope.
As bonus it could even declare in its sideEffects: false
in its package.json
, which can increase the bundlers efficiency (although I don't expect improvements in this case). More info here: https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free
Describe alternatives you've considered
I tried to use the code bellow with my v5 initialization, to prevent it from being overwritten, but then the v4 imports start failing.
Object.defineProperty(window, 'DD_RUM', {
configurable: false,
enumerable: true,
writable: false,
value: window.DD_RUM,
});
Hello @romulof , We don't support several instances of the SDK on the same page. Maybe you could first move to single global initialization using v4 and then upgrade to v5.
I do get that my project has issues 😬, but it would be really good if Datadog didn't use global namespace. It could even remove the limitation of having multiple SDKs instances on the same page which could aid big projects to upgrade to new major versions of your library.
Mixpanel SDK does this. NPM version can only be accessed by module reference, and the one they deploy on a CDN uses window.mixpanel
.