An open API service indexing awesome lists of open source software.

https://github.com/daniel15/lazysentry

Example code to lazy load Sentry using ES2020 import()
https://github.com/daniel15/lazysentry

Last synced: about 1 year ago
JSON representation

Example code to lazy load Sentry using ES2020 import()

Awesome Lists containing this project

README

          

# LazySentry

This is some code I use to lazy-load the [Sentry JavaScript SDKs](https://github.com/getsentry/sentry-javascript/), including a basic lazy-loaded React `ErrorBoundary`. Currently, Sentry's JavaScript SDK is [very large](https://github.com/getsentry/sentry-javascript/issues/2707). They have built-in basic lazy loading support, however it [does not work for on-premise installations](https://github.com/getsentry/sentry/issues/22715), and also does not support the tight React integration via an ErrorBoundary.

Until the bundle size is reduced ([as part of v7 of the SDK](https://github.com/getsentry/sentry-javascript/issues/2817)), lazy-loading the SDK can at least defer the cost a bit.

# Files in this repo

- `LazySentryImports`: The actual parts of the Sentry module that we want to use
- `LazySentry`: Handles loading Sentry. If Sentry has not been loaded yet, the functions (eg. `captureException`) are buffered in a queue. Once the Sentry library is loaded, all the buffered calls are replayed
- `LazySentryErrorBoundary`: React ErrorBoundary that handles a lazy-loaded Sentry SDK

# Usage

- Use `LazySentry` instead of `@sentry/browser` or `@sentry/react` in your code.
- Call `init` at some appropriate time, such as after your app has loaded all its main code, or in a `requestIdleCallback` callback.
- If you want to use more features from Sentry, add them to `LazySentryImports` then add wrappers for them in `LazySentry`.

# Example

This code snippet is used in the Sentry initialisation code on dnstools.ws: https://github.com/Daniel15/dnstools/blob/master/src/DnsTools.Web/ClientApp/src/index.tsx. The LazySentry repo is configured as a Git submodule.

# Support

There is no warranty nor any support provided for the code in this repository.