Skip to content

ref(loader): Streamline var usage #47316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/sentry/templates/sentry/js-sdk-loader.js.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load sentry_helpers %}(function sentryLoader(_window, _document, _script, _onerror, _onunhandledrejection, _namespace, _publicKey, _sdkBundleUrl, _config, _lazy) {
{% load sentry_helpers %}(function sentryLoader(_window, _document, _errorEvent, _unhandledrejectionEvent, _namespace, _publicKey, _sdkBundleUrl, _config, _lazy) {
var lazy = _lazy;
var forceLoad = false;
for (var i = 0; i < document.scripts.length; i++) {
Expand Down Expand Up @@ -59,14 +59,14 @@
// it was probably(?) a legacy behavior that they left to not modify few years old snippet
// https://www.html5rocks.com/en/tutorials/speed/script-loading/
var _currentScriptTag = _document.scripts[0];
var _newScriptTag = _document.createElement(_script);
var _newScriptTag = _document.createElement('script');
_newScriptTag.src = _sdkBundleUrl;
_newScriptTag.crossOrigin = 'anonymous';
// Once our SDK is loaded
_newScriptTag.addEventListener('load', function () {
try {
_window.removeEventListener('error', onError);
_window.removeEventListener('unhandledrejection', onUnhandledRejection);
_window.removeEventListener(_errorEvent, onError);
_window.removeEventListener(_unhandledrejectionEvent, onUnhandledRejection);
// Add loader as SDK source
_window.SENTRY_SDK_SOURCE = 'loader';
var SDK_1 = _window[_namespace];
Expand Down Expand Up @@ -148,8 +148,8 @@
}
// Because we installed the SDK, at this point we have an access to TraceKit's handler,
// which can take care of browser differences (eg. missing exception argument in onerror)
var tracekitErrorHandler = _window[_onerror];
var tracekitUnhandledRejectionHandler = _window[_onunhandledrejection];
var tracekitErrorHandler = _window.onerror;
var tracekitUnhandledRejectionHandler = _window.onunhandledrejection;
// And now capture all previously caught exceptions
for (var i = 0; i < data.length; i++) {
if ('e' in data[i] && tracekitErrorHandler) {
Expand Down Expand Up @@ -195,11 +195,11 @@
queue({ f: f, a: arguments });
};
});
_window.addEventListener('error', onError);
_window.addEventListener('unhandledrejection', onUnhandledRejection);
_window.addEventListener(_errorEvent, onError);
_window.addEventListener(_unhandledrejectionEvent, onUnhandledRejection);
if (!lazy) {
setTimeout(function () {
injectSdk(onLoadCallbacks);
});
}
})(window, document, 'script', 'onerror', 'onunhandledrejection', 'Sentry', '{{ publicKey|safe }}', '{{ jsSdkUrl|safe }}', {{ config|to_json|safe }}, {{ isLazy|safe|lower }});
})(window, document, 'error', 'unhandledrejection', 'Sentry', '{{ publicKey|safe }}', '{{ jsSdkUrl|safe }}', {{ config|to_json|safe }}, {{ isLazy|safe|lower }});
2 changes: 1 addition & 1 deletion src/sentry/templates/sentry/js-sdk-loader.min.js.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% load sentry_helpers %}!function(e,n,r,t,i,o,a,c,s,f){for(var u=f,forceLoad=!1,p=0;p<document.scripts.length;p++)if(document.scripts[p].src.indexOf(a)>-1){u&&"no"===document.scripts[p].getAttribute("data-lazy")&&(u=!1);break}var d=!1,l=[],_=function(e){("e"in e||"p"in e||e.f&&e.f.indexOf("capture")>-1||e.f&&e.f.indexOf("showReportDialog")>-1)&&u&&E(l),_.data.push(e)};function v(){_({e:[].slice.call(arguments)})}function h(e){_({p:"reason"in e?e.reason:"detail"in e&&"reason"in e.detail?e.detail.reason:e})}function E(a){if(!d){d=!0;var f=n.scripts[0],u=n.createElement(r);u.src=c,u.crossOrigin="anonymous",u.addEventListener("load",(function(){try{e.removeEventListener("error",v),e.removeEventListener("unhandledrejection",h),e.SENTRY_SDK_SOURCE="loader";var n=e[o],r=n.init;n.init=function(e){var t=s;for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);!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}(t,n),r(t)},function(n,r){try{for(var o=0;o<n.length;o++)"function"==typeof n[o]&&n[o]();var a=_.data,c=!(void 0===(d=e.__SENTRY__)||!d.hub||!d.hub.getClient());a.sort((function(e){return"init"===e.f?-1:0}));var s=!1;for(o=0;o<a.length;o++)if(a[o].f){s=!0;var f=a[o];!1===c&&"init"!==f.f&&r.init(),c=!0,r[f.f].apply(r,f.a)}!1===c&&!1===s&&r.init();var u=e[t],p=e[i];for(o=0;o<a.length;o++)"e"in a[o]&&u?u.apply(e,a[o].e):"p"in a[o]&&p&&p.apply(e,[a[o].p])}catch(e){console.error(e)}var d}(a,n)}catch(e){console.error(e)}})),f.parentNode.insertBefore(u,f)}}_.data=[],e[o]=e[o]||{},e[o].onLoad=function(e){l.push(e),u&&!forceLoad||E(l)},e[o].forceLoad=function(){forceLoad=!0,u&&setTimeout((function(){E(l)}))},["init","addBreadcrumb","captureMessage","captureException","captureEvent","configureScope","withScope","showReportDialog"].forEach((function(n){e[o][n]=function(){_({f:n,a:arguments})}})),e.addEventListener("error",v),e.addEventListener("unhandledrejection",h),u||setTimeout((function(){E(l)}))}(window,document,"script","onerror","onunhandledrejection","Sentry",'{{ publicKey|safe }}','{{ jsSdkUrl|safe }}',{{ config|to_json|safe }},{{ isLazy|safe|lower }});
{% 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 }});
26 changes: 12 additions & 14 deletions src/sentry/templates/sentry/js-sdk-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ declare const __LOADER__IS_LAZY__: any;
(function sentryLoader(
_window,
_document,
_script,
_onerror,
_onunhandledrejection,
_errorEvent,
_unhandledrejectionEvent,
_namespace,
_publicKey,
_sdkBundleUrl,
Expand Down Expand Up @@ -85,15 +84,15 @@ declare const __LOADER__IS_LAZY__: any;
// it was probably(?) a legacy behavior that they left to not modify few years old snippet
// https://www.html5rocks.com/en/tutorials/speed/script-loading/
const _currentScriptTag = _document.scripts[0];
const _newScriptTag = _document.createElement(_script) as HTMLScriptElement;
const _newScriptTag = _document.createElement('script') as HTMLScriptElement;
_newScriptTag.src = _sdkBundleUrl;
_newScriptTag.crossOrigin = 'anonymous';

// Once our SDK is loaded
_newScriptTag.addEventListener('load', function () {
try {
_window.removeEventListener('error', onError);
_window.removeEventListener('unhandledrejection', onUnhandledRejection);
_window.removeEventListener(_errorEvent, onError);
_window.removeEventListener(_unhandledrejectionEvent, onUnhandledRejection);

// Add loader as SDK source
_window.SENTRY_SDK_SOURCE = 'loader';
Expand Down Expand Up @@ -198,8 +197,8 @@ declare const __LOADER__IS_LAZY__: any;

// Because we installed the SDK, at this point we have an access to TraceKit's handler,
// which can take care of browser differences (eg. missing exception argument in onerror)
const tracekitErrorHandler = _window[_onerror];
const tracekitUnhandledRejectionHandler = _window[_onunhandledrejection];
const tracekitErrorHandler = _window.onerror;
const tracekitUnhandledRejectionHandler = _window.onunhandledrejection;

// And now capture all previously caught exceptions
for (let i = 0; i < data.length; i++) {
Expand Down Expand Up @@ -249,8 +248,8 @@ declare const __LOADER__IS_LAZY__: any;
};
});

_window.addEventListener('error', onError);
_window.addEventListener('unhandledrejection', onUnhandledRejection);
_window.addEventListener(_errorEvent, onError);
_window.addEventListener(_unhandledrejectionEvent, onUnhandledRejection);

if (!lazy) {
setTimeout(function () {
Expand All @@ -261,10 +260,9 @@ declare const __LOADER__IS_LAZY__: any;
window as Window &
typeof globalThis & {SENTRY_SDK_SOURCE?: string; Sentry?: any; __SENTRY__?: any},
document,
'script',
'onerror',
'onunhandledrejection',
'Sentry',
'error' as const,
'unhandledrejection' as const,
'Sentry' as const,
__LOADER__PUBLIC_KEY__,
__LOADER_SDK_URL__,
__LOADER__CONFIG__,
Expand Down