22
33import { faClose } from '@fortawesome/free-solid-svg-icons' ;
44import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
5- import { useEffect , useRef , useState } from 'react' ;
5+ import { useEffect , useState } from 'react' ;
66
77import { cn } from '@web/lib/utils' ;
88
@@ -52,111 +52,16 @@ const AdTemplate = ({
5252 const pubId = useAdSenseClient ( ) ;
5353
5454 const [ isHidden , setIsHidden ] = useState ( false ) ;
55- const adRef = useRef < HTMLModElement > ( null ) ;
56- const [ isAdInitialized , setIsAdInitialized ] = useState ( false ) ;
5755
58- // Reset initialization state when ad is hidden/shown
5956 useEffect ( ( ) => {
60- if ( isHidden ) {
61- setIsAdInitialized ( false ) ;
62- }
63- } , [ isHidden ] ) ;
64-
65- useEffect ( ( ) => {
66- if ( ! pubId || isHidden || isAdInitialized || ! adRef . current ) {
67- return ;
68- }
69-
70- let isInitializing = false ;
71- let retryCount = 0 ;
72- const maxRetries = 50 ; // 5 seconds max (50 * 100ms)
73-
74- const initializeAd = ( ) => {
75- const adElement = adRef . current ;
76- if ( ! adElement || isInitializing || isAdInitialized ) {
77- return ;
78- }
79-
80- // Check if ad is already initialized by AdSense
81- const adStatus = adElement . getAttribute ( 'data-adsbygoogle-status' ) ;
82- if (
83- adStatus === 'done' ||
84- adStatus === 'filled' ||
85- adStatus === 'unfilled'
86- ) {
87- setIsAdInitialized ( true ) ;
88- return ;
57+ if ( window ) {
58+ try {
59+ ( window . adsbygoogle = window . adsbygoogle || [ ] ) . push ( { } ) ;
60+ } catch ( e ) {
61+ console . error ( e ) ;
8962 }
90-
91- // Check if the element has dimensions
92- const rect = adElement . getBoundingClientRect ( ) ;
93- if ( rect . width === 0 || rect . height === 0 ) {
94- retryCount ++ ;
95- if ( retryCount < maxRetries ) {
96- // Retry after a short delay if element has no dimensions
97- setTimeout ( initializeAd , 100 ) ;
98- }
99- return ;
100- }
101-
102- // Check if adsbygoogle script is loaded
103- if ( typeof window !== 'undefined' && window . adsbygoogle ) {
104- isInitializing = true ;
105- try {
106- // Double-check the element hasn't been initialized by another call
107- const currentStatus = adElement . getAttribute (
108- 'data-adsbygoogle-status' ,
109- ) ;
110- if ( ! currentStatus || currentStatus === '' ) {
111- ( window . adsbygoogle = window . adsbygoogle || [ ] ) . push ( { } ) ;
112- setIsAdInitialized ( true ) ;
113- } else {
114- setIsAdInitialized ( true ) ;
115- }
116- } catch ( e ) {
117- console . error ( 'AdSense initialization error:' , e ) ;
118- isInitializing = false ;
119- }
120- } else {
121- retryCount ++ ;
122- if ( retryCount < maxRetries ) {
123- // Wait for script to load
124- setTimeout ( ( ) => {
125- if ( typeof window !== 'undefined' && window . adsbygoogle ) {
126- initializeAd ( ) ;
127- }
128- } , 100 ) ;
129- }
130- }
131- } ;
132-
133- // Use ResizeObserver to wait for element to have dimensions
134- const resizeObserver = new ResizeObserver ( ( entries ) => {
135- for ( const entry of entries ) {
136- if ( entry . contentRect . width > 0 && entry . contentRect . height > 0 ) {
137- initializeAd ( ) ;
138- resizeObserver . disconnect ( ) ;
139- break ;
140- }
141- }
142- } ) ;
143-
144- if ( adRef . current ) {
145- resizeObserver . observe ( adRef . current ) ;
14663 }
147-
148- // Fallback: try after a delay even if ResizeObserver doesn't fire
149- const timeoutId = setTimeout ( ( ) => {
150- initializeAd ( ) ;
151- resizeObserver . disconnect ( ) ;
152- } , 500 ) ;
153-
154- return ( ) => {
155- resizeObserver . disconnect ( ) ;
156- clearTimeout ( timeoutId ) ;
157- isInitializing = false ;
158- } ;
159- } , [ pubId , isHidden , isAdInitialized ] ) ;
64+ } , [ ] ) ;
16065
16166 const InfoText = ! pubId
16267 ? ( ) => (
@@ -174,7 +79,6 @@ const AdTemplate = ({
17479 { ! isHidden && (
17580 < >
17681 < ins
177- ref = { adRef }
17882 className = { cn ( 'adsbygoogle' , isHidden ? 'hidden' : '' ) }
17983 style = { {
18084 display : 'block' ,
0 commit comments