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 enabled.

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
// 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
  });

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.

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.16.0/bundle.tracing.min.js"
  integrity="sha384-9AzLSPs4mvl6bVQ/I3aJxaNo83FPgk3mJgkerlJpsr60WnX2P5TcPAFiT7mWl7VF"
  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.16.0/bundle.tracing.replay.min.js"
  integrity="sha384-mS0u9tZtwcLhptREGpk1vdcodyqOml1tYvEs6LMR67H3vThBaheg8o8t7iN/o2AQ"
  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.16.0/bundle.replay.min.js"
  integrity="sha384-HVJY0KrTXe1gDSB9WH7eTjEV1RqAQWIfIZErx9K6sS3ONEHuixoAy27iYE/EEaMg"
  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.16.0/bundle.min.js"
  integrity="sha384-OzyuW29fGksFHuo0baQU5fQ/KVsTvHXVohS5TTYyoJGjOHZR/wxRV34n+DKNztGq"
  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-hb+34mzIgjhb5iD8ZisGM9GjS6ssozEmruESW4QAk7ViKYD5NCgqHu7LDFm0ly3o
browserprofiling.jssha384-l5EfstbG9gGQGzWeSWDyhxTwP38US8bjYaCBM3TdIaQ0vvmw+uqIgdyRD/uATcaq
browserprofiling.min.jssha384-5KNOWyX2TvQSgyx5ohjs0oWqKb1dsIY1ZRxb5wcswlMKGRHqbm107EG3bxN0KHfo
bundle.debug.min.jssha384-lwxdUByzNcbyhjijfSqVXO8+DAS9KeGffWVfw9n8O7/tP89uFtgOtT3vNyeDkBpq
bundle.feedback.debug.min.jssha384-LrqYZ2xGJhxKDWHcgMBixyW4SQNkoVVGGSBsQ6k+piovqaEQpBGnxw2ClzmpPk9N
bundle.feedback.jssha384-F0H069JQCYY6XvZGMJUxh7iaCiDyk0gB68cTpX52zt96FbA4ZazK4DMYtdXMdGgS
bundle.feedback.min.jssha384-UPlMUxRIJFNTHYLGUHSFFevrv8VGJXbAgysB+voOWpNuwPVq2lWzjP9d8J1QDnbb
bundle.jssha384-plSAYDsOL2djVWvFBAnn4txn57YELqyREePNpcz+5OuERX9c7VP8LZ476EDNVMqg
bundle.min.jssha384-OzyuW29fGksFHuo0baQU5fQ/KVsTvHXVohS5TTYyoJGjOHZR/wxRV34n+DKNztGq
bundle.replay.debug.min.jssha384-MF+5vk6cKVgLxeq04rzB27Z/g+JyueoDKntetQmqCGl/HQFJBe9i6/BM7La7Lw2i
bundle.replay.jssha384-W78Gj4wU11/rZgT283DFsxg92XQq88mgewnShmAZW7jR/Bp9bSw3W6p1r/FkZHyk
bundle.replay.min.jssha384-HVJY0KrTXe1gDSB9WH7eTjEV1RqAQWIfIZErx9K6sS3ONEHuixoAy27iYE/EEaMg
bundle.tracing.debug.min.jssha384-m3EkhavHqr2Fa44vdFKqoyB36RyF8SbSNXbLLc1g5mO9ipfkqXzuoZZx2T1E3rKA
bundle.tracing.jssha384-PWpBS2UZK+a6IfNBRpHR5/H45gxR4NB2xA4MYb7P9i3GrurIRTJ7RNVn1nqYFo8H
bundle.tracing.min.jssha384-9AzLSPs4mvl6bVQ/I3aJxaNo83FPgk3mJgkerlJpsr60WnX2P5TcPAFiT7mWl7VF
bundle.tracing.replay.debug.min.jssha384-+hGa2pkxiW6/u7AID9rqi9M/uoT0K7W81p6WKcyeNIT64W1n0cMf3T5ao/0zR5Dh
bundle.tracing.replay.feedback.debug.min.jssha384-u0UQ8hzjvOkA6VAUwNCmdSOLkjH+I7u9ZVzi3yxVDwsYj5c1XKW/GYgo0lwEHVRb
bundle.tracing.replay.feedback.jssha384-ogsFqAjIiuuarSUpzyfUQ4ZWSBphW3NyZeRic11UvKwEQqtYL5xYy79EMvwhdiom
bundle.tracing.replay.feedback.min.jssha384-RWC3piuLPpNVGj/XvywrLHn5f/msD7hyzKrdScaz0fdC3BvByGt7RGMgro57WdB/
bundle.tracing.replay.jssha384-szAL+7BRTHWnUhEa0SdUTyPfbQ0Yp3Se+VqoyipPIrl0z+xprUlUbTIG7FmlGiRW
bundle.tracing.replay.min.jssha384-mS0u9tZtwcLhptREGpk1vdcodyqOml1tYvEs6LMR67H3vThBaheg8o8t7iN/o2AQ
captureconsole.debug.min.jssha384-RU0vBkbkCGIgsTPg0SXLNHo3jbi1CO4zGiyAioq9ks3D+rI3inR4LbYcUJzcGugX
captureconsole.jssha384-ONSr+cXUTdMpAHilEaJe944OeoSYQNlHzoWkp2cqD0NsGs6f7DVLKZJMLHFTCkJ5
captureconsole.min.jssha384-VYOfqEqBZi2GpF4A4FS/P5hfVLufYVlwU9+qVvrj0mQp2n5XurgHDoc1Iq4p2MXq
contextlines.debug.min.jssha384-dZaoXoBO+Je8iJV90UdGeNRm3YMwTBEUTE9WSEtoWNBs8K27DZqZYpHatyGyFOWU
contextlines.jssha384-j3uyQ9idjGVa7PGAxE7FI5WmMoLy5iFrwa3W10AKrbY7tQP3xtV6QBlFUJyQbZZZ
contextlines.min.jssha384-ubnlLUBObg+yZFBDdcEcImuFKPPR1moDZNVatTlYXzN31RxWqjJ6IJWxsQmInNJ/
dedupe.debug.min.jssha384-IzJDVIO9AfNJkr9XKWLtPqRB/IiHcV+aWQZvVYDOdM6A0b0L66eABqB+PifxISRq
dedupe.jssha384-YHypyAXBWaYCMs48QzR4UZqifvt2iAUAjHWfiR0T+ajIXd79KKKz6N95jhIUjnee
dedupe.min.jssha384-Gxrc8bNrYbnWvz7ksRlvx/z4FLT4EmSeCARUREV8e6EBvHcniHp1ZSJuY/AuiLS7
extraerrordata.debug.min.jssha384-shndq2Z4NjuN7RoXodEFvpfXby5gOd7VTE6vNkBhESEUspI0tbjKd4SDJWyxDXG2
extraerrordata.jssha384-7wnCLjVcWYAw6d7l0Q6X9fnFpUrs+l1ofVOaUmyzcoWwUJx7ZcedhE2iAPO5Q1gt
extraerrordata.min.jssha384-dy+u368orG7icwJu5kt6XoP/6xhYFhQBCJ7wXmEygA+/vBP+P3KcvDCm/6RXFtSe
feedback-modal.debug.min.jssha384-VYvIDJ7CX6K7kolm3IT0zaC7Xi9p+Lqi7xA6nZ3QEyrlpThSsqcaM1nfhhbjfFXW
feedback-modal.jssha384-vQqveZyk5OeCoehx+Wn8TsMdD54bXAHPgUIaST5e8Jy5RNvkorQDO8WT6T7gSWDr
feedback-modal.min.jssha384-x0v9ui/BcvKwTQ2kSVrb4hwI9dJ2CN8pNSFHDl6snfPTf2arOUl/9oOk+bLvhJI2
feedback-screenshot.debug.min.jssha384-fO/iizr4u3I/9O/MXLG2mOgcHhl/Uhg957OSMggVpX+v8xaYachsSaFIF50+Z6Hr
feedback-screenshot.jssha384-HNpi2J0WKoWvSlV7SICPONWMLsh5OF28DYYETCz/HyUQ6+tEm1/iVPmrbWdBF4xX
feedback-screenshot.min.jssha384-Ues/mirs/1T3CvtWHJAHFnq4f64ekvH+PtzXxjabF1Y2BtDrjUwKgTdNCfXk6V3G
feedback.debug.min.jssha384-qGokAdnrL6ysmXAaZNsU8q823FmGyE+mMgJQQyUrVGSVNiBX0kyXjXrikdLlOH8/
feedback.jssha384-2MSvrhAgMCxhnRV3XRb3ZzEKIuAlDbJI1GlGm35F+noTcERsFnkaoOFHlJmjpa7d
feedback.min.jssha384-tT3HL7pJacCByqLlI2OMdm2W0rWbaHyl4QUE4wIMl+vlvGZu07wcL1mLPt+kSkb2
graphqlclient.debug.min.jssha384-eoxHK08a+V5nxvHsTCbvJaDHjZ+0epvwmroxYkT9qTXhj0c6zlu5IgZsxWC8Q8Nv
graphqlclient.jssha384-mezuigitnK5z42zsVru2tXBaS2O5jWJMzuOLe95jK2k9SLX99vFQY5yqBUuhhSjJ
graphqlclient.min.jssha384-sepRNEh3KClmg4HJZmsNbbrLT+UyOU9oxZ+Guh83Dbigk3XhzWLIVdnhwVQS2nm7
httpclient.debug.min.jssha384-zqJjMKk7YW6D4dx+DPOoyaF/scLTbcOGrWW3Pg5zfRWl+Q5NvuaSCyQrixBtMhiU
httpclient.jssha384-sGaq0pf4D+OtIWQirj+YS5Tkrjx+pUbNYYxNdQnFYY56MMmrucVQ2FsVmK2US1QH
httpclient.min.jssha384-1e9FkYdz6HqGg8a+ojwgeDtjrBGmR0zntKdqMryKoqoYTyDGjuKZybN+pKwvW3iI
modulemetadata.debug.min.jssha384-847x7RWgTYOHHL7NrW0aQClTy7e/+iyPSMPgNj94IaoWwwa8DA4bEFUn/ab4fUmK
modulemetadata.jssha384-wmRn9UEe/rLXqRNuVTDA/ws1CUTeu5msmqh5sSsQDGD+c9tJ8QFiwR87jk6p6Eev
modulemetadata.min.jssha384-X8ufEbOk6YGP7CJeR9SADgcPE7QMSv43zIGqR/iFnpMXRIhp6lzWQEjelh1Wgdb9
multiplexedtransport.debug.min.jssha384-XjtN1LhqDIuEdinBT0JWsRwEMEX7flGpZZNpF5519HKmWUlyIm7LnRkrOTn3yFot
multiplexedtransport.jssha384-4NpWTb9iiDmLXneAfSYAgczewghGhL5Nwd0wofBM7qycKnaxOwfhH2GkR00wkYE9
multiplexedtransport.min.jssha384-RdESa5uN7mE3e4x9pYaongXxB722qgC2JfHkpJpldTXeEUdkwO1wO2AdsAhCr9W8
replay-canvas.debug.min.jssha384-X3jGPxoCnIYFZKZVj+ABYJkhMdOOq3v9TC1O+h+NvJTCsedX78wWSrrPLp3+aeVv
replay-canvas.jssha384-xBkZmvi089YOtcZa5LV9UKOyg9jColoLu18R4x0iAfndqKqqHIq6dKVBvuBJxYKl
replay-canvas.min.jssha384-QtWRi60anFQfi6sQkZKetq38yigcHmNEornSYLM4eWgJDms7UYoZSCUL82ABBIy8
replay.debug.min.jssha384-6u02l1ngTtmzp8P4vzB+g8dZCEAEzXJp9XCYRbaSUOBHCfr5mgC3/FfDMKVu5dn8
replay.jssha384-EgpXnDOjIPaI0JNf+iVZphK9uZZlFFjuZgRrG+t3pLb+igo1HxmJra2+5E0N8UPU
replay.min.jssha384-pjfIEJTnsaTHZVH3D2DhJ551KxzBTpVvgYL3eScKAxqH3htIc4AKkoXSVeulmAeq
reportingobserver.debug.min.jssha384-in6F+hWXL5zA+kHfAXNpm8RNTBa1znTuxIMZ3syFH21ZJ1USzb/z7uvZ/mDiAdyx
reportingobserver.jssha384-fLEeaPitOgHVGYgTvMR2X7QoGpm1m1Lki1x/e4tC0jncXMss6F7UEsNTTMZHbmqb
reportingobserver.min.jssha384-LED2B8uFO+pLuR3Mg5hAFw62KPDMLAGxR3U4I2dYrLIs4tzd4m0VH8DFhW6TqJoD
rewriteframes.debug.min.jssha384-IaB4beFRbdIiti0SRzOFlT5E/VqT86FencgHsgM9o4eJsdAobsS+CdwIHJlOPWZB
rewriteframes.jssha384-X8UPXKXHEULrG8//8TQC8g6mC3etB2oNfkQDURAOrTfJ1qqhDzsJ8MZg0U4jiKBr
rewriteframes.min.jssha384-gICD/fCdU4zUNgcLd5NG3SgsJdeiozFAV+L2vOJnli1dtU8B8SyYZ92+dml/kB08
spotlight.debug.min.jssha384-kjbglgOZ7HTukxrEVoxClR0M/v16IUda5GYnwUje/tFd02eJH2UIrLGIab4yFM7O
spotlight.jssha384-No7hxcpegOk9+rNAXcH0WH2ZtR36Hxaf1Z36kQhzQWuV9hscbyYavyteykrmAtxn
spotlight.min.jssha384-y1lrHIV0ct/uLmVZeFBvwr/WyEM7VAiSYkm2q/KJErk0EJAC/8zVkFaUG3UlPD5+

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