-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This is especially a requirement for feedback & screenshot capturing, but ideally we can build this in a generic way so also others things (e.g. the Loader) can leverage it.
For feedback, we want to allow users to capture & annotate screenshots. This is quite some code, so we want to lazy load this. However, it is not super easy today to set up lazy loading, and it requires bundler-specifics to work properly.
This proposes to provide an easy to use solution for this: client.lazyAddIntegration(integrationName)
.
This API could be used like this:
// instead of
const { feedbackIntegration } = await import('@sentry/browser');
getClient().addIntegration(feedbackIntegration(feedbackOptions));
// You can do:
getClient().lazyAddIntegration('feedbackIntegration', feedbackOptions);
Which would under the hood do something like this:
async function lazyAddIntegration(functionName: string, options: any) {
const fileName = convertFunctionNameToFileName(functionName); // TODO, maybe an allow-list?
const url = `https://browser.sentry-cdn.com/${SDK_VERSION}/${fileName}.min.js`;
const script = document.createElement('script');
script.setAttribute('src', url);
const waitForLoad = new Promise((resolve, reject) => {
script.addEventListener('load', resolve);
script.addEventListener('error', reject);
});
document.body.appendChild(script);
await waitForLoad;
const integration = typeof window[functionName] === 'function' ? window[functionName](options) : undefined;
if(integration) {
getClient().addIntegration(integration);
}
}
This leverages the fact that we can already publish integrations as CDN bundles, which are versioned through the URL. This way, we can easily load the correct version of an integration for the current version.
The additional benefit of this is that it fully interops with non-lazy loading. E.g. if you don't want this, you can just do addIntegration()
normally - nothing needs to be done! This also means that testing this is easy, and users who may not want to load from a CDN can just await import()
themselves as before.