This is some code I use to lazy-load the Sentry JavaScript SDKs, including a basic lazy-loaded React ErrorBoundary
. Currently, Sentry's JavaScript SDK is very large. They have built-in basic lazy loading support, however it does not work for on-premise installations, 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), lazy-loading the SDK can at least defer the cost a bit.
LazySentryImports
: The actual parts of the Sentry module that we want to useLazySentry
: 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 replayedLazySentryErrorBoundary
: React ErrorBoundary that handles a lazy-loaded Sentry SDK
- 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 arequestIdleCallback
callback. - If you want to use more features from Sentry, add them to
LazySentryImports
then add wrappers for them inLazySentry
.
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.
There is no warranty nor any support provided for the code in this repository.