Skip to content

Commit e7cf355

Browse files
committed
fix(loader): Improve loader callback handling
1 parent 94fd113 commit e7cf355

File tree

3 files changed

+66
-62
lines changed

3 files changed

+66
-62
lines changed

src/sentry/templates/sentry/js-sdk-loader.js.tmpl

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// We only want to lazy inject/load the sdk bundle if
2828
// an error or promise rejection occured
2929
// OR someone called `capture...` on the SDK
30-
injectSdk(onLoadCallbacks);
30+
injectSdk();
3131
}
3232
queue.data.push(content);
3333
};
@@ -47,7 +47,7 @@
4747
: e,
4848
});
4949
}
50-
function injectSdk(callbacks) {
50+
function injectSdk() {
5151
if (injected) {
5252
return;
5353
}
@@ -64,29 +64,31 @@
6464
_newScriptTag.crossOrigin = 'anonymous';
6565
// Once our SDK is loaded
6666
_newScriptTag.addEventListener('load', function () {
67-
try {
68-
_window.removeEventListener(_errorEvent, onError);
69-
_window.removeEventListener(_unhandledrejectionEvent, onUnhandledRejection);
70-
// Add loader as SDK source
71-
_window.SENTRY_SDK_SOURCE = 'loader';
72-
var SDK_1 = _window[_namespace];
73-
var oldInit_1 = SDK_1.init;
74-
// Configure it using provided DSN and config object
75-
SDK_1.init = function (options) {
76-
var target = _config;
77-
for (var key in options) {
78-
if (Object.prototype.hasOwnProperty.call(options, key)) {
79-
target[key] = options[key];
67+
setTimeout(function () {
68+
try {
69+
_window.removeEventListener(_errorEvent, onError);
70+
_window.removeEventListener(_unhandledrejectionEvent, onUnhandledRejection);
71+
// Add loader as SDK source
72+
_window.SENTRY_SDK_SOURCE = 'loader';
73+
var SDK_1 = _window[_namespace];
74+
var oldInit_1 = SDK_1.init;
75+
// Configure it using provided DSN and config object
76+
SDK_1.init = function (options) {
77+
var target = _config;
78+
for (var key in options) {
79+
if (Object.prototype.hasOwnProperty.call(options, key)) {
80+
target[key] = options[key];
81+
}
8082
}
81-
}
82-
setupDefaultIntegrations(target, SDK_1);
83-
oldInit_1(target);
84-
};
85-
sdkLoaded(callbacks, SDK_1);
86-
}
87-
catch (o_O) {
88-
console.error(o_O);
89-
}
83+
setupDefaultIntegrations(target, SDK_1);
84+
oldInit_1(target);
85+
};
86+
sdkLoaded(SDK_1);
87+
}
88+
catch (o_O) {
89+
console.error(o_O);
90+
}
91+
});
9092
});
9193
_currentScriptTag.parentNode.insertBefore(_newScriptTag, _currentScriptTag);
9294
}
@@ -115,12 +117,12 @@
115117
__sentry.hub &&
116118
__sentry.hub.getClient());
117119
}
118-
function sdkLoaded(callbacks, SDK) {
120+
function sdkLoaded(SDK) {
119121
try {
120122
// We have to make sure to call all callbacks first
121-
for (var i = 0; i < callbacks.length; i++) {
122-
if (typeof callbacks[i] === 'function') {
123-
callbacks[i]();
123+
for (var i = 0; i < onLoadCallbacks.length; i++) {
124+
if (typeof onLoadCallbacks[i] === 'function') {
125+
onLoadCallbacks[i]();
124126
}
125127
}
126128
var data = queue.data;
@@ -171,13 +173,13 @@
171173
if (lazy && !forceLoad) {
172174
return;
173175
}
174-
injectSdk(onLoadCallbacks);
176+
injectSdk();
175177
};
176178
_window[_namespace].forceLoad = function () {
177179
forceLoad = true;
178180
if (lazy) {
179181
setTimeout(function () {
180-
injectSdk(onLoadCallbacks);
182+
injectSdk();
181183
});
182184
}
183185
};
@@ -199,7 +201,7 @@
199201
_window.addEventListener(_unhandledrejectionEvent, onUnhandledRejection);
200202
if (!lazy) {
201203
setTimeout(function () {
202-
injectSdk(onLoadCallbacks);
204+
injectSdk();
203205
});
204206
}
205207
})(window, document, 'error', 'unhandledrejection', 'Sentry', '{{ publicKey|safe }}', '{{ jsSdkUrl|safe }}', {{ config|to_json|safe }}, {{ isLazy|safe|lower }});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{% load sentry_helpers %}!function(e,n,r,t,i,o,a,c,s){for(var f=s,forceLoad=!1,u=0;u<document.scripts.length;u++)if(document.scripts[u].src.indexOf(o)>-1){f&&"no"===document.scripts[u].getAttribute("data-lazy")&&(f=!1);break}var p=!1,d=[],l=function(e){("e"in e||"p"in e||e.f&&e.f.indexOf("capture")>-1||e.f&&e.f.indexOf("showReportDialog")>-1)&&f&&h(d),l.data.push(e)};function _(){l({e:[].slice.call(arguments)})}function v(e){l({p:"reason"in e?e.reason:"detail"in e&&"reason"in e.detail?e.detail.reason:e})}function h(o){if(!p){p=!0;var s=n.scripts[0],f=n.createElement("script");f.src=a,f.crossOrigin="anonymous",f.addEventListener("load",(function(){try{e.removeEventListener(r,_),e.removeEventListener(t,v),e.SENTRY_SDK_SOURCE="loader";var n=e[i],a=n.init;n.init=function(e){var r=c;for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[t]=e[t]);!function(e,n){var r=e.integrations||[];if(!Array.isArray(r))return;var t=r.map((function(e){return e.name}));e.tracesSampleRate&&-1===t.indexOf("BrowserTracing")&&r.push(new n.BrowserTracing);(e.replaysSessionSampleRate||e.replaysOnErrorSampleRate)&&-1===t.indexOf("Replay")&&r.push(new n.Replay);e.integrations=r}(r,n),a(r)},function(n,r){try{for(var t=0;t<n.length;t++)"function"==typeof n[t]&&n[t]();var i=l.data,o=!(void 0===(u=e.__SENTRY__)||!u.hub||!u.hub.getClient());i.sort((function(e){return"init"===e.f?-1:0}));var a=!1;for(t=0;t<i.length;t++)if(i[t].f){a=!0;var c=i[t];!1===o&&"init"!==c.f&&r.init(),o=!0,r[c.f].apply(r,c.a)}!1===o&&!1===a&&r.init();var s=e.onerror,f=e.onunhandledrejection;for(t=0;t<i.length;t++)"e"in i[t]&&s?s.apply(e,i[t].e):"p"in i[t]&&f&&f.apply(e,[i[t].p])}catch(e){console.error(e)}var u}(o,n)}catch(e){console.error(e)}})),s.parentNode.insertBefore(f,s)}}l.data=[],e[i]=e[i]||{},e[i].onLoad=function(e){d.push(e),f&&!forceLoad||h(d)},e[i].forceLoad=function(){forceLoad=!0,f&&setTimeout((function(){h(d)}))},["init","addBreadcrumb","captureMessage","captureException","captureEvent","configureScope","withScope","showReportDialog"].forEach((function(n){e[i][n]=function(){l({f:n,a:arguments})}})),e.addEventListener(r,_),e.addEventListener(t,v),f||setTimeout((function(){h(d)}))}(window,document,"error","unhandledrejection","Sentry",'{{ publicKey|safe }}','{{ jsSdkUrl|safe }}',{{ config|to_json|safe }},{{ isLazy|safe|lower }});
1+
{% load sentry_helpers %}!function(e,n,t,r,i,o,a,c,s){for(var f=s,forceLoad=!1,u=0;u<document.scripts.length;u++)if(document.scripts[u].src.indexOf(o)>-1){f&&"no"===document.scripts[u].getAttribute("data-lazy")&&(f=!1);break}var p=!1,d=[],l=function(e){("e"in e||"p"in e||e.f&&e.f.indexOf("capture")>-1||e.f&&e.f.indexOf("showReportDialog")>-1)&&f&&h(),l.data.push(e)};function _(){l({e:[].slice.call(arguments)})}function v(e){l({p:"reason"in e?e.reason:"detail"in e&&"reason"in e.detail?e.detail.reason:e})}function h(){if(!p){p=!0;var o=n.scripts[0],s=n.createElement("script");s.src=a,s.crossOrigin="anonymous",s.addEventListener("load",(function(){setTimeout((function(){try{e.removeEventListener(t,_),e.removeEventListener(r,v),e.SENTRY_SDK_SOURCE="loader";var n=e[i],o=n.init;n.init=function(e){var t=c;for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);!function(e,n){var t=e.integrations||[];if(!Array.isArray(t))return;var r=t.map((function(e){return e.name}));e.tracesSampleRate&&-1===r.indexOf("BrowserTracing")&&t.push(new n.BrowserTracing);(e.replaysSessionSampleRate||e.replaysOnErrorSampleRate)&&-1===r.indexOf("Replay")&&t.push(new n.Replay);e.integrations=t}(t,n),o(t)},function(n){try{for(var t=0;t<d.length;t++)"function"==typeof d[t]&&d[t]();var r=l.data,i=!(void 0===(f=e.__SENTRY__)||!f.hub||!f.hub.getClient());r.sort((function(e){return"init"===e.f?-1:0}));var o=!1;for(t=0;t<r.length;t++)if(r[t].f){o=!0;var a=r[t];!1===i&&"init"!==a.f&&n.init(),i=!0,n[a.f].apply(n,a.a)}!1===i&&!1===o&&n.init();var c=e.onerror,s=e.onunhandledrejection;for(t=0;t<r.length;t++)"e"in r[t]&&c?c.apply(e,r[t].e):"p"in r[t]&&s&&s.apply(e,[r[t].p])}catch(e){console.error(e)}var f}(n)}catch(e){console.error(e)}}))})),o.parentNode.insertBefore(s,o)}}l.data=[],e[i]=e[i]||{},e[i].onLoad=function(e){d.push(e),f&&!forceLoad||h()},e[i].forceLoad=function(){forceLoad=!0,f&&setTimeout((function(){h()}))},["init","addBreadcrumb","captureMessage","captureException","captureEvent","configureScope","withScope","showReportDialog"].forEach((function(n){e[i][n]=function(){l({f:n,a:arguments})}})),e.addEventListener(t,_),e.addEventListener(r,v),f||setTimeout((function(){h()}))}(window,document,"error","unhandledrejection","Sentry",'{{ publicKey|safe }}','{{ jsSdkUrl|safe }}',{{ config|to_json|safe }},{{ isLazy|safe|lower }});

src/sentry/templates/sentry/js-sdk-loader.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ declare const __LOADER__IS_LAZY__: any;
4747
// We only want to lazy inject/load the sdk bundle if
4848
// an error or promise rejection occured
4949
// OR someone called `capture...` on the SDK
50-
injectSdk(onLoadCallbacks);
50+
injectSdk();
5151
}
5252
queue.data.push(content);
5353
};
@@ -71,7 +71,7 @@ declare const __LOADER__IS_LAZY__: any;
7171
});
7272
}
7373

74-
function injectSdk(callbacks) {
74+
function injectSdk() {
7575
if (injected) {
7676
return;
7777
}
@@ -90,34 +90,36 @@ declare const __LOADER__IS_LAZY__: any;
9090

9191
// Once our SDK is loaded
9292
_newScriptTag.addEventListener('load', function () {
93-
try {
94-
_window.removeEventListener(_errorEvent, onError);
95-
_window.removeEventListener(_unhandledrejectionEvent, onUnhandledRejection);
93+
setTimeout(() => {
94+
try {
95+
_window.removeEventListener(_errorEvent, onError);
96+
_window.removeEventListener(_unhandledrejectionEvent, onUnhandledRejection);
9697

97-
// Add loader as SDK source
98-
_window.SENTRY_SDK_SOURCE = 'loader';
98+
// Add loader as SDK source
99+
_window.SENTRY_SDK_SOURCE = 'loader';
99100

100-
const SDK = _window[_namespace];
101+
const SDK = _window[_namespace];
101102

102-
const oldInit = SDK.init;
103+
const oldInit = SDK.init;
103104

104-
// Configure it using provided DSN and config object
105-
SDK.init = function (options) {
106-
const target = _config;
107-
for (const key in options) {
108-
if (Object.prototype.hasOwnProperty.call(options, key)) {
109-
target[key] = options[key];
105+
// Configure it using provided DSN and config object
106+
SDK.init = function (options) {
107+
const target = _config;
108+
for (const key in options) {
109+
if (Object.prototype.hasOwnProperty.call(options, key)) {
110+
target[key] = options[key];
111+
}
110112
}
111-
}
112113

113-
setupDefaultIntegrations(target, SDK);
114-
oldInit(target);
115-
};
114+
setupDefaultIntegrations(target, SDK);
115+
oldInit(target);
116+
};
116117

117-
sdkLoaded(callbacks, SDK);
118-
} catch (o_O) {
119-
console.error(o_O);
120-
}
118+
sdkLoaded(SDK);
119+
} catch (o_O) {
120+
console.error(o_O);
121+
}
122+
});
121123
});
122124

123125
_currentScriptTag.parentNode!.insertBefore(_newScriptTag, _currentScriptTag);
@@ -159,12 +161,12 @@ declare const __LOADER__IS_LAZY__: any;
159161
);
160162
}
161163

162-
function sdkLoaded(callbacks, SDK) {
164+
function sdkLoaded(SDK) {
163165
try {
164166
// We have to make sure to call all callbacks first
165-
for (let i = 0; i < callbacks.length; i++) {
166-
if (typeof callbacks[i] === 'function') {
167-
callbacks[i]();
167+
for (let i = 0; i < onLoadCallbacks.length; i++) {
168+
if (typeof onLoadCallbacks[i] === 'function') {
169+
onLoadCallbacks[i]();
168170
}
169171
}
170172

@@ -221,14 +223,14 @@ declare const __LOADER__IS_LAZY__: any;
221223
if (lazy && !forceLoad) {
222224
return;
223225
}
224-
injectSdk(onLoadCallbacks);
226+
injectSdk();
225227
};
226228

227229
_window[_namespace].forceLoad = function () {
228230
forceLoad = true;
229231
if (lazy) {
230232
setTimeout(function () {
231-
injectSdk(onLoadCallbacks);
233+
injectSdk();
232234
});
233235
}
234236
};
@@ -253,7 +255,7 @@ declare const __LOADER__IS_LAZY__: any;
253255

254256
if (!lazy) {
255257
setTimeout(function () {
256-
injectSdk(onLoadCallbacks);
258+
injectSdk();
257259
});
258260
}
259261
})(

0 commit comments

Comments
 (0)