-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Add lazyLoadIntegration
utility
#11339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...ckages/browser-integration-tests/suites/integrations/lazyLoad/existingIntegration/init.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
import { httpClientIntegration } from '@sentry/browser'; | ||
|
||
window.Sentry = { | ||
...Sentry, | ||
// This would be done by the CDN bundle otherwise | ||
httpClientIntegration: httpClientIntegration, | ||
}; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [], | ||
}); |
7 changes: 7 additions & 0 deletions
7
...ges/browser-integration-tests/suites/integrations/lazyLoad/existingIntegration/subject.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
window._testLazyLoadIntegration = async function run() { | ||
const integration = await window.Sentry.lazyLoadIntegration('httpClientIntegration'); | ||
|
||
window.Sentry.getClient()?.addIntegration(integration()); | ||
|
||
window._integrationLoaded = true; | ||
}; |
24 changes: 24 additions & 0 deletions
24
...ckages/browser-integration-tests/suites/integrations/lazyLoad/existingIntegration/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
|
||
sentryTest('it bails if the integration is already loaded', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
const hasIntegration = await page.evaluate('!!window.Sentry.getClient()?.getIntegrationByName("HttpClient")'); | ||
expect(hasIntegration).toBe(false); | ||
|
||
const scriptTagsBefore = await page.evaluate('document.querySelectorAll("script").length'); | ||
|
||
await page.evaluate('window._testLazyLoadIntegration()'); | ||
await page.waitForFunction('window._integrationLoaded'); | ||
|
||
const scriptTagsAfter = await page.evaluate('document.querySelectorAll("script").length'); | ||
|
||
const hasIntegration2 = await page.evaluate('!!window.Sentry.getClient()?.getIntegrationByName("HttpClient")'); | ||
expect(hasIntegration2).toBe(true); | ||
|
||
expect(scriptTagsAfter).toBe(scriptTagsBefore); | ||
}); |
12 changes: 12 additions & 0 deletions
12
dev-packages/browser-integration-tests/suites/integrations/lazyLoad/validIntegration/init.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = { | ||
...Sentry, | ||
// Ensure this is _not_ set | ||
httpClientIntegration: undefined, | ||
}; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [], | ||
}); |
7 changes: 7 additions & 0 deletions
7
...ckages/browser-integration-tests/suites/integrations/lazyLoad/validIntegration/subject.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
window._testLazyLoadIntegration = async function run() { | ||
const integration = await window.Sentry.lazyLoadIntegration('httpClientIntegration'); | ||
|
||
window.Sentry.getClient()?.addIntegration(integration()); | ||
|
||
window._integrationLoaded = true; | ||
}; |
24 changes: 24 additions & 0 deletions
24
dev-packages/browser-integration-tests/suites/integrations/lazyLoad/validIntegration/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
|
||
sentryTest('it allows to lazy load an integration', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
const hasIntegration = await page.evaluate('!!window.Sentry.getClient()?.getIntegrationByName("HttpClient")'); | ||
expect(hasIntegration).toBe(false); | ||
|
||
const scriptTagsBefore = await page.evaluate<number>('document.querySelectorAll("script").length'); | ||
|
||
await page.evaluate('window._testLazyLoadIntegration()'); | ||
await page.waitForFunction('window._integrationLoaded'); | ||
|
||
const scriptTagsAfter = await page.evaluate<number>('document.querySelectorAll("script").length'); | ||
|
||
const hasIntegration2 = await page.evaluate('!!window.Sentry.getClient()?.getIntegrationByName("HttpClient")'); | ||
expect(hasIntegration2).toBe(true); | ||
|
||
expect(scriptTagsAfter).toBe(scriptTagsBefore + 1); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { SDK_VERSION, getClient } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import type { BrowserClient } from '../client'; | ||
import { WINDOW } from '../helpers'; | ||
|
||
// This is a map of integration function method to bundle file name. | ||
const LazyLoadableIntegrations = { | ||
replayIntegration: 'replay', | ||
replayCanvasIntegration: 'replay-canvas', | ||
feedbackIntegration: 'feedback', | ||
captureConsoleIntegration: 'captureconsole', | ||
contextLinesIntegration: 'contextlines', | ||
linkedErrorsIntegration: 'linkederrors', | ||
debugIntegration: 'debug', | ||
dedupeIntegration: 'dedupe', | ||
extraErrorDataIntegration: 'extraerrordata', | ||
httpClientIntegration: 'httpclient', | ||
reportingObserverIntegration: 'reportingobserver', | ||
rewriteFramesIntegration: 'rewriteframes', | ||
sessionTimingIntegration: 'sessiontiming', | ||
} as const; | ||
|
||
const WindowWithMaybeIntegration = WINDOW as { | ||
Sentry?: Partial<Record<keyof typeof LazyLoadableIntegrations, IntegrationFn>>; | ||
}; | ||
|
||
/** | ||
* Lazy load an integration from the CDN. | ||
* Rejects if the integration cannot be loaded. | ||
*/ | ||
export async function lazyLoadIntegration(name: keyof typeof LazyLoadableIntegrations): Promise<IntegrationFn> { | ||
const bundle = LazyLoadableIntegrations[name]; | ||
|
||
if (!bundle || !WindowWithMaybeIntegration.Sentry) { | ||
throw new Error(`Cannot lazy load integration: ${name}`); | ||
} | ||
|
||
// Bail if the integration already exists | ||
const existing = WindowWithMaybeIntegration.Sentry[name]; | ||
if (typeof existing === 'function') { | ||
return existing; | ||
} | ||
|
||
const url = getScriptURL(bundle); | ||
const script = WINDOW.document.createElement('script'); | ||
script.src = url; | ||
script.crossOrigin = 'anonymous'; | ||
|
||
const waitForLoad = new Promise<void>((resolve, reject) => { | ||
script.addEventListener('load', () => resolve()); | ||
script.addEventListener('error', reject); | ||
}); | ||
|
||
WINDOW.document.body.appendChild(script); | ||
|
||
try { | ||
await waitForLoad; | ||
} catch { | ||
throw new Error(`Error when loading integration: ${name}`); | ||
} | ||
|
||
const integrationFn = WindowWithMaybeIntegration.Sentry[name]; | ||
|
||
if (typeof integrationFn !== 'function') { | ||
throw new Error(`Could not load integration: ${name}`); | ||
} | ||
|
||
return integrationFn; | ||
} | ||
|
||
function getScriptURL(bundle: string): string { | ||
const client = getClient<BrowserClient>(); | ||
const options = client && client.getOptions(); | ||
const baseURL = (options && options.cdnBaseUrl) || 'https://browser.sentry-cdn.com'; | ||
|
||
return new URL(`/${SDK_VERSION}/${bundle}.min.js`, baseURL).toString(); | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/browser/test/unit/utils/lazyLoadIntegration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { TextDecoder, TextEncoder } from 'util'; | ||
import { SDK_VERSION, lazyLoadIntegration } from '../../../src'; | ||
import * as Sentry from '../../../src'; | ||
const patchedEncoder = (!global.window.TextEncoder && (global.window.TextEncoder = TextEncoder)) || true; | ||
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails (deleted in afterAll) | ||
const patchedDecoder = (!global.window.TextDecoder && (global.window.TextDecoder = TextDecoder)) || true; | ||
|
||
import { JSDOM } from 'jsdom'; | ||
|
||
const globalDocument = global.document; | ||
const globalWindow = global.window; | ||
const globalLocation = global.location; | ||
|
||
describe('lazyLoadIntegration', () => { | ||
beforeEach(() => { | ||
const dom = new JSDOM('<body></body>', { | ||
runScripts: 'dangerously', | ||
resources: 'usable', | ||
}); | ||
|
||
global.document = dom.window.document; | ||
// @ts-expect-error need to override global document | ||
global.window = dom.window; | ||
global.location = dom.window.location; | ||
// @ts-expect-error For testing sake | ||
global.Sentry = undefined; | ||
}); | ||
|
||
// Reset back to previous values | ||
afterEach(() => { | ||
global.document = globalDocument; | ||
global.window = globalWindow; | ||
global.location = globalLocation; | ||
}); | ||
|
||
afterAll(() => { | ||
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails | ||
patchedEncoder && delete global.window.TextEncoder; | ||
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails | ||
patchedDecoder && delete global.window.TextDecoder; | ||
}); | ||
|
||
test('it rejects invalid name', async () => { | ||
// @ts-expect-error For testing sake - otherwise this bails out anyhow | ||
global.Sentry = Sentry; | ||
|
||
// @ts-expect-error we want to test this | ||
await expect(() => lazyLoadIntegration('invalid!!!')).rejects.toThrow('Cannot lazy load integration: invalid!!!'); | ||
}); | ||
|
||
test('it rejects without global Sentry variable', async () => { | ||
await expect(() => lazyLoadIntegration('httpClientIntegration')).rejects.toThrow( | ||
'Cannot lazy load integration: httpClientIntegration', | ||
); | ||
}); | ||
|
||
test('it does not inject a script tag if integration already exists', async () => { | ||
// @ts-expect-error For testing sake | ||
global.Sentry = Sentry; | ||
|
||
const integration = await lazyLoadIntegration('httpClientIntegration'); | ||
|
||
expect(integration).toBe(Sentry.httpClientIntegration); | ||
expect(global.document.querySelectorAll('script')).toHaveLength(0); | ||
}); | ||
|
||
test('it injects a script tag if integration is not yet loaded xxx', async () => { | ||
// @ts-expect-error For testing sake | ||
global.Sentry = { | ||
...Sentry, | ||
httpClientIntegration: undefined, | ||
}; | ||
|
||
// We do not await here, as this this does not seem to work with JSDOM :( | ||
// We have browser integration tests to check that this actually works | ||
void lazyLoadIntegration('httpClientIntegration'); | ||
|
||
expect(global.document.querySelectorAll('script')).toHaveLength(1); | ||
expect(global.document.querySelector('script')?.src).toEqual( | ||
`https://browser.sentry-cdn.com/${SDK_VERSION}/httpclient.min.js`, | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.