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.32.0/bundle.tracing.min.js"
  integrity="sha384-azPZNenw1tFrK+792hW27tmLnD4rHaeVToH/tzGQEC8vTwaE8DeelviliETYPmdA"
  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.32.0/bundle.tracing.replay.min.js"
  integrity="sha384-aY5ljJRirHHzPuiA7KRG8Rg7iLdOJyAE4Vw6MXVPZBXCl2K834P6JgVD67ERE9z3"
  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.32.0/bundle.replay.min.js"
  integrity="sha384-RDFkKgVf2+OCm4ZKTbszQt9gsZSixdfC29BagGhnX+mIafcxrkcOtECsQ+gDlWj9"
  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.32.0/bundle.min.js"
  integrity="sha384-QCnJs++wm4rMgRGo++QPVTI3psIt827xIV9hBp/7YCMW6rvgv53000Q5/zFNpIbr"
  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-l0X5eWQphuxj1MDjF276fHS6HG2qYyWANJssfMDR9mSv4NB9x/wbiph7ctHmKoRZ
browserprofiling.jssha384-NhiMXVnla/64JQcEk/vt3DROhxJKKc38dnU13hEs+rcoP5ge/kgmHk7yVXoKVtP5
browserprofiling.min.jssha384-egNWf96hCkRE9/KFfa7pEBjcvswdTPHTczvqMrHWxeKv5AtwUjOJqK/jhmlxIwCk
bundle.debug.min.jssha384-2Hh/QH89BdwsZYJIw10ZPKgtO4ifFYZG+2ekKaGVFepvjtzN5km2gG05LZUGNhU+
bundle.feedback.debug.min.jssha384-BPYB2fgeEl6f3GPgbQWtcvu7NmgM05s0LNwsA1UhZ66mKgx6AAhHLQBh0MPD7SM0
bundle.feedback.jssha384-Z97zODEHsJiRsNd+HhmxcTAqmG1MX2oYOSzD/bLgHEEdGVcJepbKPiOFSowCmFkS
bundle.feedback.min.jssha384-3Q3GWnuOyFF/SPSGSo31zJZcsSNLcQqcsNKX9XuNE6E/4ikZzJ+JVocmGgMb1aWY
bundle.jssha384-GN5Inz8ZwBXJfHPbe4llQjwvzwlzlCdU//x/e3uREv1Ebut77N5w30TY3N069G6j
bundle.min.jssha384-QCnJs++wm4rMgRGo++QPVTI3psIt827xIV9hBp/7YCMW6rvgv53000Q5/zFNpIbr
bundle.replay.debug.min.jssha384-BGnMGk0cksnjnM4gcqizRaDdDUCdhyn91+P2vDer3CPySAWO2XDu0EK9g2KCoCMQ
bundle.replay.jssha384-skq23fHJ76QmoQkKFg/p5iYeKPTNMLp1TEw2s3m02FVfOQ2PIlhjhKqx8JzkoWFB
bundle.replay.min.jssha384-RDFkKgVf2+OCm4ZKTbszQt9gsZSixdfC29BagGhnX+mIafcxrkcOtECsQ+gDlWj9
bundle.tracing.debug.min.jssha384-8HF7iWtlp4uYTHy5w4ucy0wBEry563QNPvnKhYij3NoSqOCA48N9Wm7HjzvTEZkm
bundle.tracing.jssha384-3Nz7ZPBzdG/p51gR5Ljnzo+sCqcQ/qTCNKN1X8/RaJIjFl4HJdJbcKchToH1TZEK
bundle.tracing.min.jssha384-azPZNenw1tFrK+792hW27tmLnD4rHaeVToH/tzGQEC8vTwaE8DeelviliETYPmdA
bundle.tracing.replay.debug.min.jssha384-+7h54TB1y9c+8dXf9dUIJgPMLk/x4VAkrQst2M7iQbnsmouQqikbF4J3aOxogbJE
bundle.tracing.replay.feedback.debug.min.jssha384-W8MI3PVjoOcBzD7+P7HX60ehG2Acv+3+KwnemEBt+Yx1vSEYIPeoFgfw0emVDpo3
bundle.tracing.replay.feedback.jssha384-hfwpRBKSS6qBtHUxW4ekkDJHoeQmnfoH88txRZSBQEljqhOlfeuhBAwijT5ZwMQW
bundle.tracing.replay.feedback.min.jssha384-GHTjIrbHZ+8PIFegHWG8Q0+ZeaOkmERX0iVeYlf9weCR639U6DciE8ebQOBEFOFz
bundle.tracing.replay.jssha384-tF9n/gfPR6k6oWv4LbxU4hdRPQxAWCmCsdV562j947hgmmVqLf/vougr9oWq/zGo
bundle.tracing.replay.min.jssha384-aY5ljJRirHHzPuiA7KRG8Rg7iLdOJyAE4Vw6MXVPZBXCl2K834P6JgVD67ERE9z3
captureconsole.debug.min.jssha384-HLhvpHLwJuzeoZu4HbuMoUOZFSDe9jkY+DT2H6As8sR8tghqBNqZdxu0LeWWZYW+
captureconsole.jssha384-0FyXwFlyNGG+YPuTblS8EagrCOgP1HKvWJd3aZ2Su29eTc2qaz5Oval+rdmLbIOq
captureconsole.min.jssha384-KxvYNSjkot/u6zSXP6+MLBdIwt91T+KuUJg0rf1/MxPIoGVkIlhqp72/iZEXmPRn
contextlines.debug.min.jssha384-uUiUmhRUzx8AMU9m8YWNNcomdrwpGKamLLORF4cEEvlJz/sq/3pcaCrfzlwbDXKo
contextlines.jssha384-Q63UWDGobZntsORR7BqeCT84rAX2qKNQzugPxtlaKxBAHkvRWYdK7QJz6bbgpoDw
contextlines.min.jssha384-456jfP2XneVR5oo7NIJPZ8otNNApg0Bz/OmsYAXymcdFjmRa6jOmwgMrL7oTBElx
debug.debug.min.jssha384-avap/3n+063DRLu2dgLlYts7fLA8Q0zMggcJKDHdBSusFPh56IMd3AkMeaHPLE2Y
debug.jssha384-dG4RbJvTtZr5/IRPeR1VzR8FPzyVc3ewKMsWFMUIe2mZ8dv9LKfDWtolXqZ3FT1j
debug.min.jssha384-PSJFvWP06lvc7tred+wWukyt5FrjlnjM4qHN2txWAylFSEz6ynLH87aH3p/8m1Om
dedupe.debug.min.jssha384-+H6Aq/FHZMMZTyUMO+vHNH6oW8A78zzNz9BJTRVmyzeT+CpoJ6pjThyiE2TWwu3S
dedupe.jssha384-SAyFxZXL0DHFjWSw7LzKlLFX9CnNpc2vqZO8OeqWjvi+AM1YsShL8q4idlORi5RU
dedupe.min.jssha384-cK1FL2Xtfc+dvpHhBuXaO70Wvh2EH6vOtnJ2fkqnGKCbozfUUo9nsvroBMcv0dJM
extraerrordata.debug.min.jssha384-wDxYSgrRY59nQKBEhVJGSrc9V28WJ3CKH0t63iQ4UFQwR++nuUe/h9AS5RkawIUC
extraerrordata.jssha384-0IdIAewOEClaip7IJqND+0g1DxzRN0K/m7EtAmd9ohOno0FXt/2ES846Mhz6Bg+4
extraerrordata.min.jssha384-814RS2zWYV2VEgtPqB2IfiRpzpOGzxj+hZhbYbynJp0/Z531gj0t64hUc8XE/x14
feedback-modal.debug.min.jssha384-nMm8WPqMKNzDI2MZ5R4lDCW7YRiI9wBdRCx9w58K8YETxQewkMhEiE/NIsmVHEEt
feedback-modal.jssha384-9sMFOuqTBHwvPR2P09KF9gYjwTXfFHqMHqt2nRQr2Qk+qJ4cc4TJTRCXYq24qZjD
feedback-modal.min.jssha384-RK05do9Dc8D8KAZZOH6IeLLyqlowR4VPSsK9q7IwfMXygi5Y3tW869YgsOo3DHqM
feedback-screenshot.debug.min.jssha384-RF5FsLUKGLc1zfddNJL5Td+9Iw1pK5Iv2S3/swugzMIcEatQcjIf3wvmbvEk23IM
feedback-screenshot.jssha384-FsBx2CIB8RNYslK0g/xzUhOlYwiNgyEXzVIBpPG1iSn8P+uw/5rLznMNdAGph6vH
feedback-screenshot.min.jssha384-jRzcjqvc58ZYYQfKNMWy/3QQcXEySaWjaltfWZ5uR7jvwyVpgCLnuIo3mvgW7AyU
feedback.debug.min.jssha384-1v5FxX5Tp5kLrzju8E8XuDHrO8OUbl1GOfbv6QEfYkwk/fjTNPcUOlUFnuWCArPt
feedback.jssha384-AwA1h62CUQo2a+sRjEtOeQ7663QI7luVIjvpMvz0z8gAhAmBYQgTA3gVTDEmBtFK
feedback.min.jssha384-D0PhfwZpfmCp6LHI9oksYWrLjFODbCvLQEiOnNM3EhvT+VzvQOot/fvJx6UfXw87
httpclient.debug.min.jssha384-OhoMGY9nv+Uq7oKseD2WYIr0xsATNxUygTVRb8oBm9KAw6u2qx1JUHHDAIShQaZ1
httpclient.jssha384-L+9WZnuec8pNyYkIKpQid/uy9zQzIOp19WoKgfdbYwZZ1YYL1ucJ4qsNVYU6vlBo
httpclient.min.jssha384-NuOoyV5iSfx1wyH9ZqMVSNKPHRKk2U9jSad1dUaz4wnAe7ub1LeTA3GLjNZd5EVO
replay-canvas.debug.min.jssha384-DLQbzCYhqDSq8H6p2KA83clBKLjzKL2DiI7YfcV+EjWZw2HJGucXZcMCdjbRegNc
replay-canvas.jssha384-VjcOkZ9I68sDyCXjVoseElr8ghVSX+S1aB5w7TFVlHA/IxZve7iExg3wir6uBW4d
replay-canvas.min.jssha384-oJNCsVTuDwjvrpPpKMxow5E1uFPWTfbOAy6Z/SlPjpKgFVQImlnXqb9sS+5Dbpg/
replay.debug.min.jssha384-CqYuplohmkk3vOMRG7t5WaxG1UDuz/Hq3FX9G3el72IsR6fp7MB9SOMRpTh2gWGY
replay.jssha384-8fg9xrZuAOOov514vqVkG0TfXwrx0YKgkixM6eq+Go7TovPq+XmGJuSy7QaNltzw
replay.min.jssha384-q/f8G7dQVthVkMQa+DE317WDhJvXr572Z5gCUQ+AxTTPNtaLjw4NbwWyc0LPSEEm
reportingobserver.debug.min.jssha384-osi6SELy47aEDAHI89wxtzhR76fQG4NT1jdSYPjwKHDrNkQU8SVrSfajZWtzaz3k
reportingobserver.jssha384-HcAMUWZh172LQKRxOBAMTmBzc7fkZpi3Nnw/p4AW3TPyrIxhcC8hhvk9CRHRrz/N
reportingobserver.min.jssha384-rLG5dXt+v3X9UkGxj91Tvz6PT48RybrIPRigMKP9NSQO0Irp2Ge6Z2sQPArRfsou
rewriteframes.debug.min.jssha384-dlPJ8igefIsda3+ReCDgaS/3y0dW82xFe5bumo2qkgJu9q/NvisGLvX7qL92QwMq
rewriteframes.jssha384-ubkurekmc04MnugyBh4/rJHXKcnXDXWRMJB5AMkcI96kw3JLCPFGtna/+dPGlmFr
rewriteframes.min.jssha384-ZUp8w68q8lNY+Z+WhQfcSdQAKRZ1eFZXdnTAkNWB6o/pd0+tYNNmbA8J3dDzVzql
sessiontiming.debug.min.jssha384-LgPkRPZtDGSDOLfi1C8GBP86Z6w8PHs6FnBjgufehS36IibWDC3UXmJAO6UNqxwl
sessiontiming.jssha384-S8AjR6sCvc3vg+zZyvM+Ahye7BF98w5Ig3spMi1LNJTPLgCMLbhMzd9zs9wdFeAs
sessiontiming.min.jssha384-r2QrdwGm7Jpre4X8stxYJXvMq2k/DnnIdXLrBvVSTiNpOKzkvFCMkU0dMb1e7LW0

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