Load the Google AdSense external script to my remix site. #8570
-
After going through a lot of hurdles I managed to put on the ads.txt file, robots.txt and dynamic sitemap.xml to my site and they all work flawlessly. A while back my website was approved by google AdSense for monetization, all I need to do was add a link tag and few scripts tags to my website to get the ads showing. Adding links was very when using remix, the issue comes when I need to load external async scripts. All I need to add was the script below, but I get a lot of hydration errors:
The problem:
What I tried My code
update:
when I add the script in the above way. I can see the ads. But I need to reload the page every time. The first time the page loads, the script is not being executed. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Running into a similar issue myself, some input by the devs would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
It seems to me that all you need to do is add your script tag inside the Do you have a sample repo that I can check out? // root.tsx
return (
<html>
<head>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456" crossorigin="anonymous"></script>
...
</head>
...
</html>
) |
Beta Was this translation helpful? Give feedback.
-
I found an article that explains fairly well the issue that I am facing (but with NextJS), it appears to happen when the adsense script is loaded even without any ad elements, specifically on the Brave browser. export default function App() {
return (
<html lang="en">
<head>
<script
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456"
crossOrigin="anonymous"
></script>
</head>
<body>
{/* <Outlet /> */}
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
} https://deniapps.com/blog/unhandled-runtime-error-hydration-and-google-ads-in-nextjs |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm having the same problem. Have you found a solution? |
Beta Was this translation helpful? Give feedback.
-
After manually adding the script in a useEffect, I use setTimeout before pushing the ad and this seems to work for me. This is inside a component I made to hold an AdUnit. const handleAds = () => {
if (!document.getElementById("adsbygoogleaftermount")) {
const script = document.createElement("script");
script.id = "adsbygoogleaftermount";
script.type = "text/javascript";
script.async = true;
script.src =
"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxxxx";
script.crossOrigin = "anonymous";
document.head.appendChild(script);
}
setTimeout(() => {
// @ts-expect-error adsbygoogle exists
if (!window.adsbygoogle || initializedRef.current) return; // NOTE: only need to push once
// @ts-expect-error adsbygoogle exists
(adsbygoogle = window.adsbygoogle || []).push({});
initializedRef.current = true;
}, 200);
};
useEffect(() => {
handleAds();
}, []); |
Beta Was this translation helpful? Give feedback.
After manually adding the script in a useEffect, I use setTimeout before pushing the ad and this seems to work for me. This is inside a component I made to hold an AdUnit.