@@ -8,6 +8,7 @@ interface Props {
88 adLayout? : string ;
99 adLayoutKey? : string ;
1010 className? : string ;
11+ refreshInterval? : number ; // in seconds, default 5
1112}
1213
1314const {
@@ -17,12 +18,16 @@ const {
1718 adLayout,
1819 adLayoutKey,
1920 className = " " ,
21+ refreshInterval = 5 ,
2022} = Astro .props ;
23+
24+ // Generate unique ID for this ad instance
25+ const adId = ` ad-${adType }-${adSlot }-${Math .random ().toString (36 ).substr (2 , 9 )} ` ;
2126---
2227
2328{
2429 GOOGLE_CONFIG .ADSENSE_CLIENT_ID && (
25- <div class = { ` ad-container ${className } ` } style = " min-height: 0;" >
30+ <div id = { adId } class = { ` ad-container ${className } ` } style = " min-height: 0;" >
2631 <script
2732 is :inline
2833 async
@@ -77,17 +82,79 @@ const {
7782 (adsbygoogle = window.adsbygoogle || []).push({ } );
7883 </script >
7984
85+ <script is :inline define :vars = { { adId , refreshInterval }} >
86+ // Ad refresh functionality
87+ (function() {
88+ const adContainer = document .getElementById (adId );
89+ if (! adContainer ) return ;
90+
91+ let refreshTimer ;
92+ let refreshCount = 0 ;
93+ const maxRefreshes = 20 ; // Limit refreshes to prevent abuse
94+
95+ function refreshAd() {
96+ const adElement = adContainer .querySelector (' .adsbygoogle' );
97+ if (! adElement ) return ;
98+
99+ // Check if ad is loaded and visible
100+ const adStatus = adElement .getAttribute (' data-ad-status' );
101+ const isVisible = adContainer .offsetWidth > 0 && adContainer .offsetHeight > 0 ;
102+
103+ if (adStatus === ' filled' && isVisible && refreshCount < maxRefreshes ) {
104+ try {
105+ // Clear existing ad
106+ adElement .innerHTML = ' ' ;
107+
108+ // Re-push the ad
109+ (adsbygoogle = window .adsbygoogle || []).push ({});
110+
111+ refreshCount ++ ;
112+ console .log (` Ad refreshed (${refreshCount }/${maxRefreshes }): ${adId } ` );
113+ } catch (error ) {
114+ console .warn (' Ad refresh failed:' , error );
115+ }
116+ }
117+ }
118+
119+ // Start refreshing after initial load
120+ function startRefreshing() {
121+ if (refreshTimer ) clearInterval (refreshTimer );
122+
123+ refreshTimer = setInterval (refreshAd , refreshInterval * 1000 );
124+
125+ // Also refresh on visibility change (tab focus, etc.)
126+ document .addEventListener (' visibilitychange' , function () {
127+ if (! document .hidden ) {
128+ setTimeout (refreshAd , 1000 ); // Small delay after tab becomes visible
129+ }
130+ });
131+ }
132+
133+ // Wait for ad to load before starting refresh cycle
134+ const checkAdLoaded = setInterval (function () {
135+ const adElement = adContainer .querySelector (' .adsbygoogle' );
136+ if (adElement && adElement .getAttribute (' data-ad-status' ) === ' filled' ) {
137+ clearInterval (checkAdLoaded );
138+ setTimeout (startRefreshing , 2000 ); // Start refreshing 2 seconds after initial load
139+ }
140+ }, 500 );
141+
142+ // Cleanup on page unload
143+ window .addEventListener (' beforeunload' , function () {
144+ if (refreshTimer ) {
145+ clearInterval (refreshTimer );
146+ }
147+ });
148+ } )();
149+ </script >
150+
80151 <style >
81- .ad-container:empty,
82- .ad-container:has(.adsbygoogle:not([data-ad-status])) {
83- display : none ! important ;
152+ .ad-container {
153+ min - height : 0 ! important ;
84154 }
85155 .adsbygoogle {
86156 min - height : 0 ! important ;
87157 }
88- .adsbygoogle[data-ad-status="unfilled"] {
89- display : none ! important ;
90- }
91158 </style >
92159 </div >
93160 )
0 commit comments