Change the way routing works, so ad will fit better for everyone, we gotta find away to refresh it so we can use <Link> but right now quick and dirty solution
This commit is contained in:
32
src/hooks/useRefreshAds.ts
Normal file
32
src/hooks/useRefreshAds.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/router';
|
||||
|
||||
const useRefreshAds = () => {
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
console.log("Initializing Ad Refresh...");
|
||||
window.googletag = window.googletag || { cmd: [] };
|
||||
|
||||
router.events.on('routeChangeComplete', () => {
|
||||
console.log("Refreshing Ads...");
|
||||
window.googletag.cmd.push(() => {
|
||||
if (window.googletag?.pubads) {
|
||||
window.googletag.pubads().refresh();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
router.events.off('routeChangeComplete', () => {});
|
||||
};
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export default useRefreshAds;
|
||||
Reference in New Issue
Block a user