From 9d30110644ec29dbfb23cf27f15df19a5ca157b4 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 12:20:46 +0200 Subject: [PATCH 1/4] fix(browser): Try multiple options for `lazyLoagIntegration` script parent element lookup --- packages/browser/src/utils/lazyLoadIntegration.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/utils/lazyLoadIntegration.ts b/packages/browser/src/utils/lazyLoadIntegration.ts index 168d1fd1013b..582010e30aa2 100644 --- a/packages/browser/src/utils/lazyLoadIntegration.ts +++ b/packages/browser/src/utils/lazyLoadIntegration.ts @@ -1,5 +1,6 @@ import { SDK_VERSION, getClient } from '@sentry/core'; import type { IntegrationFn } from '@sentry/types'; +import { logger } from '@sentry/utils'; import type { BrowserClient } from '../client'; import { WINDOW } from '../helpers'; @@ -68,7 +69,14 @@ export async function lazyLoadIntegration( script.addEventListener('error', reject); }); - WINDOW.document.body.appendChild(script); + const currentScript = WINDOW.document.currentScript; + const parent = (currentScript && currentScript.parentElement) || WINDOW.document.body || WINDOW.document.head; + + if (!parent) { + throw new Error(`Could not find parent element to insert lazy-loaded ${name} script`); + } + + parent.appendChild(script); try { await waitForLoad; From 77d79a1715641c9468fdff9541ebb30aa3148b33 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 12:28:06 +0200 Subject: [PATCH 2/4] formatting --- packages/browser/src/utils/lazyLoadIntegration.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/browser/src/utils/lazyLoadIntegration.ts b/packages/browser/src/utils/lazyLoadIntegration.ts index 582010e30aa2..275d5055985e 100644 --- a/packages/browser/src/utils/lazyLoadIntegration.ts +++ b/packages/browser/src/utils/lazyLoadIntegration.ts @@ -1,6 +1,5 @@ import { SDK_VERSION, getClient } from '@sentry/core'; import type { IntegrationFn } from '@sentry/types'; -import { logger } from '@sentry/utils'; import type { BrowserClient } from '../client'; import { WINDOW } from '../helpers'; From 03d5c8f558e2c42d5913af5b562efb4fd70571fa Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 12:33:24 +0200 Subject: [PATCH 3/4] don't throw --- packages/browser/src/utils/lazyLoadIntegration.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/browser/src/utils/lazyLoadIntegration.ts b/packages/browser/src/utils/lazyLoadIntegration.ts index 275d5055985e..c60b8733a66c 100644 --- a/packages/browser/src/utils/lazyLoadIntegration.ts +++ b/packages/browser/src/utils/lazyLoadIntegration.ts @@ -1,5 +1,6 @@ import { SDK_VERSION, getClient } from '@sentry/core'; import type { IntegrationFn } from '@sentry/types'; +import { logger } from '@sentry/utils'; import type { BrowserClient } from '../client'; import { WINDOW } from '../helpers'; @@ -71,12 +72,12 @@ export async function lazyLoadIntegration( const currentScript = WINDOW.document.currentScript; const parent = (currentScript && currentScript.parentElement) || WINDOW.document.body || WINDOW.document.head; - if (!parent) { - throw new Error(`Could not find parent element to insert lazy-loaded ${name} script`); + if (parent) { + parent.appendChild(script); + } else { + logger.error(`Could not find parent element to insert lazy-loaded ${name} script`); } - parent.appendChild(script); - try { await waitForLoad; } catch { From 0d1adc9fd1f4b1e5936450bc5bbe5e7c82105144 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 13:55:37 +0200 Subject: [PATCH 4/4] code review suggestions --- packages/browser/src/utils/lazyLoadIntegration.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/browser/src/utils/lazyLoadIntegration.ts b/packages/browser/src/utils/lazyLoadIntegration.ts index c60b8733a66c..82260ae9724f 100644 --- a/packages/browser/src/utils/lazyLoadIntegration.ts +++ b/packages/browser/src/utils/lazyLoadIntegration.ts @@ -1,6 +1,5 @@ import { SDK_VERSION, getClient } from '@sentry/core'; import type { IntegrationFn } from '@sentry/types'; -import { logger } from '@sentry/utils'; import type { BrowserClient } from '../client'; import { WINDOW } from '../helpers'; @@ -70,12 +69,12 @@ export async function lazyLoadIntegration( }); const currentScript = WINDOW.document.currentScript; - const parent = (currentScript && currentScript.parentElement) || WINDOW.document.body || WINDOW.document.head; + const parent = WINDOW.document.body || WINDOW.document.head || (currentScript && currentScript.parentElement); if (parent) { parent.appendChild(script); } else { - logger.error(`Could not find parent element to insert lazy-loaded ${name} script`); + throw new Error(`Could not find parent element to insert lazy-loaded ${name} script`); } try {