Loader Script

Learn about the Sentry JavaScript Loader Script

The Loader Script is the easiest way to initialize the Sentry SDK. The Loader Script also automatically keeps your Sentry SDK up to date and offers configuration for different Sentry features.

To use the loader, go in the Sentry UI to Settings > Projects > (select project) > Client Keys (DSN), and then press the "Configure" button. Copy the script tag from the "JavaScript Loader" section and include it as the first script on your page. By including it first, you allow it to catch and buffer events from any subsequent scripts, while still ensuring the full SDK doesn't load until after everything else has run.

Copied
<script
  src="https://js.sentry-cdn.com/examplePublicKey.min.js"
  crossorigin="anonymous"
></script>

By default, Tracing and Session Replay are disabled.

To have correct stack traces for minified asset files when using the Loader Script, you will have to either host your Source Maps publicly or upload them to Sentry.

The loader has a few configuration options:

  • What version of the SDK to load
  • Using Tracing
  • Using Session Replay
  • Showing debug logs

To configure the version, use the dropdown in the "JavaScript Loader" settings, directly beneath the script tag you copied earlier.

JavaScript Loader Settings

Note that because of caching, it can take a few minutes for version changes made here to take effect.

If you only use the Loader for errors, the loader won't load the full SDK until triggered by one of the following:

  • an unhandled error
  • an unhandled promise rejection
  • a call to Sentry.captureException
  • a call to Sentry.captureMessage
  • a call to Sentry.captureEvent

Once one of those occurs, the loader will buffer that event and immediately request the full SDK from our CDN. Any events that occur between that request being made and the completion of SDK initialization will also be buffered, and all buffered events will be sent to Sentry once the SDK is fully initialized.

Alternatively, you can set the loader to request the full SDK earlier: still as part of page load, but after all of the other JavaScript on the page has run. (In other words, in a subsequent event loop.) To do this, include data-lazy="no" in your script tag.

Copied
<script
  src="https://js.sentry-cdn.com/examplePublicKey.min.js"
  crossorigin="anonymous"
  data-lazy="no"
></script>

Finally, if you want to control the timing yourself, you can call Sentry.forceLoad(). You can do this as early as immediately after the loader runs (which has the same effect as setting data-lazy="no") and as late as the first unhandled error, unhandled promise rejection, or call to Sentry.captureMessage or Sentry.captureEvent (which has the same effect as not calling it at all). Note that you can't delay loading past one of the aforementioned triggering events.

If Tracing and/or Session Replay is enabled, the SDK will immediately fetch and initialize the bundle to make sure it can capture transactions and/or replays once the page loads.

While the Loader Script will work out of the box without any configuration in your application, you can still configure the SDK according to your needs.

For Tracing, the SDK will be initialized with tracesSampleRate: 1 by default. This means that the SDK will capture all traces.

For Session Replay, the defaults are replaysSessionSampleRate: 0.1 and replaysOnErrorSampleRate: 1. This means Replays will be captured for 10% of all normal sessions and for all sessions with an error.

You can configure the release by adding the following to your page:

Copied
<script>
  window.SENTRY_RELEASE = {
    id: "...",
  };
</script>

The loader script always includes a call to Sentry.init with a default configuration, including your DSN. If you want to configure your SDK beyond that, you can configure a custom init call by defining a window.sentryOnLoad function. Whatever is defined inside of this function will always be called first, before any other SDK method is called.

Be sure to define this function before you add the loader script, to ensure it can be called at the right time:

Copied
<script>
  // Configure sentryOnLoad before adding the Loader Script
  window.sentryOnLoad = function () {
    Sentry.init({
      // add custom config here
    });
  };
</script>

<script
  src="https://js.sentry-cdn.com/examplePublicKey.min.js"
  crossorigin="anonymous"
></script>

Inside of the window.sentryOnLoad function, you can configure a custom Sentry.init() call. You can configure your SDK exactly the way you would if you were using the CDN, with one difference: your Sentry.init() call doesn't need to include your DSN, since it's already been set. Inside of this function, the full Sentry SDK is guaranteed to be loaded & available.

Copied
<script>
  // Configure sentryOnLoad before adding the Loader Script
  window.sentryOnLoad = function () {
    Sentry.init({
      release: " ... ",
      environment: " ... "
    });
    Sentry.setTag(...);
    // etc.
  };
</script>

By default, the loader will make sure you can call these functions directly on Sentry at any time, even if the SDK is not yet loaded:

  • Sentry.captureException()
  • Sentry.captureMessage()
  • Sentry.captureEvent()
  • Sentry.addBreadcrumb()
  • Sentry.withScope()
  • Sentry.showReportDialog()

If you want to call any other method when using the Loader, you have to guard it with Sentry.onLoad(). Any callback given to onLoad() will be called either immediately (if the SDK is already loaded), or later once the SDK has been loaded:

Copied
<script>
  window.sentryOnLoad = function () {
    Sentry.init({
      // ...
    });
  };
</script>

<script
  src="https://js.sentry-cdn.com/examplePublicKey.min.js"
  crossorigin="anonymous"
></script>

<script>
  // Guard against window.Sentry not being available, e.g. due to Ad-blockers
  window.Sentry &&
    Sentry.onLoad(function () {
      // Inside of this callback,
      // we guarantee that `Sentry` is fully loaded and all APIs are available
      const client = Sentry.getClient();
      // do something custom here
    });
</script>

When using the Loader Script with just errors, the script injects the SDK asynchronously. This means that only unhandled errors and unhandled promise rejections will be caught and buffered before the SDK is fully loaded. Specifically, capturing breadcrumb data will not be available until the SDK is fully loaded and initialized. To reduce the amount of time these features are unavailable, set data-lazy="no" or call forceLoad() as described above.

If you want to understand the inner workings of the loader itself, you can read the documented source code in all its glory over at the Sentry repository.

Because the loader script injects the actual SDK asynchronously to keep your pageload performance high, the SDK's tracing functionality is only available once the SDK is loaded and initialized. This means that if you e.g. have fetch calls right at the beginning of your application, they might not be traced. If this is a critical issue for you, you have two options to ensure that all your fetch calls are traced:

  • Initialize the SDK in window.sentryOnLoad as described in Custom Configuration. Then make your fetch call in the Sentry.onload callback.
    Example
    Copied
    <script>
      window.sentryOnLoad = function () {
        Sentry.init({
          // ...
        });
      };
    </script>
    
    <script
      src="https://js.sentry-cdn.com/examplePublicKey.min.js"
      crossorigin="anonymous"
    ></script>
    
    <script>
      Sentry.onLoad(function () {
        fetch("/api/users");
      });
    </script>
    
  • Use the CDN bundles instead of the Loader Script. This will ensure that the SDK is loaded synchronously, and that all your fetch calls are traced.

Sentry supports loading the JavaScript SDK from a CDN. Generally we suggest using our Loader instead. If you must use a CDN, see Available Bundles below.

To use Sentry for error and tracing, you can use the following bundle:

Copied
<script
  src="https://browser.sentry-cdn.com/9.34.0/bundle.tracing.min.js"
  integrity="sha384-cRQDJUZkpn4UvmWYrVsTWGTyulY9B4H5Tp2s75ZVjkIAuu1TIxzabF3TiyubOsQ8"
  crossorigin="anonymous"
></script>

To use Sentry for error and tracing, as well as for Session Replay, you can use the following bundle:

Copied
<script
  src="https://browser.sentry-cdn.com/9.34.0/bundle.tracing.replay.min.js"
  integrity="sha384-gHcGsjf15+oILUd/CRoMCbLIjr/uvLY+dIT3+olcPVFtghwoWJjtIHCrDMaOkdbN"
  crossorigin="anonymous"
></script>

To use Sentry for error monitoring, as well as for Session Replay, but not for tracing, you can use the following bundle:

Copied
<script
  src="https://browser.sentry-cdn.com/9.34.0/bundle.replay.min.js"
  integrity="sha384-lZ1G75zByMnlFeZydgHd7zf/yOUL0qCVrb20JP6GNSPaSDKnRCOcJp1V1WExXX4b"
  crossorigin="anonymous"
></script>

If you only use Sentry for error monitoring, and don't need performance tracing or replay functionality, you can use the following bundle:

Copied
<script
  src="https://browser.sentry-cdn.com/9.34.0/bundle.min.js"
  integrity="sha384-53P6MMkVn0DDaKYIzeUJsL4myy0ml1QVsErYuIdCyys2xCGn9wplX9qhVMmqnl/B"
  crossorigin="anonymous"
></script>

Once you've included the Sentry SDK bundle in your page, you can use Sentry in your own bundle:

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  // this assumes your build process replaces `process.env.npm_package_version` with a value
  release: "my-project-name@" + process.env.npm_package_version,
  integrations: [
    // If you use a bundle with tracing enabled, add the BrowserTracing integration
    Sentry.browserTracingIntegration(),
    // If you use a bundle with session replay enabled, add the Replay integration
    Sentry.replayIntegration(),
  ],

  // We recommend adjusting this value in production, or using tracesSampler
  // for finer control
  tracesSampleRate: 1.0,

  // Set `tracePropagationTargets` to control for which URLs distributed tracing should be enabled
  tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],
});

Our CDN hosts a variety of bundles:

  • @sentry/browser with error monitoring only (named bundle.<modifiers>.js)
  • @sentry/browser with error and tracing (named bundle.tracing.<modifiers>.js)
  • @sentry/browser with error and session replay (named bundle.replay.<modifiers>.js)
  • @sentry/browser with error, tracing and session replay (named bundle.tracing.replay.<modifiers>.js)
  • each of the integrations in @sentry/integrations (named <integration-name>.<modifiers>.js)

Each bundle is offered in both ES6 and ES5 versions. Since v7 of the SDK, the bundles are ES6 by default. To use the ES5 bundle, add the .es5 modifier.

Each version has three bundle varieties:

  • minified (.min)
  • unminified (no .min), includes debug logging
  • minified with debug logging (.debug.min)

Bundles that include debug logging output more detailed log messages, which can be helpful for debugging problems. Make sure to enable debug to see debug messages in the console. Unminified and debug logging bundles have a greater bundle size than minified ones.

For example:

  • bundle.js is @sentry/browser, compiled to ES6 but not minified, with debug logging included (as it is for all unminified bundles)
  • rewriteframes.es5.min.js is the RewriteFrames integration, compiled to ES5 and minified, with no debug logging
  • bundle.tracing.es5.debug.min.js is @sentry/browser with tracing enabled, compiled to ES5 and minified, with debug logging included
FileIntegrity Checksum
browserprofiling.debug.min.jssha384-TvByuKsDHkXCCNOvTzbgGqDFrZdbsyX2U93F3hTUbf4SYrgutUfx0ngjlsCRPzmp
browserprofiling.jssha384-8QboHiPkTd9hdcHCgxN/wfeWoHh7wqX5mNXoKxfEvzDW04Yz17EeJQXF6gR01x5Q
browserprofiling.min.jssha384-BJg98HXxv4YXwbFt1A3TzIYTm18Uxfc1JoXO+JN2X1GumqbZSKbqUoAMUCWnFEab
bundle.debug.min.jssha384-J1qHQL5++54mHgNewN/w94d4DqtuBXatSFt7B43ss5+oxZ6aFph7QcLWAVrMWiSE
bundle.feedback.debug.min.jssha384-mkCsZIstDGzGeNiTWUE6R0cePKIiS6szABCF8HVNz8w+ZAhypIbCgx+HtJ3Bk7Jn
bundle.feedback.jssha384-CSvFIa4o9c22GlzgxriqKlIz8NqvHQx7z4C+OqDFL0RjIDkLn9ZTD+ePaZRXtXh9
bundle.feedback.min.jssha384-lg8gbaw1yIceFKiVZ+LfugEFTXcTxDpxkMg8m6NPvAeX2Xxj6kuyFcCtP1wNwa3h
bundle.jssha384-eHyAPKIAgcJaVmKIn6Y+MH9ASFosUgjeXWwUbysI5o0JY4dv9taH8irfm6/6DY2s
bundle.min.jssha384-hvRF3mi8KPAyi1wKSeNMGuGD2dH+g4tc+SApgh50hNrkidHfALex6VB2OBKdpHiC
bundle.replay.debug.min.jssha384-wXYKMmBdR5NR+0yYBg2XcZvsoTTeg26yrCOvPk9IsoEYy52MaCfEMjMAn351VYQ0
bundle.replay.jssha384-IgWFocIDic2rE0rJQ8ieJZjCwdHJiIFMbvWsPlkcB6eiv5G0BvmoYfD3bZ2gf77n
bundle.replay.min.jssha384-eSB4ODbMfGz+/BPTWeum8UW7z7m/PPRRRHvRxoM1HuINJXuyoEiamvfZoj9bzQ+j
bundle.tracing.debug.min.jssha384-0YZ8eck1Oz9YJsjXEBKa5DSNVOA/hZkQqaZdknQru3yjphea66qCCsRftnci3YYm
bundle.tracing.jssha384-8xCCi3rTsB8ljKEx10Yf1SzGWvsFtGNgA52SxPyggwSHhyHHwvy+ZuCU+E9cwX/a
bundle.tracing.min.jssha384-fiBQZNDvJBvPN2N/q/t4uH8LeFZOvI5Ke/5WAw2YDg2MGWV2iPcqqiaEke5QD9yg
bundle.tracing.replay.debug.min.jssha384-9XU6n1pjGwJQfHxDGHixTHqOFB50ahtgFDvDG2SMWdQV5T5OKUIXVfnwKmlGiouU
bundle.tracing.replay.feedback.debug.min.jssha384-cPyqUphbPU6GKfvem53XESKue83KULUuVt7l0a1KNxl6woJ0wPDbAm9O6z3xXHnP
bundle.tracing.replay.feedback.jssha384-a3lfTOCOazv/PeFOuST06fTSTWXKgMaiTwXntihH06DyqoaTiidwcinUOiaBhSZs
bundle.tracing.replay.feedback.min.jssha384-s98UlZBF92juRgDT1EIdEI+uM2OysFKkILC4iMxa+KAo7SiEJL7CM1pMdA3pNJJD
bundle.tracing.replay.jssha384-/T7GvKyT+I54rRIZppk4tr4sWIO/hiSM1Lh7xJsyz4ePybaYCB6eePIzinCyc3o2
bundle.tracing.replay.min.jssha384-dxzxBcold1WEwYSWgqiDmQYRYWYUgh+5vDztgIr+Pl88jN2V3wKpwCi/urMQ08Or
captureconsole.debug.min.jssha384-BIeXFJoD8Bx/1yuqW/Jmnyzc0Nw115l0tjCcpbYvl8S+s3dDLr3OzDqzgEKM/RuF
captureconsole.jssha384-nS+XjdDDzJ5rGWNeyP1wWGMJQ9/8XkpvX3J5kAQvHv90iyfnlcOm1FXesCAQ2f40
captureconsole.min.jssha384-0GHiTFr1MAuGFqp6pf9Hv8goCmaj+RttxjXoUbO6pkzhgRB8vtBiPkZDZv+WaYZ3
contextlines.debug.min.jssha384-DPKomLpjV31FW2UF6S+DE+MveTpU/mDQjEzDOyv7ujjsi/ABIyOL3vgRff1SSVb9
contextlines.jssha384-SxG+P9JuRf/Q+GVPqKSRVjsAa6xAvvBgM2Ma/W7/RfdtppNsrbrKSN3m2lD0TRw4
contextlines.min.jssha384-E1t9GwP03gatXZIBe3n7FJSxiKez89hfiSOQM3WR0MbC/NZL86P4v8PeA9erOi5b
dedupe.debug.min.jssha384-T/DXHe1XWDGiHQlZ1jGpMM0hnBytf2Y69whriJnB/cdxJdG62AZaF5SVtnDepJZ+
dedupe.jssha384-QMJB8UsXP9qxHMJi0iy/C8ZEW2IouroEq/0JrjE/GIlcXcYUiks8EQhuTQMlmSlv
dedupe.min.jssha384-t5Y+mnFKh/tcsJZCiIhXnBZfJZ1PaiEb2RNSbv/j8L7Sk+APQUmk3V/Lwu4Zb7iv
extraerrordata.debug.min.jssha384-pJXp/KYiHTyFjdOEijLKzBF1swgHCO1gJNxjig2qJ3IyhsL2o6WmUFJfKEQRICCV
extraerrordata.jssha384-UOw6qq++JbNJAkgDLeFAO/NYiuOYTv7YBzOHLf9hFYrClVBhbGcxfS6ngUr67BzG
extraerrordata.min.jssha384-QzUPmf9yoYOvL6UEdz+7XLeUa5SCAcnqTtgh232PAGX96iaub3nZmTDDi29S8NZH
feedback-modal.debug.min.jssha384-MPR+GUMrn4awniHe8RIQNpsSnbR51Yr/Ds6T/Uff2oiiHZ2nYPjaxqxgEuQaAj+r
feedback-modal.jssha384-zahffBALGmrL0Bf3iq+WqNxvANq7sNfzKV9ZARuJ6rFn1/85XULzyRjdmmpG+uFT
feedback-modal.min.jssha384-fZ9R2OMsOWz130gEKcfCg5MEuyfFvUOSVH7Y51OmoxVJLlPdteSgwPbjuzaYMR7z
feedback-screenshot.debug.min.jssha384-i8bvrxJqbArMGy6GkXUUv9bvV+ZojXzRlqJVFqfCIZ4t+9DODIdtW/2voNCOcLA/
feedback-screenshot.jssha384-TYdtXBddACc+lMy7qFScn9qzRUAXZgJ6xRRPuSOssvgIOAhLqg5meQPQ16Q+pPUJ
feedback-screenshot.min.jssha384-KfB4jSbGiCT+1yDhgu8aDk1EzJnCZ3+v/fqCEtO9vBdVBuzdE3ZN5FizP9kbloqd
feedback.debug.min.jssha384-J0jufiHOQ36it6QZ1CP/81UvRvb+Swg4EB7f0VLehIykxr6q5T06SP2fWUsNZZ5y
feedback.jssha384-K3ctUTM6c7mZFbHUpMvfnA9JFwYAyVpc4qUcJIME8osGsXod6E7XHNx31jJKNnTA
feedback.min.jssha384-zbuDcy1G8+czxa2d7RdenAL3qtXteX/QlomSgw6xaRMW1B/pGBkla7Zpo3mPqywO
graphqlclient.debug.min.jssha384-0MGjUt624Z0b5U7gcv+XZiTRpMcjwzr3TLqZTkWrm+1wqEjJpjRgX565pJ6CnKVB
graphqlclient.jssha384-HPZ8ApVfg9ar9V/kJUgb2R/l1m6JxDZ6mS42/ywC0+D5XiQ7LrTZhL8vh2Bb8Zh7
graphqlclient.min.jssha384-fmXo607HiYvDbjg7FxQs5oU1dGDnDieSTLfUMy0BbDd3A6gSOAQWxHHCNxHi6mS7
httpclient.debug.min.jssha384-Eq5bR8RhA9x6KsTZ0GhExTqv+AY0im0ZEVJiYIpFBurHWQPHbw1h/U3uzk02RtWa
httpclient.jssha384-SPRXlqaDHDyo08tTa++vXbY5REZchrdYx4rKePTz8FH8RHe6ApW+zIIRo8SwrFjN
httpclient.min.jssha384-wpETR8YEgoXgeFzgb4r4oFh9FHnqpz1z73mdr+OmQlKSFmpDsljl+F1iu3xIVFhQ
modulemetadata.debug.min.jssha384-TE3C6aDwxjCKJoTdTGCcZs0XZ/cJ95Z3VNw6iuV9UDLbwPEy/np4d72++p9VmzG6
modulemetadata.jssha384-tP0eaQuU0cu9lwywGWufSieE90LI1jCyEAmREZkwR/4votVA35Seu1baIjwsrH0f
modulemetadata.min.jssha384-WvoQitZ3/QiYv1SPUbS1ohJPxesDdGkFD925xsev3oTFPv5qgnNKKQS0ZIK+eOpb
multiplexedtransport.debug.min.jssha384-oLKzxEyYlc21i3m+JGsDnqsL2ZrWiHfHrtdkCQfIMetityth87wk/7g0326ZIold
multiplexedtransport.jssha384-HjEG/9s+FCSBGwUI2Vf267apnKlNGNbZFyX3CCFX8IZdS8s1uHbEYA3TY0uL7m/a
multiplexedtransport.min.jssha384-hSB1r90zXZSYuvyX4+VEFQ5UgI4QSNY4W/dlSyolLLmfybQkp+Ia3RV1OGyB3ZKr
replay-canvas.debug.min.jssha384-NwSRhCUaSRM4ZZyjHYrDRzMEPVLUS++TjPDbiNnrj2J7OdkrOHkvri8Xse13GYTU
replay-canvas.jssha384-+KB2CQ6PnYybuXZRRtMvA9CVTRgypAEw1ijNGkwy6QPQ+oo06Yf6Fy6f4Gej3NsH
replay-canvas.min.jssha384-KYCBbT6qQrPXc9xEnchTcyFumAD71NYhadohnG7O9zLnkxqCjkpsJW0KDGvPeH1W
replay.debug.min.jssha384-yZMxbQ6flVt4ffVU1Y+PG34ztbZODn+rBIUA+g2qRGBFh+si367oJTv/8lB722B4
replay.jssha384-8Wi63IYR13MXkd8Y/0nfvQgfVruJyK6whGa0Lg3cGtB/P9AsaWJorZyxXf19uYpx
replay.min.jssha384-OYd/lrg0SNKzPCfPIqwP2MyHDp5ufE9Dr+vNS9CDr4BvHPmzAXr1m2ewgu7Z8Qf5
reportingobserver.debug.min.jssha384-gJw0vRq5wYLK3kXTw3TzqaFpEPsibSG5YOuIFsNeuu0huyCIWoFSKmAL25jdbtLx
reportingobserver.jssha384-DMzkJ1+Xm3dgxHZW5782Wc04u7t+LQY7NilgJKb0yuLAoYo0qmTiRp0QQnRrofes
reportingobserver.min.jssha384-uCk91VVwyMBhxlHojOuQloqhoe0mzvRGCF9+hY1ct5yzbQ84crkR9DATIjHJeYRT
rewriteframes.debug.min.jssha384-sHF/m8cOjkmKzWdsAiRzEFuG+s0HttidMhuMGI9aOee+Wlh7NvWF8NPqvnQkX9xm
rewriteframes.jssha384-tfgBSlZFJ8NhsJNop4f5ZHtlBALcZJfbHGrG5A3mWbRP+1WwSUxQZqsAI5vLiYbm
rewriteframes.min.jssha384-Ef+pNn/oo+1qpr9Ck52gTaigkmh18gQ8qrhAu7zfEL0pTK4/roKEDIwe2ZpZw0U+
spotlight.debug.min.jssha384-SCVlAbVC2SuaQmEe9H+f8gnL78q80XF5ENJDnq0fHmCIrshRLDxJxjyvM14Msob3
spotlight.jssha384-45tC2cmaTRiWnvbhmafMP84s32CpQKUyPlmrFhNIgAUUxtuJGoGZs7XeiHa/6VEc
spotlight.min.jssha384-80DxXs1BLDh5RXcnTqkOshCDWBgR8JYbcrMUkeAgYB89gf1qPRer5ldsoZwbEjDh

To find the integrity hashes for older SDK versions, you can view our SDK release registry for the Browser SDK here.

If you use the defer script attribute, we strongly recommend that you place the script tag for the browser SDK first and mark all of your other scripts with defer (but not async). This will guarantee that that the Sentry SDK is executed before any of the others.

Without doing this you will find that it's possible for errors to occur before Sentry is loaded, which means you'll be flying blind to those issues.

If you have a Content Security Policy (CSP) set up on your site, you will need to add the script-src of wherever you're loading the SDK from, and the origin of your DSN. For example:

  • script-src: https://browser.sentry-cdn.com https://js.sentry-cdn.com
  • connect-src: *.sentry.io
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").