diff --git a/client/src/components/adsense-ad.tsx b/client/src/components/adsense-ad.tsx index 961ba7e..6c66d16 100644 --- a/client/src/components/adsense-ad.tsx +++ b/client/src/components/adsense-ad.tsx @@ -18,15 +18,25 @@ export default function AdSenseAd({ const adRef = useRef(null); useEffect(() => { - try { - // Ensure adsbygoogle is loaded - if (typeof window !== 'undefined') { - // @ts-ignore - (window.adsbygoogle = window.adsbygoogle || []).push({}); + const timer = setTimeout(() => { + try { + // Ensure adsbygoogle is loaded + if (typeof window !== 'undefined' && adRef.current) { + // Check if element has width before initializing ad + const elementWidth = adRef.current.offsetWidth; + if (elementWidth > 0) { + // @ts-ignore + (window.adsbygoogle = window.adsbygoogle || []).push({}); + } else { + console.warn('AdSense element has no width, skipping initialization'); + } + } + } catch (error) { + console.error('AdSense initialization error:', error); } - } catch (error) { - console.error('AdSense initialization error:', error); - } + }, 100); // Small delay to ensure DOM is rendered + + return () => clearTimeout(timer); }, []); const adStyle: React.CSSProperties = { @@ -42,7 +52,7 @@ export default function AdSenseAd({ } return ( -
+