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-iKL+Vf0JnLP1Npbo4NV37NorQkSldmLw3UZ6dtz5kCYQKToVnSxLpLYD0+/CLPHV
browserprofiling.jssha384-yxKb/ebt7O2v0jbWcWR9VpqfnMvSeY4U45lnVq1oNcxYUJhOZqo8vFdmWDD6kgs2
browserprofiling.min.jssha384-mPtD+n7G8j6Bd3/IxRoPgQgDniDLjgeuDzti64puyvbbPhbHhQBUsww/PmqYzpHD
bundle.debug.min.jssha384-AdBUtrR/ctGhjwfGrU0tWyhg8SSkoHh4Zl6Gl+i+T5vWEy8TFhsPjKCz1khuD5dN
bundle.feedback.debug.min.jssha384-Qvr2QOkbQCyJeXQxc87v77hj2EDHWARB3WyPULDe29CPYkXa7eahASdIwE/2KYNE
bundle.feedback.jssha384-AcLTRzSPEkK5gQ0K39dNJntDmx41TiWhlgvNv5S4U1bjsXv3y/a9P0mcuxfsFoqx
bundle.feedback.min.jssha384-kBb6uFl1Zty2Ske48IKQVC61FLz+ZJozfVrBRuXL6sXYGy5VO2OF7emeNYLaCsIT
bundle.jssha384-x0DvAW/5wEPtxcdVRaaam8hBKMslD+ph5uNfLEwxJl0MDtwRV6CftyN+Mx2slCa6
bundle.min.jssha384-39+F/ic+MQK0IMqud9qBn6/cqJCZ5ZBT+VqvbrHdosCJVGtJDa47SaQBCE8FRGhE
bundle.replay.debug.min.jssha384-OBA2BXcCCUqBeJRpAtlVUCvV4gC3kfo0EXq8h8aNvZuEG25U/M1MVxV7VxoBzpp+
bundle.replay.jssha384-LeIOL93NbQ82gaP/1j5x2iu5zLZ0IZ776jzXJ0W7l3wihqtOj6zsCrqLoz/mtPhm
bundle.replay.min.jssha384-953zNw0CZ/HlerH3SZPZf/LfWHue7T2WoHDv8svqsjYqQUvHUcvL45C0Bkedl7uh
bundle.tracing.debug.min.jssha384-BHxza46xsBh8FUZggn6bejMB+XBv6INdyr0XI5tg5Dx5Kq/d6DLakZmZrudzY3nB
bundle.tracing.jssha384-qsi17lmgPOT3Gv8aKKWKWD2ZgR5SokLG5iV1HL6GMhpk9fqujLH7R+1R86IGXroI
bundle.tracing.min.jssha384-SFODEb3T2QkMjTr3S1it5zIK0cIW7jsComFUkmJ9nU9uaWs3AzA6cubt8xn0zW7A
bundle.tracing.replay.debug.min.jssha384-13zaib7cjyXZQEcFPBlBGTu6pl82TEY2oTi40jd0Jzoc7juTIf5Lm+hBdOkQDvvc
bundle.tracing.replay.feedback.debug.min.jssha384-5J//x9v9kTtfQEQdD3hEBSN08aPenO2QlSu8C7GDz0J9ODF/YhHvndSjSXmMhdHZ
bundle.tracing.replay.feedback.jssha384-dLJ1ZSooe1bIrMvHQU+HS0zqdvMCxMUrnZ5bjO0RjRP99jeYW/AkfEemwzN7HcjS
bundle.tracing.replay.feedback.min.jssha384-DwWaGsCNTmRNYxIlyeOSfUBe8h4XZ5svIqy4oIcxf95ehSvLmLieqgPhpHKjEY0i
bundle.tracing.replay.jssha384-JO6/XdMty30K4+JWqlrHz3dslrIQLxn1W619jlTinC9S/X2hiFjEUtxVXDkXK/kw
bundle.tracing.replay.min.jssha384-SIGrDsrnoAKOKmjZmYDf8QQjvA4TN+IDfZQtMPdWU32p4X4ARp2MIH3F/LvVlLsh
captureconsole.debug.min.jssha384-LrzL2XMD1LdYVU7PaaA4ooVJ8FX2HxGTkzazbrm7HB4lbgD9w/bzK5tobaRMcfuG
captureconsole.jssha384-sggg+Wppf0EeFiW6xSVOkvpMaoeQryqaglb1kIjcG35rMpSrrkmUKfhetsb5p6JS
captureconsole.min.jssha384-7fYjekY7ylhXT/OupUdneqxsE5kh3uQUPV8Fjozn4veWC9+a0TWrHH+1VhAYK8Ry
contextlines.debug.min.jssha384-FAcmNlqCwZcXJ4ffyugEMExcHnRQFl+3MAdPok8+bcJYS9z1ro6HfQRiSU9aLQaG
contextlines.jssha384-NMJOHURj4lqKN2EwUqiM36l2tgttZj6/RbsRKbCdlyQY/MzFbr4LerGGyuItf54w
contextlines.min.jssha384-OuSqs6d4ZUKEzjUundlIWpsn6ZziPaxseFpEQLO3coaxjDyGCFQufAKlcesWtA8p
dedupe.debug.min.jssha384-ZD3SwXSI5RIosgPwUgtY9FoP5wPfdmOtIiLN2SmmYkJVM1BbGcn4W2Aj/EN3+R9h
dedupe.jssha384-4ampAxcQeLQhe4YzIPdIDqapLOoUPQ3YGJJ5jPZZfL2BfRImbgrpAugsQnLMaUCR
dedupe.min.jssha384-vVJpBlKFOlT+QYcZI4umGmHZwO3K7PUhIroIKyGcKccKt4SyNNSz50/pDDUYeATW
extraerrordata.debug.min.jssha384-e3PYuR9/86hWTQMBQJYMcaFZkMXV8YCP8fmSB7H2QnvtRsy0fHIkh3e2tAAbMLm2
extraerrordata.jssha384-D2u9SUVwc7l1Gi30XEaW3/bMe9LtyL5wpOI5C9jHjt6Q5RYT+kcxzl0JmVahwuaz
extraerrordata.min.jssha384-AibELA/vhge6oR+KLUPmvxfMBzXmD/chBhPZuank6+qyA0DlSkMaE3IiEXsgxBUl
feedback-modal.debug.min.jssha384-hAKWGoi08MmDXKF9TEGfs9yVl26PZ5MW3WMZlIqxbSC38pULClKopJ956/miXor9
feedback-modal.jssha384-9ATfW5BDAIG2NFYxBhDcm51GJ23ZKgyen3RkG8moXw28weCvbxvNXzxN3NGYSrSN
feedback-modal.min.jssha384-FpthBeRmoPiQphUC4W0FHy1+flaLQWz32g8Xio+239YwxGRUKBqVxKPc8q6/2Yez
feedback-screenshot.debug.min.jssha384-xdm47Thz9s/GlqLx3DMZIwKtAQLs/OTf77u4l451se8W2/U6lpKFa9+kvbdYkItX
feedback-screenshot.jssha384-qE0zb4HPkICX/9uKVC1ZLDWsQWN8w7XDle1WANvMdzKY2vIqp68hNv5LYyppxZ7Z
feedback-screenshot.min.jssha384-mG+I1J8jbiIGVmcEdUCBg4rAsoTPu8Q7yFJXSm9NT1CKgBPAT12jzvxAskZuIf4w
feedback.debug.min.jssha384-A9iOLMFx0ZR6TFINchaS0GlvDV17lWZHqftEynLM9H9Z3GHZG50UYKo2VzviOXTp
feedback.jssha384-C5YbafzNNYzV/hQO13sW84WfDb/OJps2aqiQ6JkDXDDJxHabCtGqDZMapluA56NE
feedback.min.jssha384-RzbkJ/3F8KNt67aeTwZHKL/LOqszMW+11ORbnfbWayOEFq0fMOuVzORUyfC2iu+d
graphqlclient.debug.min.jssha384-UpqaQPaUoxEpgL99ZCg0EW0OiMI0fmpRZNzhhG/RugKSzR67mLNsp67oMw9vtMLo
graphqlclient.jssha384-BdsTgcC95nGG1MF1KxMKWsLzRFfd7vRbHicXgkUKR+bhS0/7OYvL0P3diMFPWT/g
graphqlclient.min.jssha384-N/E7Y/bdhbcPahCZmoqLhXL522TLZ98V3JT9qQXon0tR3u9yfsXRdWXM464WXp8h
httpclient.debug.min.jssha384-4vibPsi8c5BgwD6h4/VTZwlIibODmJ7+XgKMt5DC73y6jE7nvgbaYCNdunVV8CDH
httpclient.jssha384-wA2t6AobcdWdCHKSpZknrAlKgQUbpui3bt2YpsUlPlh5/s/KWRQj9W00r5G73xzU
httpclient.min.jssha384-4PqZYQJ0jr4/TdWY/rAEJJldychtuIUFnthBPWfvmouvVUbAONvTmADClrka9pKC
modulemetadata.debug.min.jssha384-JiHJC5S/98tx3B25iSy4NTtKUVxMIeQy/fxJdfJ4VB+jCAdsRpGwm0cBNYy/lCr5
modulemetadata.jssha384-MXV1j6v7fwEVMhg944zA09OHcGSrOWIAhOSmD+1Je8K7BHT3gl2XQc9iQZzZc40/
modulemetadata.min.jssha384-dOhQxqajcZyoohH6wH7a5bWtAsz7YXxI/6V/uKLqXnQ9gjRNqa3xC6+AnGoXAW0k
multiplexedtransport.debug.min.jssha384-0v+jueafWez90nwZP1bS0D1u+GL8JoBt9TbxcikiY+W3ItficxIs2qMy8tNqFLH1
multiplexedtransport.jssha384-KnUeznZXEnx2sUjZZFvMH70UqsZuuGiuOMSaDISQM+cLy+yFlZ/UXEhdj3d/b2zV
multiplexedtransport.min.jssha384-sveUEgbHWCbhGygzCS+W3R4v9FCN2b79J7mefbLnedKJnuMJ9ORikzrNL0+3TkeA
replay-canvas.debug.min.jssha384-gHabfQW0GUqSmiLi/p5zWSgQBrSzGSfi3li03yuIqzLUe7/+D1xsgGE5kCD2Yqr/
replay-canvas.jssha384-8zwK6Wux3VWpE8k9tl+iFJIMnt3pfAW6PsZ/QiqvcwTwaQnEp5jTWHgvQK9mk2j6
replay-canvas.min.jssha384-Vis5rIm1sWVNnHfUWrWcyzM26EU4IdMrsoPmeP7hujJocLqSa5k2sSTaNfJX2Iz4
replay.debug.min.jssha384-3Am6zz5LoGBnS5k6Ap2inq/UrMq/HGVTpHEBikMvqakYOU30FiJblm7yDATd4AHn
replay.jssha384-Ep0SRJZloh2I0Uc0kKe53JPwayPr0OKsP7U7rcevSA/ui13jkdB0/oWoDetYPPbi
replay.min.jssha384-Om84907ZB+upeuSlBi/2tem+VQLzY7mi2Eq8pq14pSk2aoh7+SnI8u3R9GvEkFgK
reportingobserver.debug.min.jssha384-8UDN8xm/YK7VgFgHIT7ORuH9K76t1/zU2wEayZJ3pWJ0cqKhoo1xOaX6SSqXXhw0
reportingobserver.jssha384-KIuj6voMUbn51KrHam8V7PtSYWCBucNWFQmJ6ev24e+WNuOGMwv5IdzNVLOHtnx2
reportingobserver.min.jssha384-NWQwIcdUpfnN/kSqzZLmHorVDxipgTxUJ2G19HrKQIEwYvQ70TAQJP9yjKDG583F
rewriteframes.debug.min.jssha384-KtXPmPpmEQ6LBEi/u2Zb8DjpETUk4G245soM78AdUoCPqz0baWYNxwCm2pgkHbXG
rewriteframes.jssha384-BMI6eTPf6xO0b46xd1qW8bRIyOSdA0B6fU66h3+q06R8zpp5bVECyiZvu2YeActT
rewriteframes.min.jssha384-SrFiyhf9vHMM4buxT+uvnTT/v9KbaPthb3ZLdI7JLOTTLVwq9urzBv8RZgyRz2S8
spotlight.debug.min.jssha384-tLy/y84kWpmPE9r1f82gNCRvQvdSs3KOwLgC8Z751w+MU7647C4ix47lWxoM+9VY
spotlight.jssha384-zl1J5kxp5ZU1/gKg//IFICujG8lUh10gYYBt1+oAL9wFyO1nZjIaf9NWi/LPPBta
spotlight.min.jssha384-sfw6PgNMHR3h062EiOqC3M+tNVqzehD3o5SeGJ5eWKCWl66lq45Fjge1skQJPpbz

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