Skip to content

Commit 03eb680

Browse files
authored
fix(browser): Try multiple options for lazyLoadIntegration script parent 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`
1 parent e1783a6 commit 03eb680

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/browser/src/utils/lazyLoadIntegration.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ export async function lazyLoadIntegration(
6868
script.addEventListener('error', reject);
6969
});
7070

71-
WINDOW.document.body.appendChild(script);
71+
const currentScript = WINDOW.document.currentScript;
72+
const parent = WINDOW.document.body || WINDOW.document.head || (currentScript && currentScript.parentElement);
73+
74+
if (parent) {
75+
parent.appendChild(script);
76+
} else {
77+
throw new Error(`Could not find parent element to insert lazy-loaded ${name} script`);
78+
}
7279

7380
try {
7481
await waitForLoad;

0 commit comments

Comments
 (0)