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/8.12.0/bundle.tracing.min.js"
  integrity="sha384-UWQQs0k3Ocxrcyom6IaEPh2MkDdAQ3s4yevQWelxVKtAoVwU+1HY2z6ZHc89pr0U"
  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/8.12.0/bundle.tracing.replay.min.js"
  integrity="sha384-SjQKaG7q9FfmUfQAgDPZeq7lH3iN7cToclarPqBM23W9EO6TMIV9QPqUVeF6SSDX"
  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/8.12.0/bundle.replay.min.js"
  integrity="sha384-TaArDXVFEK3kuNJD+8qTdXDxWcD37uIHO+VNyDsb8lWL3BCiNzpFDwOyvu8aCI3g"
  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.12.0/bundle.min.js"
  integrity="sha384-duwJ0VKe8AOkooplp+o5+xEN5t6RGsDigfI+hul+Zpe1r6YaqECcewAnikmwrXhg"
  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-EOewP0GmaTQCXSgkhFxyqjAEsSDrj3HLNhFTuWF+S3CTi4Xdqj1wQUhuF9uBXUcC
browserprofiling.jssha384-rtXOQwRSptZcL7BlEF0AMjoh0NtTlkkyej+rP6/6jwmUPJWGVO4kHlRxy61L5Fuy
browserprofiling.min.jssha384-80Faxv1MmTk04R1b6H85ptZ9o+lZyNHvFWeSn/rPxoZRc8a6FmS3ZqB/AL+LEH3q
bundle.debug.min.jssha384-11x8gnEHiaYsrWCnRcr3y8keci5j1JcRNLkb23RPXvL5p5QaVys8bXA8XM/MtN8G
bundle.feedback.debug.min.jssha384-aamTzE5uVfs5tWdx19Vm1fD8o321tMYHnwyD6ug/mXhIHDUzvrXw1YeYBoDHYNSS
bundle.feedback.jssha384-I8C5FxKVMZM1zbA1LvgFox+8FmhsCp4CpcAjYLPd6HQC2LRQlvb3uK+O+lPdqeWh
bundle.feedback.min.jssha384-vtVr2cuiFU6XxLqBJ6APNWv4xxBwAwd2jWcDJabc4oO07Ih6PNkqDa9EbgIDX2tr
bundle.jssha384-totlsF/zvB5T3c2I0B4bcZHIUmhXVc6j0pvy5KI010BoUmU2MntYWM80UGHqTa8q
bundle.min.jssha384-duwJ0VKe8AOkooplp+o5+xEN5t6RGsDigfI+hul+Zpe1r6YaqECcewAnikmwrXhg
bundle.replay.debug.min.jssha384-xk6ocZW0/RFy+82muE/kxMV0nD/8WgsNsMKNiq2WU1BfKn782GFNo5GakcGIoC3D
bundle.replay.jssha384-wYhnjmrndOt2tvaSoQTP3qfRA32422xuUESzmCX5pB8U7QjsrZbLCbFOfFgPVa3m
bundle.replay.min.jssha384-TaArDXVFEK3kuNJD+8qTdXDxWcD37uIHO+VNyDsb8lWL3BCiNzpFDwOyvu8aCI3g
bundle.tracing.debug.min.jssha384-C1qywR9bOskr2rTjsi7W/B/jCWD6BPt27FPV6+4jtdqGmQn+CAwD1eHH4MXkYuuV
bundle.tracing.jssha384-AxaNzu/avbIMZyBrA8yektfYGk/eBKNQdqgz5wcn1/crdR0agoM6IMOY98RSH1ht
bundle.tracing.min.jssha384-UWQQs0k3Ocxrcyom6IaEPh2MkDdAQ3s4yevQWelxVKtAoVwU+1HY2z6ZHc89pr0U
bundle.tracing.replay.debug.min.jssha384-aWx1ANNyrGHVtxfRx98KXl+yLmc5D9Nu+C6yBhB7nu8e1THmDLtEzn0Qp4q8rxuG
bundle.tracing.replay.feedback.debug.min.jssha384-CiZpGIbDpySS8qebL2G1Zlr9kg7l72hrTQWnPX4mYGtQQElJInNhviSitMZkpvas
bundle.tracing.replay.feedback.jssha384-H+TNVCyK33WKvr1pXJrlsO6yt0+QAAl0S+uXFEasZ9S6eN4hn+EuxQNGva0OluxT
bundle.tracing.replay.feedback.min.jssha384-vFggwzRj2tI6YR/tg/gaLTGJmVqqtcBRwmuZkcr8vTqfPlXk7/KsqJjMoCZXq2Oq
bundle.tracing.replay.jssha384-n1PU7FMHHTAI4atQWzZjGdkRk102H4jZnH44ty7+GftupU1JzEPkmcQBfZpmo948
bundle.tracing.replay.min.jssha384-SjQKaG7q9FfmUfQAgDPZeq7lH3iN7cToclarPqBM23W9EO6TMIV9QPqUVeF6SSDX
captureconsole.debug.min.jssha384-ZCOJ7hBD9YxnaYif/1thB855ZY/BUXkxF2fj4HshHv6cCmD4F62/9DsPPbRCqGmB
captureconsole.jssha384-UtPXw677pJZ6Lc6Mi8ziwM3N2M0VcuLXshLAHJcPapPfZDRfIwoXUpL/vFW+68Em
captureconsole.min.jssha384-whf1Uc4tj++VhWyA4F950xXu8+tK/SnkZFC4rt0MMI4/PmbAmV/z91Gp9meahpWf
contextlines.debug.min.jssha384-R8xrRe1H8wewWBNFt9hUZxd1MYd+9xgHDdtgt0ERhQ0SHRy2JgipQxhygtNSbg56
contextlines.jssha384-F7fnRXwxc/PuJp3Fln9yBkvurFP7d4G5I/iFtHUJ7nXp01w5Z/zNgn01VdiZdrcE
contextlines.min.jssha384-N4R2VKpjQQUp1HXO+/jJ7XPCZpSDHvhMzckaQynZn/x/VfY/ypQOYdzXfl9abkq8
debug.debug.min.jssha384-SLLDEaesZ0RSbDq5Wa7OnYKkt1rB0SCdgAIRKgNDVQ1vQPxw55XRNZhkCQ/IX8qE
debug.jssha384-Cxn3mYlDjsfdDASSP/nSEEfznfV08NPzNgPotXyugToOTzZ6BB9xfKFtp7BfiP3Q
debug.min.jssha384-coNaJZvn7dw+tlG7PlZfcAMGuzfKoYX0yzc4Ne7ceL3fE4z2nmtscT5PHTJLthJp
dedupe.debug.min.jssha384-1OXDnhemhtqUQZEyFm6lTpyeYmyUZndmJpkSckOM0ec6QkYyqLdnOK1ff0tyDZL4
dedupe.jssha384-hprauMtDR2g1nRzRsoXPJPw76cWQAtHxXucCAbrSPCX6b+R5D5QM/0LMvYTf/Z7h
dedupe.min.jssha384-EZm5yn2tFMcb7GkELWZGb++CfpBZNA/or0SJ4Vl+EXuIXqXO0p353U88ws4qiDgC
extraerrordata.debug.min.jssha384-cvFjsnInRnOq7FqWbOdzHn5iWW86ukXfH820SXIYJZPeXnro8igfjXzNsdu5kc2H
extraerrordata.jssha384-vAUDL919LPvm69cojzXlB/6BTefZWflIXe39dlpGXzAiPzJG5H8dzxuyIOwGz1bc
extraerrordata.min.jssha384-JenptMBhLWzfYsouHVmUbbbDtmQmNkURqNXTz65sfQ3f5L8KhAdQWnatTj02Esja
feedback-modal.debug.min.jssha384-eejkJ4DtHn0vNesDYG2/pocAR5mDBz6YpoGXOM1fiRC4oQwUuoILn0wNj0cwnvcH
feedback-modal.jssha384-ygToZj9+nNWA0DHAqec0o4V7rP2Pav66p8snf8//5O5bFFzyiRqsBVtpo7liZMqg
feedback-modal.min.jssha384-578OLCLl99EaIquotoqS97+YNuSgyfCQ+gptAF8+pl+f4cOiNgTFWBYziMWxU7C2
feedback-screenshot.debug.min.jssha384-dz1dJQz0mjNi2y6eQg/xMXfEZwa0DWiD8EHMJuCj8sArKYrBIa5yypxx+qrm0ewX
feedback-screenshot.jssha384-R8wUcAIU1yrBzszwJ6LmY7bSaACXBuRcCXAvFhT8LbZ/LGR8uzHPy9Df5c+ii+/9
feedback-screenshot.min.jssha384-7C28h9O6M7KUGT9gXNzvGl8qlgGHDYL4fuFBVSU7tRuDjqiaexrq2m705P0fVEvt
feedback.debug.min.jssha384-Oskaph38fKFgvGq85jhvWnzqiLNSUs7ZFR7mIJ5VqKcuLBmEyohpAPmFTEupQvZJ
feedback.jssha384-WkWNquZNg8Tkj+faMLx9qNuVxxu6uYVuP5VYsuFDr79kNolkRfvT4gkgi3U3FcIo
feedback.min.jssha384-wUXvrvcp02ldNFWQU/w9xlbpm4mdu++yoXbtKZ4h6piuJ2bQvRdsp5A3IgRqa7iU
httpclient.debug.min.jssha384-vyaxRVhwTeimcbFecReUEyeF39dJIe308w/FypqWYxVDspEc+9AlvJLkMrN3Qhy2
httpclient.jssha384-PVfREqmhlDwysP1xlSvEg6BNnd+93lE6FwICSoq02VuP9GU9ZajZobAsQhQVJsz+
httpclient.min.jssha384-nnYbXRLR71PTi848DKHx1Kb3AypwN1sfYWoMFBOQOwg0tGv9gFijMkINgKLRXhQW
replay-canvas.debug.min.jssha384-ZfBY06FIulJS4iWTktVv8yRtuk8dQTbngKf3VpKqlDG44b2gPrFOCDO2ka1Xk918
replay-canvas.jssha384-Sv7GoLq1aDvzqU/zyEuqiXtVsw9GTntxp+KX30ddR3IjvL1qHacJmLXy1k3XlVpT
replay-canvas.min.jssha384-OyolHuqjz/GvrJZL8bO0IGOTqz9eEsvHL09gQ8ymPpc3pYFwj4Wgho5Pllrqui9D
reportingobserver.debug.min.jssha384-cnKXwTWeCLX8KHTj+5WMoK3YWxJCDxyUg/1K5OfZ+bTN+C3GviLO/HeuXkhOK3Nc
reportingobserver.jssha384-El/IBZXcvMbjMTy2PgZJwW7dR+ZZr6x8nJcanwVoeXAGaqZ6eLGVCk77Vj0OcasY
reportingobserver.min.jssha384-gl45Xg5Psb8SvLmj7q6yls0FcbB+G//VuKrX/VgCYfPVh0mDDje+ir7VkrH46UkQ
rewriteframes.debug.min.jssha384-SdpOekp1Ow5syGAZ5ijfBBjC1bOFf5QQM+3JliPZcZ+XuAln1+ef4aZ+x7oG6nqI
rewriteframes.jssha384-iN6cjGWbC4uLLnUbjt+uPxHOhYKZRAJEEF9ClA6UATz49R9sOryivuLou9+Cs+PG
rewriteframes.min.jssha384-9b8iesK3DcN82tMjyDbB7KR66meIXvN1UbtQRDJuaIv8QPhOKG20QwPHUCimSMZp
sessiontiming.debug.min.jssha384-+nUNH9gdqXye8tYlvv+GPfJ/EqpEJWEky1Wf/4TtYk0WpxKMMRdC+kik6v39vIo8
sessiontiming.jssha384-gEVxjJBJghS/NCcMCLtAc49yMfMJtYZCGsmXTkquMdrK+8SSWfKm26HIrPmIG7zL
sessiontiming.min.jssha384-j2oQX0J9lDycRXoxBNORQ8bQY/denpMZyX+yT9Np21k8xlDi44DA3DSl42ZmxkgU

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