Skip to content

Commit b9e85c1

Browse files
authored
feat(browser): Add moduleMetadataIntegration lazy loading support (#13817)
This PR fixes #13803, and adds support for `moduleMetadataIntegration` to be lazy loaded, in [this](https://docs.sentry.io/platforms/javascript/configuration/integrations/#2-load-from-cdn-with-lazyloadintegration) manner. This integration is crucial for the [micro-frontend recommended solution](https://docs.sentry.io/platforms/javascript/best-practices/micro-frontends/#automatically-route-errors-to-different-projects-depending-on-module).
1 parent a91a5ba commit b9e85c1

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
Sentry.init({
4+
dsn: 'https://[email protected]/1337',
5+
integrations: [],
6+
});
7+
8+
window.Sentry = {
9+
...Sentry,
10+
// Ensure this is _not_ set
11+
moduleMetadataIntegration: undefined,
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
window._testLazyLoadIntegration = async function run() {
2+
const integration = await window.Sentry.lazyLoadIntegration('moduleMetadataIntegration');
3+
4+
window.Sentry.getClient()?.addIntegration(integration());
5+
6+
window._integrationLoaded = true;
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from '@playwright/test';
2+
import { SDK_VERSION } from '@sentry/browser';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
6+
sentryTest('it allows to lazy load the moduleMetadata integration', async ({ getLocalTestUrl, page }) => {
7+
const url = await getLocalTestUrl({ testDir: __dirname });
8+
9+
await page.route(`https://browser.sentry-cdn.com/${SDK_VERSION}/modulemetadata.min.js`, route => {
10+
return route.fulfill({
11+
status: 200,
12+
contentType: 'application/javascript;',
13+
body: "window.Sentry.moduleMetadataIntegration = () => ({ name: 'ModuleMetadata' })",
14+
});
15+
});
16+
17+
await page.goto(url);
18+
19+
const hasIntegration = await page.evaluate('!!window.Sentry.getClient()?.getIntegrationByName("ModuleMetadata")');
20+
expect(hasIntegration).toBe(false);
21+
22+
const scriptTagsBefore = await page.evaluate<number>('document.querySelectorAll("script").length');
23+
24+
await page.evaluate('window._testLazyLoadIntegration()');
25+
await page.waitForFunction('window._integrationLoaded');
26+
27+
const scriptTagsAfter = await page.evaluate<number>('document.querySelectorAll("script").length');
28+
29+
const hasIntegration2 = await page.evaluate('!!window.Sentry.getClient()?.getIntegrationByName("ModuleMetadata")');
30+
expect(hasIntegration2).toBe(true);
31+
32+
expect(scriptTagsAfter).toBe(scriptTagsBefore + 1);
33+
});

dev-packages/browser-integration-tests/utils/generatePlugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const IMPORTED_INTEGRATION_CDN_BUNDLE_PATHS: Record<string, string> = {
3737
reportingObserverIntegration: 'reportingobserver',
3838
sessionTimingIntegration: 'sessiontiming',
3939
feedbackIntegration: 'feedback',
40+
moduleMetadataIntegration: 'modulemetadata',
4041
};
4142

4243
const BUNDLE_PATHS: Record<string, Record<string, string>> = {

packages/browser/rollup.bundle.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const reexportedPluggableIntegrationFiles = [
1212
'rewriteframes',
1313
'sessiontiming',
1414
'feedback',
15+
'modulemetadata',
1516
];
1617

1718
browserPluggableIntegrationFiles.forEach(integrationName => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { moduleMetadataIntegration } from '@sentry/core';

packages/browser/src/utils/lazyLoadIntegration.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const LazyLoadableIntegrations = {
2121
rewriteFramesIntegration: 'rewriteframes',
2222
sessionTimingIntegration: 'sessiontiming',
2323
browserProfilingIntegration: 'browserprofiling',
24+
moduleMetadataIntegration: 'modulemetadata',
2425
} as const;
2526

2627
const WindowWithMaybeIntegration = WINDOW as {

0 commit comments

Comments
 (0)