File tree Expand file tree Collapse file tree 4 files changed +63
-3
lines changed 
dev-packages/browser-integration-tests/suites/integrations/lazyLoad/validIntegrationNpm 
packages/browser/src/utils Expand file tree Collapse file tree 4 files changed +63
-3
lines changed Original file line number Diff line number Diff line change 1+ import  *  as  Sentry  from  '@sentry/browser' ; 
2+ 
3+ // So we can use this in subject.js 
4+ // We specifically DO NOT set this on window.Sentry as we want to test a non-CDN bundle environment, 
5+ // where window.Sentry is usually not available 
6+ window . _testSentry  =  {  ...Sentry  } ; 
7+ 
8+ Sentry . init ( { 
9+   dsn : 'https://public@dsn.ingest.sentry.io/1337' , 
10+   integrations : [ ] , 
11+ } ) ; 
Original file line number Diff line number Diff line change 1+ window . _testLazyLoadIntegration  =  async  function  run ( )  { 
2+   const  integration  =  await  window . _testSentry . lazyLoadIntegration ( 'httpClientIntegration' ) ; 
3+ 
4+   window . _testSentry . getClient ( ) ?. addIntegration ( integration ( ) ) ; 
5+ 
6+   window . _integrationLoaded  =  true ; 
7+ } ; 
Original file line number Diff line number Diff line change 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 an integration when using the NPM package' ,  async  ( {  getLocalTestUrl,  page } )  =>  { 
7+   const  bundle  =  process . env . PW_BUNDLE  ||  '' ; 
8+   // We only want to run this in non-CDN bundle mode 
9+   if  ( bundle . startsWith ( 'bundle' ) )  { 
10+     sentryTest . skip ( ) ; 
11+   } 
12+ 
13+   const  url  =  await  getLocalTestUrl ( {  testDir : __dirname  } ) ; 
14+ 
15+   await  page . route ( `https://browser.sentry-cdn.com/${ SDK_VERSION }  ,  route  =>  { 
16+     return  route . fulfill ( { 
17+       status : 200 , 
18+       contentType : 'application/javascript;' , 
19+       body : "window.Sentry = window.Sentry || {};window.Sentry.httpClientIntegration = () => ({ name: 'HttpClient' })" , 
20+     } ) ; 
21+   } ) ; 
22+ 
23+   await  page . goto ( url ) ; 
24+ 
25+   const  hasIntegration  =  await  page . evaluate ( '!!window._testSentry.getClient().getIntegrationByName("HttpClient")' ) ; 
26+   expect ( hasIntegration ) . toBe ( false ) ; 
27+ 
28+   const  scriptTagsBefore  =  await  page . evaluate < number > ( 'document.querySelectorAll("script").length' ) ; 
29+ 
30+   await  page . evaluate ( 'window._testLazyLoadIntegration()' ) ; 
31+   await  page . waitForFunction ( 'window._integrationLoaded' ) ; 
32+ 
33+   const  scriptTagsAfter  =  await  page . evaluate < number > ( 'document.querySelectorAll("script").length' ) ; 
34+ 
35+   const  hasIntegration2  =  await  page . evaluate ( '!!window._testSentry.getClient().getIntegrationByName("HttpClient")' ) ; 
36+   expect ( hasIntegration2 ) . toBe ( true ) ; 
37+ 
38+   expect ( scriptTagsAfter ) . toBe ( scriptTagsBefore  +  1 ) ; 
39+ } ) ; 
Original file line number Diff line number Diff line change @@ -33,12 +33,15 @@ const WindowWithMaybeIntegration = WINDOW as {
3333export  async  function  lazyLoadIntegration ( name : keyof  typeof  LazyLoadableIntegrations ) : Promise < IntegrationFn >  { 
3434  const  bundle  =  LazyLoadableIntegrations [ name ] ; 
3535
36-   if  ( ! bundle  ||  ! WindowWithMaybeIntegration . Sentry )  { 
36+   // `window.Sentry` is only set when using a CDN bundle, but this method can also be used via the NPM package 
37+   const  sentryOnWindow  =  ( WindowWithMaybeIntegration . Sentry  =  WindowWithMaybeIntegration . Sentry  ||  { } ) ; 
38+ 
39+   if  ( ! bundle )  { 
3740    throw  new  Error ( `Cannot lazy load integration: ${ name }  ) ; 
3841  } 
3942
4043  // Bail if the integration already exists 
41-   const  existing  =  WindowWithMaybeIntegration . Sentry [ name ] ; 
44+   const  existing  =  sentryOnWindow [ name ] ; 
4245  if  ( typeof  existing  ===  'function' )  { 
4346    return  existing ; 
4447  } 
@@ -61,7 +64,7 @@ export async function lazyLoadIntegration(name: keyof typeof LazyLoadableIntegra
6164    throw  new  Error ( `Error when loading integration: ${ name }  ) ; 
6265  } 
6366
64-   const  integrationFn  =  WindowWithMaybeIntegration . Sentry [ name ] ; 
67+   const  integrationFn  =  sentryOnWindow [ name ] ; 
6568
6669  if  ( typeof  integrationFn  !==  'function' )  { 
6770    throw  new  Error ( `Could not load integration: ${ name }  ) ; 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments