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, Performance Monitoring 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 Performance Monitoring
  • 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 Performance Monitoring 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 Performance Monitoring, 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 performance monitoring, you can use the following bundle:

Copied
<script
  src="https://browser.sentry-cdn.com/8.2.1/bundle.tracing.min.js"
  integrity="sha384-jXyaCouHQxvXS4XCWq9uOdHl+GIkr5Civ6P9nSCj//1te76DwcnyKKLCo9ZSWUXZ"
  crossorigin="anonymous"
></script>

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

Copied
<script
  src="https://browser.sentry-cdn.com/8.2.1/bundle.tracing.replay.min.js"
  integrity="sha384-/4v+kZ3awZQ8cYFGMezkIloc+Ix1r6jh/t68L0ra3tzZZFXx84d/ofMIvQw7QFsE"
  crossorigin="anonymous"
></script>

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

Copied
<script
  src="https://browser.sentry-cdn.com/8.2.1/bundle.replay.min.js"
  integrity="sha384-fJJhXcJ20du4ixiPAjI+NfUOjNUoO/mCCwBWH5mS9OScu2V+KzW+RbjpdzVx0agl"
  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/8.2.1/bundle.min.js"
  integrity="sha384-xTCf5YIXCBHJbtoL8lY27zHybLy/BLgfMDT7tA4TNQDfFt9YdeiqxDzfmbgHDrrH"
  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 performance monitoring 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 performance monitoring (named bundle.tracing.<modifiers>.js)
  • @sentry/browser with error and session replay (named bundle.replay.<modifiers>.js)
  • @sentry/browser with error, performance monitoring 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 performance monitoring enabled, compiled to ES5 and minified, with debug logging included
FileIntegrity Checksum
bundle.debug.min.jssha384-I1O0EpQWSHFLif0pwQziW7M999U/0fcnTC5c1rSpoPRnQARBPokPjCoJZcXBCz6x
bundle.feedback.debug.min.jssha384-J1lkJiE5IY8CvMjWpLmGS2AC5VY0HLzRUyOvkL6wFAJkjX0mhe/esXG/wMUko9c2
bundle.feedback.jssha384-SDBkkkRc4CIhS1WXNktOA96nu/BD/6Jh3H/ahFOsO0/qHS04WyphOuA68HIK9+rX
bundle.feedback.min.jssha384-nkViuxMmNjTC5WlLP03FizGJc9AuzoRc/yC+FRd6VAPIqeWnn+JBO4HJvS6HLgOn
bundle.jssha384-C6aTsBptKg9Wp4VHCedhft30J1eKlipAm4dhkQQpdtEzYOeJ/9k0h02ZBJdPkQKO
bundle.min.jssha384-xTCf5YIXCBHJbtoL8lY27zHybLy/BLgfMDT7tA4TNQDfFt9YdeiqxDzfmbgHDrrH
bundle.replay.debug.min.jssha384-PsVPYiVkCZyr2cjxf+yV11XaI601NmcJpSaoHuruLiLSNiqyn2UCe0e/ugcmBA7C
bundle.replay.jssha384-KoV1jftBGhOG7ZzPwuPnlcGhtVfVC+nMWPsdtkFs3dVWArEONz51D8AwH92POxsU
bundle.replay.min.jssha384-fJJhXcJ20du4ixiPAjI+NfUOjNUoO/mCCwBWH5mS9OScu2V+KzW+RbjpdzVx0agl
bundle.tracing.debug.min.jssha384-mgEh/nL62K/wEP3rhKOQod/NrqoxvT8tGnjVxXLUw+X0ZdDKYJzisEE0T4D/7vJ9
bundle.tracing.jssha384-nV4pZWz+5iQ9VTqgaeOq1dRH86JhES8JxQ36EWOU7FxTySd81fiJGXa+5PZPgG/N
bundle.tracing.min.jssha384-jXyaCouHQxvXS4XCWq9uOdHl+GIkr5Civ6P9nSCj//1te76DwcnyKKLCo9ZSWUXZ
bundle.tracing.replay.debug.min.jssha384-qThgK69uI3InufTJ0Dyb6KM5yzXmwIrhjAUSJYB52HDVBz0Plk8nySCx/uGED30A
bundle.tracing.replay.feedback.debug.min.jssha384-rUhlPxKajqnlZaW41t2l/bSdF2LtTXv5AgVXhSEQ9LAbo9sn8zPNxn0mX5ajlCku
bundle.tracing.replay.feedback.jssha384-9vJ/y4i4vrIB1E8SN5cCGfIdVrAvS4lzUBZinqo0ZPkUvVSVpAU+pBGj2USbMJnL
bundle.tracing.replay.feedback.min.jssha384-nAbdydSQ+sjjJMk5hVc8OohsJNsbCxew2B/CL9RTHjT20Mu3Fr/PZicPnUGjKbWC
bundle.tracing.replay.jssha384-xMfzcl4Jqhx8ulnOyDbSiPlQs9pwXj1bnyPBccbiUEmRNiDeuVJd3SI17MX2ChNq
bundle.tracing.replay.min.jssha384-/4v+kZ3awZQ8cYFGMezkIloc+Ix1r6jh/t68L0ra3tzZZFXx84d/ofMIvQw7QFsE
captureconsole.debug.min.jssha384-HSYMnZRvYXM7D3hS7Q/rEqMFyk+Zyr5UmOSdhHQXbRm0t0gdpuFM+aDIopmKqcod
captureconsole.jssha384-5UcwDrI33bloSgZlhw77GOyQNdi4Ng+vruqZpMAO3F7DPsFZtTFjCFXPQgTmuuVr
captureconsole.min.jssha384-R3ZI0cP2swJSDexxFjEljCA3OR/0HliCTbQNWEy/T/SwWOFhRr5LmTT/X0HVQJ/N
contextlines.debug.min.jssha384-o562YkqJVNZPthPfi6qAxTlVbORxAQ0+plL/p2uZ6N93skT7/iasuTcEzJZ9kG4y
contextlines.jssha384-DZI+vzZKtX9C/JH7++oG2CadzzVCW7R8ybKbhTdMB0nCNPD174yCResSLwtCwdnx
contextlines.min.jssha384-sA6FHF7LzxA4AZZEgJHS2g+IvLlbUv/T2CvHePfQL6ZqbSHxaOyppI3jJLeZth85
debug.debug.min.jssha384-L9PCjzgcj1jiPgggnYddPETbUKF7tkuulTWCyLTXJl8eLhds2V9o0bvhM3OLAAYB
debug.jssha384-xmxF/G/bf8F5zw9iZAHeX6T261ZJwWh7z0RHl3DuVFHbdsoJQfvc0bcSRaIFarQ+
debug.min.jssha384-riFZAKK6FmGPbcG7KVcyBHgnb2Jv5A7vHkCrIHN+w7heJSCRtUW0Vugzx1fWjyYX
dedupe.debug.min.jssha384-WGpkMrKeSXi94W402gx9dPkxrUXhJU0b0ZS7hp5Ao1P9H/Q1UMdsTQJF4ZHUj1nA
dedupe.jssha384-50ihYPvGdjt3c9F9EBFkS1Au+qqp3Ot6Xg0ieYgCOqBDYGN5m38Y0b832Zh/I6JZ
dedupe.min.jssha384-NTlwxZYOcJhxcIwNj5YFIb1DcRQOdtkfZxbu3aEh0yydUedWY85OUMt+ANxc3CGp
extraerrordata.debug.min.jssha384-UBFInnMrfpURHZ1B70LPET7kUUi2UgRBm/GLxTwlvy0o/x3Qjm/cecuqZAnYdcqd
extraerrordata.jssha384-VbeJ4pFnYrVSAiaPyyVN1TVHI44NMcI2I6xMdJQhtYFCdY2lsJsY2lY7K47iSV4J
extraerrordata.min.jssha384-GxoAWOeIbPCgba9guKaNPsR04e9Qa9g/vDwxUEMk/q84mRUM3CImT2yMZmslIIxu
feedback-modal.debug.min.jssha384-0CteUe69tmBdT6f21YdyFz0W9cM4KCRfajUFgPpPJq7UDJ/80+EAG2Exqj1HAZkb
feedback-modal.jssha384-rRqXWymL5L2f9+v7LmfMF1BvL+4cBK5UTBUBQ+kSfbaK7OE2DnD2LlkaikUooJjX
feedback-modal.min.jssha384-cU7cQUO6fcrrrMjb54nqAVB/3myCuknRB2f2+NJLecVg3Nj0W1uukl9v8jXDX58E
feedback-screenshot.debug.min.jssha384-QutcwwXJTSuFCZI9d6f9KJTc0PdjJS+GlkU93+2E9XuBqZuLi2uo7LW0jWIohP/N
feedback-screenshot.jssha384-WNEgVP04U3ZFf/VCaqtKDvtYORc8qg3Gh8OsFUbtI3CptsCct445w6+zMg4O7yeX
feedback-screenshot.min.jssha384-tHSz1AUiaIk7tWDRB18l0drA9BAiX38SR0UNbRso+FFAs5sNXMtkfa+jW29g3BNq
feedback.debug.min.jssha384-mNthlcZlmgDWqMxZrIoROnnjupRbtL2VusmMtfeV5Gj5xWkufntwxkwa/O8+mvcy
feedback.jssha384-N1aYNydXRsiNJt83G1bgKMuUpUmLiUWUkh/SnjoFrivX6H4ggn2GBDeuGo1QP6Xr
feedback.min.jssha384-y/vgTARL61dpBHlAF6oOa3f/TCKAy1LLawA7uxRT/rVBvtt4ZatIjB+8OqtPeUNm
httpclient.debug.min.jssha384-zgwktY/7Yl+isOaLVV5MR+Gs7OHqdJhSlEq8sYYhgkmk73o1fu6IFuIlEk0afR5L
httpclient.jssha384-FC4wSDX4GVSaBAJM49yxNGAxbdqd8KFTaKJW9kYDmBSiHs+Cf1n9I23AgeK+NFy7
httpclient.min.jssha384-q9RR8L0SKfuCq5RRAksnvRcpP8vfpd5hQ/BnWWYh3nO/DtTYsQKrPJtV6NNWnSN4
replay-canvas.debug.min.jssha384-KcJULeCT7IBXJM+AwhQOc6AO+m0y/9R8q4ley3ZetNqYzbVwKznlc1R49M8vCr3l
replay-canvas.jssha384-02uB8yIvgo6dZtMJS4yFzInuDn87MbsiPgqcghDXciQNZq9sWzvC38pru4CJ5QcP
replay-canvas.min.jssha384-0YZJ/eYINOayxJlhmor6bj9vUpmBuVxqq9YF5Dz14IMduwrBZT1as5iAN7nG31cR
reportingobserver.debug.min.jssha384-nl1IFiaRAO9BgGH4IBP1NhXNpdm32TcrPZbj3QsMYz8cViNnt1cRpe0MRDbfggAx
reportingobserver.jssha384-7HB6zUUWDbUyaRWoBnbUx5k85dZhUWGsUOfoCrsO673Av+emoTd+4GMKEvzAP3qi
reportingobserver.min.jssha384-ddWZZTOaIN+lBo5r67Tuz/qpg7SAlElG9dfRnxAOIBjw5VuCz7KQBs8vH++HoWL7
rewriteframes.debug.min.jssha384-blNlvsXh1V1/0OxG1WA1JvOPI7yZ411s9cnAT21ENsMkNujRVdUI1o1r8JQSq7Im
rewriteframes.jssha384-Bup+0/+iXy9hYb45P24JUPcNmJsnBnA5u6y+jtzOQ+S1Uh3o8iLQ1pXazyZnikcQ
rewriteframes.min.jssha384-KwuCoSCXx4LdA+ikaf4hTOk6pUpDWm8rXf9zqjMICxlt5K8Bmf6DBJLgg3yVkEhX
sessiontiming.debug.min.jssha384-j0pQ/1FPk8tvrbeRAxG98itGlwZ7fBAOF1y4S04gcYYNQCUgi3Hk4HPGkOSE6EBM
sessiontiming.jssha384-Mv1K2rnrg26tG1bRiKxzCgzddQ+Mf3G8m416eCRDClIO40q9ID8SPj22PqSHl2t3
sessiontiming.min.jssha384-4VXcsdktqAdH4WUI8ZvYNVExSl7RM/Lu91iMuIQgGbTHC+1UOZAgvg6OZCvr3vQJ

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