11"use client" ;
22
3- import { useEffect , useState } from "react" ;
3+ import { useEffect , useState , useRef } from "react" ;
44
55interface AdBannerProps {
66 dataAdSlot : string ;
@@ -14,52 +14,74 @@ const AdBanner: React.FC<AdBannerProps> = ({
1414 dataFullWidthResponsive = true ,
1515} ) => {
1616 const [ isMounted , setIsMounted ] = useState ( false ) ;
17- const [ adPushed , setAdPushed ] = useState ( false ) ;
17+ const containerRef = useRef < HTMLDivElement > ( null ) ;
18+ const adPushedRef = useRef ( false ) ;
1819
19- // Step 1: Set mounted state (client-side only)
2020 useEffect ( ( ) => {
2121 setIsMounted ( true ) ;
2222 } , [ ] ) ;
2323
24- // Step 2: Initialize ad after mount with delay
2524 useEffect ( ( ) => {
26- if ( ! isMounted || adPushed ) return ;
25+ if ( ! isMounted || adPushedRef . current ) return ;
2726
28- const timer = setTimeout ( ( ) => {
29- try {
30- if ( typeof window !== 'undefined' ) {
31- ( window as any ) . adsbygoogle = ( window as any ) . adsbygoogle || [ ] ;
32- ( window as any ) . adsbygoogle . push ( { } ) ;
33- setAdPushed ( true ) ;
27+ let attempts = 0 ;
28+ const maxAttempts = 10 ;
29+
30+ const tryInitAd = ( ) => {
31+ attempts ++ ;
32+
33+ // Check if container has valid width
34+ if ( containerRef . current ) {
35+ const width = containerRef . current . offsetWidth ;
36+
37+ if ( width > 0 ) {
38+ // Container has valid width, initialize ad
39+ try {
40+ ( window as any ) . adsbygoogle = ( window as any ) . adsbygoogle || [ ] ;
41+ ( window as any ) . adsbygoogle . push ( { } ) ;
42+ adPushedRef . current = true ;
43+ console . log ( `AdSense initialized successfully with width: ${ width } px` ) ;
44+ } catch ( err ) {
45+ console . error ( "AdSense initialization error:" , err ) ;
46+ }
47+ } else if ( attempts < maxAttempts ) {
48+ // Width is 0, retry after delay
49+ console . log ( `Container width is 0, retrying... (attempt ${ attempts } /${ maxAttempts } )` ) ;
50+ setTimeout ( tryInitAd , 500 ) ;
51+ } else {
52+ console . error ( "Failed to initialize AdSense: container width remained 0 after max attempts" ) ;
3453 }
35- } catch ( err ) {
36- console . error ( "AdSense initialization error:" , err ) ;
3754 }
38- } , 2000 ) ; // 2 second delay to ensure layout is stable
55+ } ;
56+
57+ // Start trying after initial delay
58+ const timer = setTimeout ( tryInitAd , 1000 ) ;
3959
4060 return ( ) => clearTimeout ( timer ) ;
41- } , [ isMounted , adPushed ] ) ;
61+ } , [ isMounted ] ) ;
4262
43- // Don't render on server side
4463 if ( ! isMounted ) {
4564 return (
4665 < div style = { {
4766 minHeight : "100px" ,
4867 width : "100%" ,
49- margin : "20px 0" ,
68+ maxWidth : "1200px" ,
69+ margin : "20px auto" ,
5070 background : "#f3f4f6"
5171 } } />
5272 ) ;
5373 }
5474
5575 return (
5676 < div
77+ ref = { containerRef }
5778 style = { {
5879 overflow : "hidden" ,
5980 minHeight : "100px" ,
6081 width : "100%" ,
82+ maxWidth : "1200px" ,
6183 textAlign : "center" ,
62- margin : "20px 0 " ,
84+ margin : "20px auto " ,
6385 padding : "10px"
6486 } }
6587 >
0 commit comments