Skip to content

Commit 2098ba0

Browse files
committed
improved ads
1 parent 3a6a01c commit 2098ba0

File tree

3 files changed

+82
-23
lines changed

3 files changed

+82
-23
lines changed

src/components/elements/AdUnit.astro

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface Props {
88
adLayout?: string;
99
adLayoutKey?: string;
1010
className?: string;
11+
refreshInterval?: number; // in seconds, default 5
1112
}
1213
1314
const {
@@ -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
)

src/pages/index.astro

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ if (!entry) {
3535
adSlot="1038118330"
3636
adFormat="auto"
3737
className="w-full"
38+
refreshInterval={5}
3839
/>
3940
</div>
4041
</aside>
@@ -45,7 +46,12 @@ if (!entry) {
4546
<LatestNews />
4647

4748
<!-- Autorelaxed Ad -->
48-
<AdUnit adType="autorelaxed" adSlot="9560154927" className="my-8" />
49+
<AdUnit
50+
adType="autorelaxed"
51+
adSlot="9560154927"
52+
className="my-8"
53+
refreshInterval={5}
54+
/>
4955

5056
<Authors />
5157
</main>
@@ -58,6 +64,7 @@ if (!entry) {
5864
adSlot="1038118330"
5965
adFormat="auto"
6066
className="w-full"
67+
refreshInterval={5}
6168
/>
6269
</div>
6370
</aside>

src/styles/global.css

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,3 @@
9090
}
9191

9292
/* Ad handling for responsive layout */
93-
.ad-container:empty,
94-
.ad-container:has(.adsbygoogle:not([data-ad-status])) {
95-
display: none !important;
96-
}
97-
98-
.adsbygoogle[data-ad-status="unfilled"] {
99-
display: none !important;
100-
}
101-
102-
/* Ensure sidebar collapses when ad is not loaded */
103-
aside:has(.ad-container:empty),
104-
aside:has(.ad-container:has(.adsbygoogle:not([data-ad-status]))),
105-
aside:has(.ad-container:has(.adsbygoogle[data-ad-status="unfilled"])) {
106-
display: none !important;
107-
}

0 commit comments

Comments
 (0)