Skip to content

Commit 3f58945

Browse files
committed
fix: ensure Google AdSense script is re-inserted after DOM updates
1 parent b5b0f00 commit 3f58945

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/GoogleAdSense.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ export const GoogleAdSense = ({
4343
existing.remove();
4444
}
4545

46-
// Re-insert the script
47-
const script = document.createElement("script");
48-
script.src = `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-${_publisherId}`;
49-
script.async = true;
50-
script.crossOrigin = "anonymous";
51-
document.head.appendChild(script);
46+
// Delay a frame to make sure the DOM is fully updated
47+
requestAnimationFrame(() => {
48+
// Insert the Google AdSense script
49+
const script = document.createElement("script");
50+
script.src = `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-${_publisherId}`;
51+
script.async = true;
52+
script.crossOrigin = "anonymous";
53+
document.head.appendChild(script);
54+
});
5255
}, [pathname, _publisherId]);
5356

5457
return null;

0 commit comments

Comments
 (0)