Shared Environments / Browser Extensions

Learn how to use Sentry in shared environments (for example in browser extensions or VSCode extensions).

We recommend using JavaScript SDK 8.x and above when using the SDK with shared environments. Check out our migration docs to upgrade from an older SDK version to 8.x and above.

These best practices are relevant for you if you set up Sentry in one of the following use-cases:

  • Browser Extensions
  • VSCode Extensions
  • Third-Party Widgets
  • Plugin Architecture
  • Libraries
  • Any other scenario where you might have multiple Sentry instances running in the same environment

For the use cases listed above, set up a client manually as seen in the example below. In addition, avoid adding any integrations that use global state, like Breadcrumbs or GlobalHandlers. Furthermore, some default integrations that use the global state have to be filtered as in the example below. As a rule of thumb, it's best to avoid using any integrations and to rely on manually capturing errors only in such scenarios.

Copied
import {
  BrowserClient,
  defaultStackParser,
  getDefaultIntegrations,
  makeFetchTransport,
  Scope,
} from "@sentry/browser";

// filter integrations that use the global variable
const integrations = getDefaultIntegrations({}).filter(
  (defaultIntegration) => {
    return !["BrowserApiErrors", "Breadcrumbs", "GlobalHandlers"].includes(
      defaultIntegration.name,
    );
  },
);

const client = new BrowserClient({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  transport: makeFetchTransport,
  stackParser: defaultStackParser,
  integrations: integrations,
});

const scope = new Scope();
scope.setClient(client);

client.init(); // initializing has to be done after setting the client on the scope

// You can capture exceptions manually for this client like this:
scope.captureException(new Error("example"));

To make it a bit simpler but somewhat still maintain "Let Sentry handle unhandled errors" you can use the following code:

Copied
// Init Sentry as shown above

try {
  // Your goes code here
  // as in import and execute your code here
  // and if an error occurs, Sentry will capture it
} catch (error) {
  scope.captureException(error);
}

If you notice that Sentry is not capturing error events from the browser extension, an Inbound Filter might be active. You can disable that by going to your Project Settings > Inbound Filters and disabling filtering out errors known to be caused by browser extensions.

Read more about Inbound Filters in the product documentation under Inbound filters.

Available in all browser-based SDKs since version 8.37.0

If for some reason, the SDK wrongfully detects that it's initialized in a browser extension, you can skip the check by specifying the skipBrowserExtensionCheck option when initializing the SDK:

Copied
import * as Sentry from "@sentry/browser";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  skipBrowserExtensionCheck: true,
  // ...
});

This option is purely meant as an escape hatch if our browser extension check is incorrectly detecting a browser extension. An example for this might be a

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