Skip to content

Commit

Permalink
fix(browser): Try multiple options for lazyLoadIntegration script p…
Browse files Browse the repository at this point in the history
…arent element lookup (#13717)

Change the order for looking up parent elements to inject the integration script:

1. `document.body`
2. `document.head`
3. `document.currentScript.parentElement`
  • Loading branch information
Lms24 committed Sep 18, 2024
1 parent e1783a6 commit 03eb680
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/browser/src/utils/lazyLoadIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ export async function lazyLoadIntegration(
script.addEventListener('error', reject);
});

WINDOW.document.body.appendChild(script);
const currentScript = WINDOW.document.currentScript;
const parent = WINDOW.document.body || WINDOW.document.head || (currentScript && currentScript.parentElement);

if (parent) {
parent.appendChild(script);
} else {
throw new Error(`Could not find parent element to insert lazy-loaded ${name} script`);
}

try {
await waitForLoad;
Expand Down

0 comments on commit 03eb680

Please sign in to comment.