Next.js Discord

Discord Forum

AdSense Ads not reloading on route change

Unanswered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
I have implemented ads unit in sidebar but upon navigating the posts ads remains same
Heres my code , I tried to reload with useeffet and Next router but didn't work

import React, { useEffect } from "react"; import { useRouter } from 'next/router'; export default function AdsenseAd() { const router = useRouter(); // get the router object const loadAds = () => { try { if (typeof window !== "undefined") { (window.adsbygoogle = window.adsbygoogle || []).push({}); } } catch (error) { } }; useEffect(() => { loadAds(); }, [router.asPath]); return ( <ins className="adsbygoogle" style={{ display: "block" }} data-ad-client={process.env.NEXT_PUBLIC_ADSENSE_ID} data-ad-slot="8478320254" data-ad-format="auto" data-full-width-responsive="true" ></ins> ); }

0 Replies