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
<script>
  window.sentryOnLoad = function () {
    Sentry.init({
      // ...
    });
  };
</script>

<script
  src="https://js.sentry-cdn.com/examplePublicKey.min.js"
  crossorigin="anonymous"
></script>

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

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.

Because the loader script injects the actual SDK asynchronously to keep your pageload performance high, the SDK's tracing functionality is only available once the SDK is loaded and initialized. This means that if you e.g. have fetch calls right at the beginning of your application, they might not be traced. If this is a critical issue for you, you have two options to ensure that all your fetch calls are traced:

  • Initialize the SDK in window.sentryOnLoad as described in Custom Configuration. Then make your fetch call in the Sentry.onload callback.
    Example
    Copied
    <script>
      window.sentryOnLoad = function () {
        Sentry.init({
          // ...
        });
      };
    </script>
    
    <script
      src="https://js.sentry-cdn.com/examplePublicKey.min.js"
      crossorigin="anonymous"
    ></script>
    
    <script>
      Sentry.onLoad(function () {
        fetch("/api/users");
      });
    </script>
    
  • Use the CDN bundles instead of the Loader Script. This will ensure that the SDK is loaded synchronously, and that all your fetch calls are traced.

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.32.0/bundle.tracing.min.js"
  integrity="sha384-ZDPmbecFqeLzmm2rmkzakPxfwNzhSzjh6FJjVedB8vC7wpk4LCe/LNoO1Bc02e89"
  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.32.0/bundle.tracing.replay.min.js"
  integrity="sha384-XfieIvQmYVRcXULjnpQAsmqksoPHbCKwOSn/pQz1p6WO9Zm0MFxWQN7KjILt+LuP"
  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.32.0/bundle.replay.min.js"
  integrity="sha384-RXIxAXMJ33lTD1wjmP5rV5dY7q6RwugHELbXktHvBijTv6t/73f6cA2y8fW2KjA+"
  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.32.0/bundle.min.js"
  integrity="sha384-9Cez1tF9J3ANJQRead7VaIP7AFLK7RusEcs4yz2b8VyeiZMlcdpQvspfx9ZCxVIv"
  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-cosGaMEJ4B/ed0EZE7NwQ2fjkb+ZGBSvqAGhketx8LGBwES21gVIvg9P9uR2cXm9
browserprofiling.jssha384-UZlR+h3HBe/oBRgkAdBzl6b2FJHHSMvg3bqCQD6+UfhZ7Eqpyjap9J0stdSYPWhd
browserprofiling.min.jssha384-HJQxXdfYvRd1w0JETG2mUM1K6zapC7Xe2A/wPoW2cvzt1rpvBfvRlntbwdWHQwNF
bundle.debug.min.jssha384-LREj0yMMpAFonHDxB2hl3RHlHeNUyQnxy1cR0dCRZzfkvmytT/d/aKb7WG9JuwZU
bundle.feedback.debug.min.jssha384-iHSlXLjqNqEwUaPoEX8eHHdE+pbTCRN92AXOkEj7GlEBjlPAi+ovRsG4NQmoakw9
bundle.feedback.jssha384-Wa4STRV0C6sStKxqqIYafc3rmCpQ8jkWo3rJ+3LNaU2igTLA8cbOZ+qqyHy4IBDi
bundle.feedback.min.jssha384-HZm3BTt+OyU6QQkTSqaznBhlVvMIJqEAi4okA0rKehxvCGZSdZiwimaezcElR6GE
bundle.jssha384-ETIrqFQSBAkVtxlOnVZmDshT3g9TYS6gJ88QELOObGBlozlHR/jw9Fg0Vk1QUVer
bundle.min.jssha384-9Cez1tF9J3ANJQRead7VaIP7AFLK7RusEcs4yz2b8VyeiZMlcdpQvspfx9ZCxVIv
bundle.replay.debug.min.jssha384-OGChqffUWlJAf0p7nEZ02GUJ9i84mgrC5iRR7SCRKxEChDkDpGBtLkVjNdS2FSwC
bundle.replay.jssha384-WCzhOJJzE6rV0t7RoJvP4evRhlSdX1pmfW7zXFUw4ABWHr/hKNytOp4UerG3CKgg
bundle.replay.min.jssha384-RXIxAXMJ33lTD1wjmP5rV5dY7q6RwugHELbXktHvBijTv6t/73f6cA2y8fW2KjA+
bundle.tracing.debug.min.jssha384-jVWWNkmqtTAQNXea7nggbShJqxP7mrDLIijgJwLQ5sa0/akYvjJImpMwTaVuqdLq
bundle.tracing.jssha384-gYva3YI3O0CHreTFTz8RWFZ62eg8RySjU4Hcm7fE1qH/P/dEc9GScGxPtIDtItUs
bundle.tracing.min.jssha384-ZDPmbecFqeLzmm2rmkzakPxfwNzhSzjh6FJjVedB8vC7wpk4LCe/LNoO1Bc02e89
bundle.tracing.replay.debug.min.jssha384-TP9vrpnhlITGM1hcR5+GnsiWX41iwDpMkWJskq6S+xrD4Hh249JW7Fl46alxyzXR
bundle.tracing.replay.feedback.debug.min.jssha384-FqgPvDGjOOMrA4NmuseqLK4UA++4PpHiMy1mgbmK198R/QAXzYO5Y4c6YMQh5Atk
bundle.tracing.replay.feedback.jssha384-eMwLX8WhdSgQMIoN3izc0kaOjD1ntn2HLdfeMmyoV04ov7JeJNwC4Ga3XV1STVEA
bundle.tracing.replay.feedback.min.jssha384-Ysq3iic+5Yn+pBlZKjcZ0ooGe9g6KnGvGAA6dEFo5tKnz5wUWG0tajuhyqxBqnmP
bundle.tracing.replay.jssha384-WVnde8DxvvPPTxvZ71a467jmsmrhzjU0NulwS0MSTZ2u6UyUYRPiAgohPaDezvli
bundle.tracing.replay.min.jssha384-XfieIvQmYVRcXULjnpQAsmqksoPHbCKwOSn/pQz1p6WO9Zm0MFxWQN7KjILt+LuP
captureconsole.debug.min.jssha384-30zpQE2O18u3gwHifkLmES1uZFxUjkeNfPP2/bGgSkfudxAr89MagV0zeJLqcER1
captureconsole.jssha384-Ye9H4Rsz5IHayV36GjpD2210IoWNJ9G+mgCHmDgolsOn8wFHWkzJQePqavqElHOa
captureconsole.min.jssha384-+QIiq/JqOizpIWEGNvSGUVCG9StyvILc0gP2CQAgJKZGhPzZEYDadyLGJ2EE6oye
contextlines.debug.min.jssha384-jSiwmvUTJfXrozgo3J4LvltUnJTYRESUz0VPAUj4P5Ov4+GKQncRAFVny0uHmWSQ
contextlines.jssha384-bv2cD/Ms9e6wN8CsP93iBPcN97ofYQWptXZuqnxbkzcQNL+u5qV4O9aQQxYCS5BJ
contextlines.min.jssha384-cxTOghLiFOAtUcsaDfz4QKRLGAyOZjnX78NPrTM2z5UuYniobnU0NyXZ2uqfR41f
dedupe.debug.min.jssha384-faAE3JwnwzD5FXdFtdXRag836dUncdnDX5rct+Lny1jByDUZReQz6mTV691dyy1d
dedupe.jssha384-zgzG/ju+vX4sBkX8TrUNMxUyiZk3YQZtGI6Qo6JXY+JYXFiieg9TarTCoeWEaH8z
dedupe.min.jssha384-bkOoZbLSZdhGpAhQ0LXU3RkPTh6Fn2WsD0LdwIoX29FHAeXKaJvjZPhiNOaF0FXr
extraerrordata.debug.min.jssha384-EiNUmAV2gTwY2om1BcszTc+BqL1e38SQ0zQ1VgqAoe5OBWr2n1A17gXprME5popT
extraerrordata.jssha384-LqTcZLal4VueaXPXEKzzJZFVGakrr81R2r3yNpxrU5N9099vEKK9mhON6nkPc9LH
extraerrordata.min.jssha384-U10aN/fnQoO31b5ZsaW/A1KNLZYuJdjqKXinVvCMxP0dZ6RBnIsxg+Nb4aPEPmxH
feedback-modal.debug.min.jssha384-zP8ZnQbVbR8CTOz4X4zW1LHw3Pvbw/9+7fiTXPHNr030NX1S92H+wd/S1AeAtbH3
feedback-modal.jssha384-mRvRkBhHT/4xFeLb1Hn5QtM090qff+B3YhrZKHHBQNq8dJOMG8GVHwyGNMIriG1Y
feedback-modal.min.jssha384-0H/McHFIE2aYPHTXzIovU0yMN/HIxQwbhgv2pVjppM7sLDT9ICV/MtnBo09ck/9s
feedback-screenshot.debug.min.jssha384-P7gUE7DN92KwgF4YGdNC0OzhiXTCQ/yI5eKXMxEfNrbgPFMy/jRxVxuyQ1fhA6Yc
feedback-screenshot.jssha384-R4ysjAwCam1MdMICyK8jK9Aov07Wkip+NZt0vJ9wAzkMatEiYHU85DvhMxtlvIjl
feedback-screenshot.min.jssha384-RBfxzDWMaemuFCDCmxw3/XD14fOtBPsCMet+vF404ai1GSMx91ZpVeXnO5ow2YkT
feedback.debug.min.jssha384-Q4xtWrj3ksnUP2ALXza9zmCNL2EGzXyzmRuIvINN9diLl+FER2XSD780Tn/R7/UU
feedback.jssha384-OkSOtKYsQlGgeXejgT5WzdCGPs7CPBNNoUCm09bKOe3ErM7cGYTubA2ccc3QUvt8
feedback.min.jssha384-fbkmSRXvFztS0XNN8GqGNMUKndjLzMQLKUL4osZDgN5ivrQ6LRsJ7Py7a2dxBcMD
graphqlclient.debug.min.jssha384-Rrm6vImsriOik+XoPSoRi1aIfUnVafjUgMDPTNyRzGae5yxpDc3JWS8duwkuV9SX
graphqlclient.jssha384-FMfQwTT0iVUqNVBCoJWfH+mm7zy3+Oj+CeKDjbwVN0Us+EJ3cZEdChQV/WLcv6Mx
graphqlclient.min.jssha384-xJ5hBS+CrE4ncj1LLO6DQNCYrLakI47oWd8Ke+pLnlXgan7HqYb1UgPa6V5AVQuT
httpclient.debug.min.jssha384-ag6w/5y5fC7MrWVVGLZ6mULaSlGoty5hRVAcrVrWwOUQpjH6dwNzAyn27N3k3XV2
httpclient.jssha384-ZAk7H3NYZZ2hd3Me6Ksm5reKGLKGx3roefoteF3r4tamDghcwDT4skypqj+qdIRz
httpclient.min.jssha384-GlPKwiHwRlntUkUEeYdiNzWSgxxMmz/cGaPTAoavkoN15KOc+Or6r+QVi/c+Hh5g
modulemetadata.debug.min.jssha384-7ZiRRgiBShYqfMBMsAdG3luCWJ+MtTh5tnQymeGlFEGQlK61ORsoIvSyGO5fHJFT
modulemetadata.jssha384-Qcf9rr4EUHveCvy8HngbljCodmY5opv5hcD5GrRSpneMUBtd2UPxKhIEepHHxztO
modulemetadata.min.jssha384-6omBEf7/38MYuP4aEiy4M4X23XmszRKzSVIFsO+KJUpgNHxeqAPEMAI733uoEAuA
multiplexedtransport.debug.min.jssha384-nboHqHPN/tMVlqDJZtZFUi29MOc191HuuKQ26bzavmp1a/ZjMdTdDvB5tK6Wavjk
multiplexedtransport.jssha384-EUuhrySSyjLU6PgYxGW4LXJbX3ZPDC0YFNMr4+rVeylWLgZOYQe/dT1RwDQG8r9m
multiplexedtransport.min.jssha384-1bmym08VVrPSFxvCefSnRgF537iHEEBP7Ux8SQMML0dD3B2c/A7WOA0Y5mKkMRkp
replay-canvas.debug.min.jssha384-YXjMtOhwgrH0OizxxJ0RHXy6OuyRLuT51Wf1UHboSRTAu3zi68FjZN7lQfoZ8kSF
replay-canvas.jssha384-6DJnCsgMiZrGkmD3BDZrSg2c8NHf9D5+LX8Erlw9AxIcpz+zm7rD23vt4gWM5UV0
replay-canvas.min.jssha384-rOrNdGhUB4ta2PVgIhc7DmtXGuOrvaJpuqcQ6G4F7U9tckeusR17tTJJ5G58PspP
replay.debug.min.jssha384-GV8hgO8xQPzhUcaqnHEuz/C4G/XS73Mdy/qdexITDJW+wjF5H5f8Z/nwzO+mnd04
replay.jssha384-5r70nD0bqXpSRP13vjNUuiGXl1lurkssHlq3DX/LoXIEq4kdl3FsOzTK4u7sjjyx
replay.min.jssha384-ueP4wW2f0p+oXXkE/SQbroUXZyMoWTvSTOI080K2QHJb5DY+8KF7a0HeEpvGJZGQ
reportingobserver.debug.min.jssha384-bI2iGwg+tKXJ+mU0pa664+iyiKWDg26sAz+mpqQUEO2tZtdulNSdZ+kmKEtEBKaT
reportingobserver.jssha384-v4INWR6d0pIX7r3QgZoVsfcMnB8nlLhlvPwT6damxyY4abSljXFPsIIIU6eQD0zD
reportingobserver.min.jssha384-Zbu/29d/6oHjmykZhm9qr0E95MAUBezV36sC2aDOoGp9nHyTQ20H3BcK0SNvRKzD
rewriteframes.debug.min.jssha384-iDGlEtgp58lGOpzk350usy6Z2xDu/ETsJjKnkyWKCdHs0wty85K3SJUggAf23cyB
rewriteframes.jssha384-tSIQzkZTUwRJ40cO8THEp7TPQAjVjHcHh+7BDK/X+qpOrMhWJh+jQq9sTJEH4X9M
rewriteframes.min.jssha384-MYY7cf2QAMvZkw67B2P5jvVF4QYbikOIzqr86tf1ffCtjSWl6fy6m1QKjjm8Dn/E
spotlight.debug.min.jssha384-4vnvQz6d34EmfT7Rl5hA6FkvB5O+uF9Dd1enNjW9tPnzcU0XNo4cxc8HjKGpb89O
spotlight.jssha384-QFJfDV3LY4f9TSb7JRS69ErZ5hJHws4ShzraJjHB3oO4oYZXccvXzYPGsjsaeezI
spotlight.min.jssha384-OrSxUIWLTo9Puru+lAXnFFfyFRUzg7zD6BtR5hTENoM2iYx21eonm8/PxAb6aK9/

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