Browse Source

feat(loader): Allow to configure 'window.sentryOnLoad' (#58574)

Allow to configure a new `window.sentryOnLoad` callback, which can be
defined before the Loader Script is loaded, thus allowing to streamline
timing issues & prevent issues if e.g. the loader script was not loaded.

tests added here
https://github.com/getsentry/sentry-javascript/pull/9331

Closes https://github.com/getsentry/sentry/issues/58428
Francesco Novy 1 year ago
parent
commit
2c57b8aa8a

+ 1 - 1
scripts/build-js-loader.ts

@@ -42,7 +42,7 @@ async function run() {
   const {code: minifiedBase} = await minify(base, {
     ecma: 5,
     mangle: {
-      reserved: ['onLoad', 'forceLoad'],
+      reserved: ['onLoad', 'forceLoad', 'sentryOnLoad'],
     },
     format: {
       ecma: 5,

+ 8 - 0
src/sentry/templates/sentry/js-sdk-loader.js.tmpl

@@ -132,12 +132,20 @@
     }
     function setupSDK(SDK) {
         try {
+            // If defined, we call window.sentryOnLoad first
+            if (typeof _window.sentryOnLoad === 'function') {
+                _window.sentryOnLoad();
+                // Cleanup to allow garbage collection
+                _window.sentryOnLoad = undefined;
+            }
             // We have to make sure to call all callbacks first
             for (var i = 0; i < onLoadCallbacks.length; i++) {
                 if (typeof onLoadCallbacks[i] === 'function') {
                     onLoadCallbacks[i]();
                 }
             }
+            // Cleanup to allow garbage collection
+            onLoadCallbacks.splice(0);
             // First call all inits from the queue
             for (var i = 0; i < queue.length; i++) {
                 var item = queue[i];

File diff suppressed because it is too large
+ 0 - 0
src/sentry/templates/sentry/js-sdk-loader.min.js.tmpl


+ 15 - 1
src/sentry/templates/sentry/js-sdk-loader.ts

@@ -189,12 +189,21 @@ declare const __LOADER__IS_LAZY__: any;
 
   function setupSDK(SDK) {
     try {
+      // If defined, we call window.sentryOnLoad first
+      if (typeof _window.sentryOnLoad === 'function') {
+        _window.sentryOnLoad();
+        // Cleanup to allow garbage collection
+        _window.sentryOnLoad = undefined;
+      }
+
       // We have to make sure to call all callbacks first
       for (let i = 0; i < onLoadCallbacks.length; i++) {
         if (typeof onLoadCallbacks[i] === 'function') {
           onLoadCallbacks[i]();
         }
       }
+      // Cleanup to allow garbage collection
+      onLoadCallbacks.splice(0);
 
       // First call all inits from the queue
       for (let i = 0; i < queue.length; i++) {
@@ -284,7 +293,12 @@ declare const __LOADER__IS_LAZY__: any;
   }
 })(
   window as Window &
-    typeof globalThis & {SENTRY_SDK_SOURCE?: string; Sentry?: any; __SENTRY__?: any},
+    typeof globalThis & {
+      SENTRY_SDK_SOURCE?: string;
+      Sentry?: any;
+      __SENTRY__?: any;
+      sentryOnLoad?: () => void;
+    },
   document,
   'error' as const,
   'unhandledrejection' as const,

Some files were not shown because too many files changed in this diff