I am loading Sentry from CDN with the defer
attributed as per Using Defer in the Sentry docs.
I have a subsequent script
tag, that initializes Sentry using Sentry.init
, as per the Usage & Configuration docs.
This results in an Uncaught ReferenceError: Sentry is not defined
as Sentry.init
is executed before Sentry has time to load due to the deferred
attribute.
What is the best way to initialize Sentry when using the defer
attribute, so that errors that may occur in other scripts (which are also using defer
) are not lost?
The code extact:
<script defer
src="https://browser.sentry-cdn.com/7.42.0/bundle.tracing.replay.min.js"
integrity="sha384-1l6jGKe2vLyGsLU9U92iHngOrra5b0R13LKJan+gGw9ZdY7iK2ayrwusH0QaMjWI"
crossorigin="anonymous">
</script>
<script>
Sentry.init({
dsn: "https://[email protected]/123456",
release: "{{ SENTRY.dsn }}", //from Django Context
integrations: [new Sentry.BrowserTracing()],
tracesSampleRate: {{ SENTRY.traces_sample_rate }} //from Django Context,
});
</script>
// all other script tags are after Sentry and have a defer attributed (as per the docs).
Should I just extract the initialization logic into a separate JS file and then load it right after the Sentry CDN script tag also using defer
, or are there better solutions?