Skip to content

Commit 9b813e6

Browse files
authored
feat(loader): Sync loader with JS monorepo (#43880)
1 parent ce0bf92 commit 9b813e6

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
// content.p = promise rejection
3030
// content.f = function call the Sentry
3131
if (
32-
(content.e ||
33-
content.p ||
32+
('e' in content ||
33+
'p' in content ||
3434
(content.f && content.f.indexOf('capture') > -1) ||
3535
(content.f && content.f.indexOf('showReportDialog') > -1)) &&
3636
lazy
@@ -56,7 +56,7 @@
5656
// come out in the wrong order. Because of that we don't need async=1 as GA does.
5757
// it was probably(?) a legacy behavior that they left to not modify few years old snippet
5858
// https://www.html5rocks.com/en/tutorials/speed/script-loading/
59-
var _currentScriptTag = _document.getElementsByTagName(_script)[0];
59+
var _currentScriptTag = _document.scripts[0];
6060
var _newScriptTag = _document.createElement(_script);
6161
_newScriptTag.src = _sdkBundleUrl;
6262
_newScriptTag.crossOrigin = 'anonymous';
@@ -68,6 +68,9 @@
6868
_window[_onerror] = _oldOnerror;
6969
_window[_onunhandledrejection] = _oldOnunhandledrejection;
7070

71+
// Add loader as SDK source
72+
_window.SENTRY_SDK_SOURCE = 'loader';
73+
7174
var SDK = _window[_namespace];
7275

7376
var oldInit = SDK.init;
@@ -137,9 +140,9 @@
137140

138141
// And now capture all previously caught exceptions
139142
for (var i = 0; i < data.length; i++) {
140-
if (data[i].e && tracekitErrorHandler) {
143+
if ('e' in data[i] && tracekitErrorHandler) {
141144
tracekitErrorHandler.apply(_window, data[i].e);
142-
} else if (data[i].p && tracekitUnhandledRejectionHandler) {
145+
} else if ('p' in data[i] && tracekitUnhandledRejectionHandler) {
143146
tracekitUnhandledRejectionHandler.apply(_window, [data[i].p]);
144147
}
145148
}
@@ -198,9 +201,9 @@
198201

199202
// Do the same store/queue/call operations for `onunhandledrejection` event
200203
var _oldOnunhandledrejection = _window[_onunhandledrejection];
201-
_window[_onunhandledrejection] = function(exception) {
204+
_window[_onunhandledrejection] = function(e) {
202205
queue({
203-
p: exception.reason
206+
p: 'reason' in e ? e.reason : 'detail' in e && 'reason' in e.detail ? e.detail.reason : e
204207
});
205208
if (_oldOnunhandledrejection) _oldOnunhandledrejection.apply(_window, arguments);
206209
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load sentry_helpers %}(function(c,u,v,n,p,e,z,A,w){function k(a){if(!x){x=!0;var l=u.getElementsByTagName(v)[0],d=u.createElement(v);d.src=A;d.crossOrigin="anonymous";d.addEventListener("load",function(){try{c[n]=r;c[p]=t;var b=c[e],d=b.init;b.init=function(a){for(var b in a)Object.prototype.hasOwnProperty.call(a,b)&&(w[b]=a[b]);d(w)};B(a,b)}catch(g){console.error(g)}});l.parentNode.insertBefore(d,l)}}function B(a,l){try{for(var d=m.data,b=0;b<a.length;b++)if("function"===typeof a[b])a[b]();var e=!1,g=c.__SENTRY__;"undefined"!==
2-
typeof g&&g.hub&&g.hub.getClient()&&(e=!0);g=!1;for(b=0;b<d.length;b++)if(d[b].f){g=!0;var f=d[b];!1===e&&"init"!==f.f&&l.init();e=!0;l[f.f].apply(l,f.a)}!1===e&&!1===g&&l.init();var h=c[n],k=c[p];for(b=0;b<d.length;b++)d[b].e&&h?h.apply(c,d[b].e):d[b].p&&k&&k.apply(c,[d[b].p])}catch(C){console.error(C)}}for(var f=!0,y=!1,q=0;q<document.scripts.length;q++)if(-1<document.scripts[q].src.indexOf(z)){f="no"!==document.scripts[q].getAttribute("data-lazy");break}var x=!1,h=[],m=function(a){(a.e||a.p||a.f&&
3-
-1<a.f.indexOf("capture")||a.f&&-1<a.f.indexOf("showReportDialog"))&&f&&k(h);m.data.push(a)};m.data=[];c[e]=c[e]||{};c[e].onLoad=function(a){h.push(a);f&&!y||k(h)};c[e].forceLoad=function(){y=!0;f&&setTimeout(function(){k(h)})};"init addBreadcrumb captureMessage captureException captureEvent configureScope withScope showReportDialog".split(" ").forEach(function(a){c[e][a]=function(){m({f:a,a:arguments})}});var r=c[n];c[n]=function(a,e,d,b,f){m({e:[].slice.call(arguments)});r&&r.apply(c,arguments)};
4-
var t=c[p];c[p]=function(a){m({p:a.reason});t&&t.apply(c,arguments)};f||setTimeout(function(){k(h)})})(window,document,"script","onerror","onunhandledrejection","Sentry","{{ publicKey|safe }}","{{ jsSdkUrl|safe }}",{{ config|to_json|safe }});
1+
{% load sentry_helpers %}(function(c,w,C,p,q,k,D,E,x){function r(a){if(!y){y=!0;var f=w.scripts[0],d=w.createElement(C);d.src=E;d.crossOrigin="anonymous";d.addEventListener("load",function(){try{c[p]=u;c[q]=v;c.SENTRY_SDK_SOURCE="loader";var b=c[k],g=b.init;b.init=function(e){for(var h in e)Object.prototype.hasOwnProperty.call(e,h)&&(x[h]=e[h]);g(x)};F(a,b)}catch(e){console.error(e)}});f.parentNode.insertBefore(d,f)}}function F(a,f){try{for(var d=l.data,b=0;b<a.length;b++)if("function"===typeof a[b])a[b]();
2+
var g=!1,e=c.__SENTRY__;"undefined"!==typeof e&&e.hub&&e.hub.getClient()&&(g=!0);e=!1;for(b=0;b<d.length;b++)if(d[b].f){e=!0;var h=d[b];!1===g&&"init"!==h.f&&f.init();g=!0;f[h.f].apply(f,h.a)}!1===g&&!1===e&&f.init();var z=c[p],A=c[q];for(b=0;b<d.length;b++)"e"in d[b]&&z?z.apply(c,d[b].e):"p"in d[b]&&A&&A.apply(c,[d[b].p])}catch(G){console.error(G)}}for(var m=!0,B=!1,t=0;t<document.scripts.length;t++)if(-1<document.scripts[t].src.indexOf(D)){m="no"!==document.scripts[t].getAttribute("data-lazy");
3+
break}var y=!1,n=[],l=function(a){("e"in a||"p"in a||a.f&&-1<a.f.indexOf("capture")||a.f&&-1<a.f.indexOf("showReportDialog"))&&m&&r(n);l.data.push(a)};l.data=[];c[k]=c[k]||{};c[k].onLoad=function(a){n.push(a);m&&!B||r(n)};c[k].forceLoad=function(){B=!0;m&&setTimeout(function(){r(n)})};"init addBreadcrumb captureMessage captureException captureEvent configureScope withScope showReportDialog".split(" ").forEach(function(a){c[k][a]=function(){l({f:a,a:arguments})}});var u=c[p];c[p]=function(a,f,d,b,
4+
g){l({e:[].slice.call(arguments)});u&&u.apply(c,arguments)};var v=c[q];c[q]=function(a){l({p:"reason"in a?a.reason:"detail"in a&&"reason"in a.detail?a.detail.reason:a});v&&v.apply(c,arguments)};m||setTimeout(function(){r(n)})})(window,document,"script","onerror","onunhandledrejection","Sentry","{{ publicKey|safe }}","{{ jsSdkUrl|safe }}",{{ config|to_json|safe }});

0 commit comments

Comments
 (0)