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-smjHaGAxe7gPj2hRZMPXDvsSE2xbhmFDziPCiz3fkX9vtJNB34DDFhUQR8OcwQiH
browserprofiling.jssha384-aTOoCEuhMI6bNjO2HpmVgN7C65pa4fUC9RtbUEEzVmmoqsfYkccyPgqKUlQpc/ov
browserprofiling.min.jssha384-OnEQ4SgrpvHzQThZQ9lpYTPX3RLYbjb9Jc74la5Cv82VqO0Hjj7OIUD5to6JnRO9
bundle.debug.min.jssha384-fHoiSY7rzRHwriHuwNUgYzmEbuCjor1PDp1snd9AwA9tu5YMk5lPDc4rAxx3aSKw
bundle.feedback.debug.min.jssha384-2v4gMNPgxjV9MQ9ou2E6qs5eEnGbhJbLgoLp+kaRAaNMILK7onK/vS1xQJMWOMe/
bundle.feedback.jssha384-pKiJcwPaf7V/IfolM3O1C6VIuuLpezP68QqlddqB+Hf09yO/9D8k8Rc5fdyoRbn8
bundle.feedback.min.jssha384-AG3Smpk4VBPnuS8aqlCcQ/b1StDea7AIQggnlTNGJdwb9yCXh+ec1XBIDOsXD8AM
bundle.jssha384-Y9DYlz+gbUIs0au2NS20XFyuVLWvptY+sPHvUKdQZdK+aSaY6sSw9LgjEs0B9b4G
bundle.min.jssha384-H72Peo2y6Da6X3Z7RoB4JXmMYXrqZcC+uBtAaM46ozsJh//h1QZsx1JEpg+fhUe9
bundle.replay.debug.min.jssha384-srdWhF0j3jLzoyTWh8wlQuVpxtYjtDAITDewJxjhWvVQVxygqboDkg9u008lC0W9
bundle.replay.jssha384-xKCfCpdtpucSkTWVPSDLry9msu6FRA/YmtUZurgiWty2LfrT7RCRHHiLggsCoiyp
bundle.replay.min.jssha384-4Xi7h/bvEnPW+quV8uesNNn4LIc3CuAAjNkT+yPdaalssdH26eM5PZdA1dM5z7Td
bundle.tracing.debug.min.jssha384-yvcMyOvx/WWNaJacyfhUpcAlCt0JcstCCFdHBMOLCKuIwI7t9aeEDNHLGPOsr8o0
bundle.tracing.jssha384-6mQDP8epV/wIFU3J1pbreZeRdhL68Tnw86ah824RIL06SddbDoITV6KC8LQq0wNr
bundle.tracing.min.jssha384-X+MnE4QFuDfouGcZGE2DAAHfa0c/MLpGLnbtG/OORX+AjaOFwIoTirVwcFRa7UQQ
bundle.tracing.replay.debug.min.jssha384-CmSYiQE1KhORsX+Iy0a9PR7lojT1DE0EqAlobxBq2l8rqYwWcZC8BoftuKfkDfc0
bundle.tracing.replay.feedback.debug.min.jssha384-yHEfBvwZstpDlw4SELYY6pquWxooXziclUzWa05yzQp1T3Lk4WZqOH9Hn/4I+OFm
bundle.tracing.replay.feedback.jssha384-b6YLFM+OIr5tboGIrCzCA26I3ymq1k0SpQlxgkjM7xEKxx6zYGOzjmx8Bz386vph
bundle.tracing.replay.feedback.min.jssha384-Tw1qL1+9eqwTF+viHCvnqfSPAhxQvBXe9AcOcWdGmTqsil/RikOs1wuZGUe2Nlnq
bundle.tracing.replay.jssha384-wNDdoFlbs4Ft0E0EiVWrgNwsr55F1obK5r8vapTX6IH0hjq/IRRKUnTIdq3ecb8e
bundle.tracing.replay.min.jssha384-qrnfn9JfsWToxvx9D/X2TTgbZ6QdcOGj147N5W8NgKt7zWfBWR+PWr+OeboGMeoi
captureconsole.debug.min.jssha384-8U2WcymPku27XpIy0nZpguC0cVYiYiXnppzJeahgvU8eTDmV9PZubpJd5GMEwX5P
captureconsole.jssha384-JhoMJRsjj8JQmiK0UuB3x0+TIg9xGLKtydltLzJvGrgbw9svESzL9xERGgdrPLYr
captureconsole.min.jssha384-8XjkIpx3LLNJss/ehkO5WBemhBDxYWdPjAFM1nkaE42Mk7XbpUHtxoWIe+8hKcp2
contextlines.debug.min.jssha384-QD6mZ9MFs/MsK6indC4SX7Q/Ow4M0oQRwQd+SjmZwnXcEZaAwWhE98pCJg6SzxB3
contextlines.jssha384-zMcu/1cP6teBbF8VS0iePeKrUvAlohreJlW+HHaYx+Z9hubNzgTpw70i/lTDu8nh
contextlines.min.jssha384-L8I7Z8wEdm+ZhdYuVAqRtzjiaJeUjnCpcWhhT0z86PfI4tI9Jl8GmRVnVEk6/Jxz
dedupe.debug.min.jssha384-EBO1/5+R6B8L9TX931pI524ArOCBO17f1VLxO3NRDdYaFpCsaaBt3Enn/CqYXr6O
dedupe.jssha384-uBCyk5f0zj9NkQ5fqJDxR2JZRTsBxUU/33JxE+mDGHDk7NeocIDb1vG3KbrilEOJ
dedupe.min.jssha384-c+aYb72LyYBzv/ErXlWsyFXhd1oQH8M9H0b3HdywOAeu0YXQeatwZelZxpJ3zvVJ
extraerrordata.debug.min.jssha384-Az8UdOJ0UL3DOm8ac8pAs8HGue1fDov8a+hI6uYpQ9alJwUW4JK7eVJilVlMTqQZ
extraerrordata.jssha384-dJWeRB/o9q77lB/ghiQuuzW9fV2Xwdjm3baQc5jbr+Smu1vnoSqKVW78MlVwtIvw
extraerrordata.min.jssha384-kmBucKeiBfc4C4LVqRr2khLI2paCynL48bMNaZ70iZVfzUkM0n4cPLQsWnshzJX0
feedback-modal.debug.min.jssha384-bswPLPBhOVSCq++glA4Gwsm31+Vj+lYnrtySU56oJfejynkffqaHoUbh1A5DGB1O
feedback-modal.jssha384-1fT/IMPo0iUma6RkyTul5bOSzdpKfnslty0MGSCHqNsedcYbDCu8+jIo/AkziAq9
feedback-modal.min.jssha384-sJBNlVW9U9Ngg6YOFA9FYeqRv2gQxSSGMa/AfWpopliRdL+/w4stUF1cSR8YFkrJ
feedback-screenshot.debug.min.jssha384-BJ/UB29lHKhNEPdtqHmqtaJ8CDh6GHSv5hY/9NEs4x7vM6zT0aZlnicpCrTAcPrD
feedback-screenshot.jssha384-q92XfyYFvNBBzYjM3x498cxJPsPhU1CrTklnqIziSM0AUWLqmPfSB2MLgMVhkVmo
feedback-screenshot.min.jssha384-+YmyKC1nVl0KrT7luVtRgR1LsL/E9YpGRwKGnb4SRHIVIVEb0KgwyuE5ULFQpxN+
feedback.debug.min.jssha384-rhOwsg2gHILMoEt8eTxRJ4Z6p328NjyEhQXZXImR9ptYNgXxQ4DtHzNN3c7jrat7
feedback.jssha384-jiKM30xfU9ZVT+ER60/C1/Y3r+jxRV5mrafNfvKeuRdjOGDBsywtfITKMiyN1LLA
feedback.min.jssha384-k8oZcxdDWhC1s8R5j8kyfeDDNad2sUejMwvWKFv6WuwztWBBGmq/Q80zDDwYSe4D
graphqlclient.debug.min.jssha384-DgfWr1FHshqLXk67dh67Wl3N3/QPDXfrK1Y0Huz3YPY7T4a6jpLBU0oOQBldtU0D
graphqlclient.jssha384-niYn92WPjX8H7UNicOvmcuKsYt4Cn9LSvJMCmlZvHe/DQpuhZ3XjGMov4WeBOpow
graphqlclient.min.jssha384-2eA8lfi2yg7vA7eK/WuIgSYNswjpfxyNiVyQsjrOekEzWAxatG8UqdLBx8IZDiM2
httpclient.debug.min.jssha384-Jj/+jC/koNBuzWO/6bSwz9rTAL2dzQUuEdsvdrn5SLfMJQrMPF9vzhEl2up1Zzht
httpclient.jssha384-bqNoWpL5C1q12lYHQUdtwlJU+S6TNXmaU4mOdJBzUNUv53HA6lupENLWxJR5570i
httpclient.min.jssha384-nN1TvSYSi40yTEncUEHyBE+BQ0HfPz4PI86JIHvaJ+M5klVz/phVN88t72YijL8O
modulemetadata.debug.min.jssha384-KFH2NB8ybkWkMNJ1V7YryEP72D8s0FaC82iJCvLEk6V4up6BeWm7J3bWF4WLzLkf
modulemetadata.jssha384-QZt4S0/it9SKFeDfm4MKmtojiqWB+sRUTOaTD75dIJhqyO/2WckFmyWTuZOMud+H
modulemetadata.min.jssha384-V83kKJ6nSSIeDfxvgz/iOX+lRFPKQ1Ykwpf1/0nUGPmPxtnz+l0coOWazDyVzInx
multiplexedtransport.debug.min.jssha384-8Be5GLB0AztLPtT8Vx9khIuJ3/KcotV1S5sWHH+G1EoQIQBjMv8JI+RD9JcC2u+X
multiplexedtransport.jssha384-GwKcZxhWFpX3FOC+dI8Ru1U89vRItBMcFTOdvJ1MMTND99RXTCbE9j4P+PV8k5I/
multiplexedtransport.min.jssha384-HBDkIpwGZIFgvBusseCXss5BdKlHBcKAvcH9hVvEODSnmSqfZ43HuB8O88NE+bfU
replay-canvas.debug.min.jssha384-sxBGApGTSeSFvjByJR5AXv67FcqBf4ASzvh4+5UCsu41G8Vln3gZnpYI8Om/p8kL
replay-canvas.jssha384-jla1kxJm1Id5ohVQZg+jw2O2sXG8JXmLlms1mgfLCJnvSv2F//9Un6wnFy1Z+bKq
replay-canvas.min.jssha384-jh0EifgSpB3O5vBidld9Eca8JqBtIwxQMulP5ZNP3JaQGK9G4ZYI6nLvHnXv6rfS
replay.debug.min.jssha384-0fqyHugF7Cy8a9ZEcYurspUZw5D3fFvfdG5w+ycfthUfxCfqWRVCEg+FYOgjrNUx
replay.jssha384-z78Dp2Ehv6Y6ui58fEU3cMInquh0CLNcEZHLLJedjbu45iM0LrieOepln+nDictE
replay.min.jssha384-xf3PyzYBpZnB7BPiBgYLHvYDcsvDiUwWfI8hZA92FGWxX7hap9q9lHgq2/O54kNM
reportingobserver.debug.min.jssha384-qaDfx9htZSRLafu0ztGd4jX0p8IkPDz25bx3Ym+fQuH8zXIftQxHKOr3IFMEZHv8
reportingobserver.jssha384-L8ovqG4gC0I6ZAiE4Ma8JSXyy1OOoUsnY/DlDUgN+5sFiPy2YOdFz8NL7GVgv6RP
reportingobserver.min.jssha384-qoP5hQ56tWbtoJC4NDoxh0scuawq60qjjaYZf9CFkGdACWoxULl5ZgLPgmZZFiN+
rewriteframes.debug.min.jssha384-DvAK3yqzTShMDOPJx2qvMgeTmSrrKjrFc7p09IygWq2vU+EyRIWT/AzDKWpjTord
rewriteframes.jssha384-FMsiSMv/s2B+JKSmRwMdpgYjt792fwNPqOxuj6ChEwj8BPcAftK/v0Stv63xf5C2
rewriteframes.min.jssha384-eFTZbMpvp5D7LKuIvCsTLl4N+t7fNdWcWQ9yoS2QRH5Ar3at9YVrENqUMRONz4b4
spotlight.debug.min.jssha384-wwVSFj0Y1l6Ug6ehEDQ/pjB7lafItLMfZnxOGfz5XS2Ik7T259Xju/pla9JMr75l
spotlight.jssha384-KC0VVE/JZBJcGiummD0nXiJa96URBoyWAGOCUkLMY0WF6py7ye1ErrrT1HfP+hXw
spotlight.min.jssha384-e6WImG7uhs7A8RcaGv2gfg8MQtRN8fqdX/75A5iT4unvCcDXbQt+4UwzXs+FamJt

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").