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.37.0/bundle.tracing.min.js"
  integrity="sha384-MsjLtlOdCf0tSGqePBfBFQzYOXMv3tb6rNQ3sxYNPI2IMPCafjgFEyH+MTpeXLo/"
  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.37.0/bundle.tracing.replay.min.js"
  integrity="sha384-wjBzOy/8wjuYml+vpPLr0j29jHZAHAO08EsqcnCH9id+mobWxwJ2qg2OsHIFtQT9"
  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.37.0/bundle.replay.min.js"
  integrity="sha384-HIejjcwjMbWkaMjlNcTFo6Pak1yGWRHacEkLZfxuOTnTSbqf2n/izQKt0KvIDXRR"
  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.37.0/bundle.min.js"
  integrity="sha384-SIzi/onsceiwgCmtN9SdhhNAYBies6z5Pk06no8Y46ncuTHC4NZFr8rbDkz+vhSb"
  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-o391SSnfnV+pxxVssUlVg9N+5X5ZkFFN3yiq2AWgR3Uw8oWxOLHd8LUQQxVycLxn
browserprofiling.jssha384-bOErwUIMHGEIY/Y6hb2mkv5MNrrLEuvcIooa+1GsA7VhnXsW51jnn878qTQ27xDR
browserprofiling.min.jssha384-S1jQtcxxLLGyXI09ocIJ7s7jWZOdMUxHdXRM8X/72o1dN2EzjdnAkmRZs1iUsyCK
bundle.debug.min.jssha384-V55k9WB/5m1EDb0pviO7mRtH5Qb4maNMckL/Fj9GFWLLlNQ9hVUroaRC6hFBGugH
bundle.feedback.debug.min.jssha384-T39WOjZHWTpnxpgxyD3Hk56kPEDTji6VSvhuOuW17KjcOxY7WvktBJiVEMjB8y5+
bundle.feedback.jssha384-9VB/NpG7VCUTHpNb9hJgkYMDFBy5n3Iv5RrV7n1wxc64BUBW2kW77UqAXVmXOdqa
bundle.feedback.min.jssha384-ZilIc9kg7SZOf5uvK1PubPZTB+diBENPanrLYvPO3cmZiNavRVo219X/ScC0I3IT
bundle.jssha384-4MWLK4xgxOO0VK67iMCq4S9XUczGk1PdhZ0vNbwOOvG0oGitL1EMBQvZUcNAZE70
bundle.min.jssha384-SIzi/onsceiwgCmtN9SdhhNAYBies6z5Pk06no8Y46ncuTHC4NZFr8rbDkz+vhSb
bundle.replay.debug.min.jssha384-KfvKjp5tD2cV16CMOJr771wV7gMAJoeldSLALW5i4VV/pbvQPNJwAdhALBgVNCV9
bundle.replay.jssha384-ER7sTZfEkVkzb/D5x7ru5H57vSdzm5M3lGKO+6AHUG4SHsPY9JyMu6IfIe9N19J5
bundle.replay.min.jssha384-HIejjcwjMbWkaMjlNcTFo6Pak1yGWRHacEkLZfxuOTnTSbqf2n/izQKt0KvIDXRR
bundle.tracing.debug.min.jssha384-t4BahkJgOZH9Xbb3esmui9rqduamEQ6WQDORs9g1lMOPXCW0n3+jAwxNSiADEFiM
bundle.tracing.jssha384-hxZi9v/rjLDhQvfWAAG7VhvELKIm1bJiVop2nlx7AGlZv2KdFXVVhkIBvXjb0IgD
bundle.tracing.min.jssha384-MsjLtlOdCf0tSGqePBfBFQzYOXMv3tb6rNQ3sxYNPI2IMPCafjgFEyH+MTpeXLo/
bundle.tracing.replay.debug.min.jssha384-QMZxQfq9dyaYxF7hnlYcsDoUTSFOs23hboLv7Nb0bCiKMP9SUMw+7a7dNN/X7nLZ
bundle.tracing.replay.feedback.debug.min.jssha384-a58qV5YpFut1UWtiA+gBeub/I4PuyP5S2KzMvKwAcvht8vqc9pt5QM+GQtNDf2KR
bundle.tracing.replay.feedback.jssha384-r8/ZCJdiUo4wU/bdHIKMjdyRsXWKUC+az8CcSLfaUPFXeqvfAyqu2ePIFnmAErmZ
bundle.tracing.replay.feedback.min.jssha384-yWcQyQZVVsA0IksMsck/qfXTEc1hOmF8UfYn6sxV/2UpVHgcUDjHc0Jk1CVxTj4i
bundle.tracing.replay.jssha384-57IKQHJp4Hl5IRNNe6NuNdy2d7A3+IkA5aOKibTxAXn5Ru7qxtRnbc3qvCmBRMpV
bundle.tracing.replay.min.jssha384-wjBzOy/8wjuYml+vpPLr0j29jHZAHAO08EsqcnCH9id+mobWxwJ2qg2OsHIFtQT9
captureconsole.debug.min.jssha384-W/Jr++lYqZMHkhVgn2G7QzAIcRIUF2YxjZrjyzmTZzPLQFghX33HmqrQ5e8LQLCh
captureconsole.jssha384-2tqjvAthH2xPd/5IoFDpfQ1kXLpNO3J92KXKiawY/DPt9/yw9/pldQxZlYKSEN0u
captureconsole.min.jssha384-ESTvt7ilxSrbrzi0I4v9jiH87auE9alqZyY/Kh4ZY/qv+caiPYVNiHWRtieAZ9Qc
contextlines.debug.min.jssha384-4K44ME6g/jmNE7wwikvSPdkInxavaaQNvInU8DK70aSJRntG8vSUZCVPKcZ3+c78
contextlines.jssha384-rkcq9wIX8TkBzC7zMKYF3TFPI5OUYNAmBTaHwGZNSQ8BfbZIjac226T8lpm7Tp8L
contextlines.min.jssha384-Iwarf5ObMwk4FtdAkqD3wsRQmvFbXFZUZYbLURqlvdDanZPjNaU8QmmjwT4fi55Y
debug.debug.min.jssha384-eKLfl/QDSt2XqdL6rYb/qh7QLSfVJ7S/XJvVC+m+4ZULqQxsb0etqqMDfAIHkbWD
debug.jssha384-uTB9DE0qsafH59dYAeYbPmHaiZ28dd1L48rpz37NpIce/9wA6aUlpC0NXPrh+m79
debug.min.jssha384-zbXRTw66QmGCJs3gsnFOWFgRxzrK+TacfdpU8tGqlafzqBUh1dxHOEhHgd5a23ss
dedupe.debug.min.jssha384-q0F/UGsAVfJFiNXrPDJVbwZyRj28ItOuVhVUvx7bVdQmo6wwl1g23PcNHpB7+VhZ
dedupe.jssha384-ruwgJ6eC2hOmzi3Q6CRWg26BOSTv0O1BPAPpjATWhp3Iz4WAH13BKZi/gF+acbBj
dedupe.min.jssha384-lVi7PN7yWr+8HN0y+6mk+ZjJiLHdQ1u5OhMDrC/VMFpqygn1/iNl6X07gd5pPj+x
extraerrordata.debug.min.jssha384-nFenhgiPv5YOgoS1dwF2QBZDrIceSR27CF67n/n+VmpicB228PniK+K8+yP+xLUa
extraerrordata.jssha384-pDLQ1hMrs276b+eLQAjijzMsy0AvGMmwdvWmW7gaCZaqeNSZHgSCRjIa/RjReC04
extraerrordata.min.jssha384-PrDtXnn2TKx4yPhS12JGYMlPYyZSG/jwrDMBoGnkk8EEmpEOzO/ZjnyQOwyoHeMr
feedback-modal.debug.min.jssha384-RBsKI81aRBJI55EfPpcWmnJ396N+soXTmHl4gf4Gh0FmsV0U3NiOI6oRSkCcSBqn
feedback-modal.jssha384-lVwuYof4rnieXKklAhbck34/iwBk617/cGDyP+weoTCQZ3mWKZ4fmakCvYE/es8p
feedback-modal.min.jssha384-jn8a6TIrDSBlnC3e4cZHgTIZNY8IjG5YnlyReLmfOHHzci2DdATmNcNHjf4Qn4vI
feedback-screenshot.debug.min.jssha384-OGsh6rODiAPK3Sibh+kxUF7d/Lz1t/whwJ8oePpj7/nwklTOO9ViUHNzbGJVHB6L
feedback-screenshot.jssha384-+wW2ynCt6sLQ1KnxKVHLEyvk53igq+FLIIAXz6BbT0WjugLSAcbO4c0a7/Sby217
feedback-screenshot.min.jssha384-trI0SsX4TwRkj9SSubh00JrE7/E47UQl5uFP0+17O5MYyZFoKD7RoaD41feK/cvJ
feedback.debug.min.jssha384-y3YnKt6ZmkeGw+tXIfKlP2YTByLtxQf8Qgpln4wT03vGRUhbrT7kC6bFa/qMjvHc
feedback.jssha384-ysQWlGePPV1DH81O14Nv0eCgHLSv7h7P2ZkJYY7p7BmFpMvgfgnAl8cteamxM6Tw
feedback.min.jssha384-QIpZfsdOoz9WSa69OSD8SDKVtVkhpVIhS3o2PO3UJKxFbzsqvnCCeimsBll3JIpO
httpclient.debug.min.jssha384-oFsJfNsJ69Wpxe0aD/VXp1HFlNj5dIWwSGpEBiEhiuFNVM7NyZ/rXj5acybqEF+q
httpclient.jssha384-qUyhEAPmZrQjW4OGfdOQHEDt5sbaomr3OuD08lwU8CcUaK2HAQL+AHL9EbFRMWXd
httpclient.min.jssha384-EQ1Hj1xduomoPbEwZltYPu7XF3/08m46lv1g+RUVhAg/6x5ZXNkJ8Xmnr0SfC26i
replay-canvas.debug.min.jssha384-GoPbvyH+Luj5uhU6KI+GFactblCybVH+W3WGnt0FAjP2Nozm940xkCWsNJn3mmND
replay-canvas.jssha384-jChVA91JuZcYSYpWSIg2jOrgqIS6s5Gbl5eDV3djmMFiUcdbqr3CwJVg5nXhDtoZ
replay-canvas.min.jssha384-e35JLC3KA9LMV1KdfjQAufYtIYT11cKRhiRiOwP+d442infM/AJsyKN9764kPHGM
replay.debug.min.jssha384-xpCAgC88lPfvvzzvX8iU4RbdxMpKRwKa/UKv+jkkSdrQmbtAkAvd4yELfhRzr6O1
replay.jssha384-Pj/qjkEsA4E3AZikmmXA/JRMs8snVrrpvlHjU9bxJC8WnlS6O72Ax2If7mXvORox
replay.min.jssha384-ceZ3t+5ITQuyx42Ui91MCzCzcPOS10PYZwSVkG0kljXO0xL44A4QRiN7Rwz+MLcL
reportingobserver.debug.min.jssha384-zglOnVUceLRWZJUj1XbW962XLFpmnD7jVkWUPFbcXuOdpITx69Zykn6+NiKCryZf
reportingobserver.jssha384-NS38VB5tyl1Gxlnj+vypX6//mqC6z8swpbb9NoTeqe4Hzog7hKsWBGb6kkknbxFd
reportingobserver.min.jssha384-qc81LgVRwu/d6HZbCQ5fkTWe1e2ElXoI8Ia13UhbQHpOrCoFo385fCBxuTF3FbPw
rewriteframes.debug.min.jssha384-uw6Ia26vlKtaBNyDteh/fGfAwSGPTRHwkITbzl0C4C+HjF8hbXyvOMxLzorZr6z/
rewriteframes.jssha384-9DMexcSL/2KavjntBIN4XB/15yyHgbrqZyY4zEOSx5pOQCYyvzqIJ+dxNRRwhOIP
rewriteframes.min.jssha384-iFf9p2ejrgzVu+jUKsznKrbbicymesY2VWL3rWgJAv2tR0VjbZnUZP3s8SJ0dmEQ
sessiontiming.debug.min.jssha384-i762Cu3bwNBhh+8Zdg3G0VRCZEVGEpRzZYsDw50ThlFcXUe2tsb6kdjzyaMAe9JW
sessiontiming.jssha384-D+r4HW3Heq0OrSmSyBSPWG9uRuLo0yZMpQolG3MvNvaeoSo8gVc3m2A3lagBEZdX
sessiontiming.min.jssha384-V6u4ROg+cXeUQMZN3Ar5l4dWD2+myw7erlIFyA/EWssKJgcH1oGp+JukhTAFvB4R

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