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.10.0/bundle.tracing.min.js"
  integrity="sha384-6ONrpTW8Zo4CpzQZozYVIyLJJPb2drswvvZRpyjojZD6bcU989tkzIMQByjQfVPT"
  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.10.0/bundle.tracing.replay.min.js"
  integrity="sha384-gg7ZQnpaAQSPPXCORyJsNwWEddMwsiUJ5t9lb+uBGNI6rT6v6wustaoAoPu0wyfo"
  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.10.0/bundle.replay.min.js"
  integrity="sha384-ZRQEnPn09jgcl8sSOLR1g2kE7IRtOP0H6mBvuwlWQ+Zch5RNUqFkwboHNjCm9S8i"
  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.10.0/bundle.min.js"
  integrity="sha384-zca5Xmpga91lhTvN37S1YDL1+XscEU48qYjyFrNnp1M3J+kmN9015xBwRXv60RLh"
  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-JCKxv9+ykrPjubd+O3WywYunZ6IOhWadPWXlDPd4+YauQkoeOzdn5Kz+rSR9kVpi
browserprofiling.jssha384-c15JWpXPSb+B6ogcW7zVB3wUSTIkSk0uS6VWnIFGfawNL7gYl5mHqnbU2vENooHe
browserprofiling.min.jssha384-IydOujNdLbCaFr9yxNccE3v97xByyA89zRSnsz0ruiI4YCylueHtql/xWuAi4ig1
bundle.debug.min.jssha384-mO5Xpw/6unqGH78badRcneM+05Onb/O+KUvSke6aLeMDT+7UHlvcWfBIjog3Redi
bundle.feedback.debug.min.jssha384-p0fexev4RZFYUshrY76VeTTn6ViFMXeaxdwBHyNIgLl6xiOIXK+DKAK/WmoMZXGM
bundle.feedback.jssha384-3ssPZIFhLaXlZlwhAwG+vbrJcdDQfNfw8omc/YhYisyQwMJqgXf/aIwVo3WFe1uD
bundle.feedback.min.jssha384-kgbU2vrebZfTsnKqznxp9CIzVzPVUODjR3WDYPB4P6+WqivRV/Qt2eNzj87l4xn4
bundle.jssha384-1T0f+iHNuQJ8yYuklvFnJ28j2qIhByCFjtBxZUta7/qKK4mOHr8rQG4ZQ+B/8TOx
bundle.min.jssha384-zca5Xmpga91lhTvN37S1YDL1+XscEU48qYjyFrNnp1M3J+kmN9015xBwRXv60RLh
bundle.replay.debug.min.jssha384-3hCVt7LHiGm15GoODywWbrV97XoUrrrmKCYJ3jHY7KGqBX7rzeybnUsDiJlhd8yr
bundle.replay.jssha384-rga9O6c9s0JfZ8CL6Ryh/Jtz23mchlQqjDH60rNRALaFbAm0zd7ZLeRtLRD9zDcZ
bundle.replay.min.jssha384-ZRQEnPn09jgcl8sSOLR1g2kE7IRtOP0H6mBvuwlWQ+Zch5RNUqFkwboHNjCm9S8i
bundle.tracing.debug.min.jssha384-c+8KrqELEHtMmLj2MxBk+4oQ9dZYTMrY0kcw8+wdAtFW+/V5hxb92NXRWVdrMTGR
bundle.tracing.jssha384-W9P5+7biKyUA+Fa7uUuY/+ykltsLLhAZQIs+0ZPbem60asxev3k5G3Guu3/wGM+W
bundle.tracing.min.jssha384-6ONrpTW8Zo4CpzQZozYVIyLJJPb2drswvvZRpyjojZD6bcU989tkzIMQByjQfVPT
bundle.tracing.replay.debug.min.jssha384-ULAkDedqxPFSGVqle9Shk1OUj/qSCaf/uOrGXDNRtu/b8d+iKuRZxAoiJi0f4VG3
bundle.tracing.replay.feedback.debug.min.jssha384-d0PFaAlmc6o2NJx7VcySHa6yIfUYdm3rOtyNYhmwTk5qBWl2GofvevKuq1WhZu/u
bundle.tracing.replay.feedback.jssha384-1IYu9GgyInb4mT1Xd6DXQUhohAwKCHiUcMtnDwBqQRX3FlXpfsQ5JM//AnWkao0g
bundle.tracing.replay.feedback.min.jssha384-qO0iL1yJy/2yIvnctJFZw48ph8pgdkqH4kLUU8PkSC1ZoYFNXF+G1ab4WiKrgUzL
bundle.tracing.replay.jssha384-bFVEQbZJFx29zX2R9ueQxT63YHzcmxCIMaXbxB1s7IBSFX7J3jrKKHrh2yK7zkVM
bundle.tracing.replay.min.jssha384-gg7ZQnpaAQSPPXCORyJsNwWEddMwsiUJ5t9lb+uBGNI6rT6v6wustaoAoPu0wyfo
captureconsole.debug.min.jssha384-JHHWUxQibulWrbMIOmgXk+EEGzA63rnjCxNzjRvL9x8VGlje3jrL+YL8royM0GrH
captureconsole.jssha384-ufgLN7GYJ419A8L78xdKVeKSwlNHLhHgLBeJFTItVTajoa+VhT1bWA+UCPtCrM3P
captureconsole.min.jssha384-5soqNJcgklwireGyVK2sE9otWdCdGhg5kAKUKM6p1/jBi581WnVSUOXOObU/GJCT
contextlines.debug.min.jssha384-xr5Th0eoPW9Xlw6Y3MuZYbPUaIezIPa/48QLNeia3A6R+Bc4+MDoT+imBrREZ87/
contextlines.jssha384-sfOTTmkp4pvSQbRedbG+RbQjVAoleSuZJ73igeCTV0lR+dYnxl+sm8nk+0kujih/
contextlines.min.jssha384-noPef4jTn+yxLx7hAUYxIFcJf5JHG+s3cUHEvkv6/pM4cUMGyQLxCIlQGUVVJ44e
debug.debug.min.jssha384-HmqZPjC2JHgao4PNsHSEsLWmLdwNAoHvW4s1rAo6Peso3L5pebXm0pRSaToazFW3
debug.jssha384-pB1aDOD1IhkFelJ/SPrnr1i2I8PLU61w0NY+zPo0CEHFcS7Xvo6B0Hy7E7q/alek
debug.min.jssha384-qEiZCSU4V7fo236gjnpTXjkUfMFXCiW5usbka6y/EKTVlbXHFR5whwhkOltfvH9w
dedupe.debug.min.jssha384-fkI44RGzH99QlAcWky1IgbzBj1iDX6Ggih3ISXsub22jXp7+Yff4SM55EZgz+mom
dedupe.jssha384-3nTg99mXy+tgoTLVxR4zaHl8Palgfv/G5vozVQPjmBgkUn4ZM3Ku8lEkcxYqcIbh
dedupe.min.jssha384-1iqL1kRs+Q3iknlZnzgsJKSE1lBZz025xr82l89U/qkdosCRXroLws6Ot/NJc9sC
extraerrordata.debug.min.jssha384-FIxYuo3ydDiRz+m62ln6lAqqt1j+tcnujdL0GfCl+PLJgjl51weW9LJJfmXqSmQ8
extraerrordata.jssha384-PzoSXI7aqzQ5BYnH2azvKXnlcOZeO41/1+IzBqps6VSzpbmy0+wp3u6VxVHyXjtt
extraerrordata.min.jssha384-kumX6Ud9dh3c3X73YCzmFnzne5wPx4OiVCq/5kTn9WZ5nri/9wHRcLqeXE2LPT6b
feedback-modal.debug.min.jssha384-fekcC/PVqbsRVUVHF3kU9nNH18XbZiqZ2qzNXBhSJzyw+B5Z/zWOQ9B/CT4zCVUj
feedback-modal.jssha384-vudx06ruiEGFMlUyt6fbvD3/YI4BCO1TKp14mMCKPMwm66fhdW+Hk+EPzpYz4WzL
feedback-modal.min.jssha384-iKNR0US7FWtGEBoaswW0fieIHYfXA3qtvXm1Jbtta7uquyyZieqsLnUUHou0GDb1
feedback-screenshot.debug.min.jssha384-N/rTYw8Ns1KBvYx7ugd7iK1ZZur9id3YE18sDkhdc109Lep0eZmD9K4lMC9/4oJp
feedback-screenshot.jssha384-pfgGn3QZSshQuNP0W4IY9xK6dTpi37bsZgwJtHCnLUzYiAooK4/T+8SBS4Ry/jtT
feedback-screenshot.min.jssha384-wwHMF/Zod/61rXGDiv/jfBERzE9Yn6AVtANJ7AvvgF8JEKDaSvFmD4jVoa9G4VmA
feedback.debug.min.jssha384-Lj2Qt9bxIz1idyMGNUqHu6DkKiSqYYrQeGQjY8cKhai6N2YausquT58yS6vBBz+3
feedback.jssha384-xXlq0tYP0B1jMIuYJP6F6Jz+sgwKQ0cPneefn1TdRigC2v0XEy+fOQgsGjrWGI6l
feedback.min.jssha384-jjG7Fald5Y+nKTUo6vrnXxQaPc9pFn46vALMwcREbChj3RSOGYH8HTeOkYDaWwiE
httpclient.debug.min.jssha384-9toG4xzu/szLmaahpx4rBQEh0WdiWFXC4s02YgLZTX95ADC2hY2iKr9DB4NiN6H1
httpclient.jssha384-m4n28WaTcy42zyH1f+CRqEw4L1UwsDHTKpuoJV0/BCkHZe7ZFi6g7c+QXTMJjwV/
httpclient.min.jssha384-sNYmXGQ1d5vyZFCg9GoeFCM1ayJ0hyHtXTRTqI6O3Qtn7lwlgIA7j2r+NmSnQhGV
replay-canvas.debug.min.jssha384-dsaMkg5zxms0gW98uu8Ccg8f97joxBzENMxLLFZcL7oS2jLucNSfhPa5bconEmeY
replay-canvas.jssha384-5MjuHboFcNWUPObx6qu08tZLfXU0fcVD9ky5OEO7oi4s9b0N0OyTFuhP+Ahtm37b
replay-canvas.min.jssha384-oQrxRNmH+qaR2QsE8goWsZNkZiLH/XIJtBpGCuFwCuH/pcA4CECrP6uGI7oYipoy
reportingobserver.debug.min.jssha384-Uaqdx1WTxI9RkZwkpORH6aVj9nHAeTXqLRgjOjk7g6KK5TBBAD6TA4W3xydTKVub
reportingobserver.jssha384-wuhZfZNwZqhfTdSUMdy+ea6IJwxjGTIeru8W+NymltThW83+0qleFJWBRIky3FUd
reportingobserver.min.jssha384-y3L5xqnwHCBBqk7ZL/lcWTALkjZ+sWgpAhCq/wJtoZNbgbKLGz+iB8Bja4B+tFGL
rewriteframes.debug.min.jssha384-TuhEZAaZLZ1Hc/Wq8ro1fpK3sZipIpy1vFizLjkukXPCjjjNTPRsxbyTryc9gRBg
rewriteframes.jssha384-GKfc+8q5duFMTHielcsDKUpNzFadulbfnYLSvWKpa21s5i0AuGbNCweJPQ69uwCL
rewriteframes.min.jssha384-XIwSPlZ5vVOcgfaLcs9TRqFMghB+blZ2PWtyQMtojV6K01G/22L/u2C1qOVFBo0J
sessiontiming.debug.min.jssha384-SO8/51cMmmTGyxsF5aIXDWwusrAQIKGoQ6Z7pivwTUk6cO37a/IpzZi2FxtS989b
sessiontiming.jssha384-Zh2ISs49geGhBFLQfMwV6PX81BFNVoyggcCEVxBN2p4o+2tS5xpCrdGE5hEPVVFL
sessiontiming.min.jssha384-5aecyicN2TTNI3tFrbu6iHld41mRFyYg0ol7l17UsOjkSkVeUSBsCHz70yc6fdpo

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