Skip to content

Commit 52f9765

Browse files
committed
fix: complete AdBanner rewrite with proper SSR handling and 2s delay
1 parent 1d046c8 commit 52f9765

1 file changed

Lines changed: 43 additions & 35 deletions

File tree

src/components/ads/AdBanner.tsx

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useEffect, useState, useRef } from "react";
3+
import { useEffect, useState } from "react";
44

55
interface AdBannerProps {
66
dataAdSlot: string;
@@ -13,51 +13,59 @@ const AdBanner: React.FC<AdBannerProps> = ({
1313
dataAdFormat = "auto",
1414
dataFullWidthResponsive = true,
1515
}) => {
16-
const [adLoaded, setAdLoaded] = useState(false);
17-
const containerRef = useRef<HTMLDivElement>(null);
18-
const adInitialized = useRef(false); // Keep adInitialized to prevent multiple pushes
16+
const [isMounted, setIsMounted] = useState(false);
17+
const [adPushed, setAdPushed] = useState(false);
1918

19+
// Step 1: Set mounted state (client-side only)
2020
useEffect(() => {
21-
if (adInitialized.current) return;
21+
setIsMounted(true);
22+
}, []);
23+
24+
// Step 2: Initialize ad after mount with delay
25+
useEffect(() => {
26+
if (!isMounted || adPushed) return;
2227

23-
// Check for available width before pushing
24-
if (!containerRef.current || containerRef.current.offsetWidth === 0) {
25-
// Retry logic could be improved with ResizeObserver but simple timeout for now
26-
const timer = setTimeout(() => {
27-
if (adInitialized.current) return;
28-
try {
29-
if (containerRef.current && containerRef.current.offsetWidth > 0) {
30-
(window as any).adsbygoogle = (window as any).adsbygoogle || [];
31-
(window as any).adsbygoogle.push({});
32-
setAdLoaded(true);
33-
adInitialized.current = true;
34-
}
35-
} catch (e) {
36-
console.error("AdSense retry error:", e);
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);
3734
}
38-
}, 1000); // 1s delay
39-
return () => clearTimeout(timer);
40-
}
35+
} catch (err) {
36+
console.error("AdSense initialization error:", err);
37+
}
38+
}, 2000); // 2 second delay to ensure layout is stable
4139

42-
try {
43-
(window as any).adsbygoogle = (window as any).adsbygoogle || [];
44-
(window as any).adsbygoogle.push({});
45-
setAdLoaded(true);
46-
adInitialized.current = true;
47-
} catch (err) {
48-
console.error("AdSense error:", err);
49-
}
50-
}, []);
40+
return () => clearTimeout(timer);
41+
}, [isMounted, adPushed]);
42+
43+
// Don't render on server side
44+
if (!isMounted) {
45+
return (
46+
<div style={{
47+
minHeight: "100px",
48+
width: "100%",
49+
margin: "20px 0",
50+
background: "#f3f4f6"
51+
}} />
52+
);
53+
}
5154

5255
return (
5356
<div
54-
ref={containerRef}
55-
className="ad-container"
56-
style={{ overflow: "hidden", minHeight: "100px", width: "100%", textAlign: "center", margin: "20px 0" }}
57+
style={{
58+
overflow: "hidden",
59+
minHeight: "100px",
60+
width: "100%",
61+
textAlign: "center",
62+
margin: "20px 0",
63+
padding: "10px"
64+
}}
5765
>
5866
<ins
5967
className="adsbygoogle"
60-
style={{ display: "block" }}
68+
style={{ display: "block", minHeight: "100px" }}
6169
data-ad-client="ca-pub-6253589071371136"
6270
data-ad-slot={dataAdSlot}
6371
data-ad-format={dataAdFormat}

0 commit comments

Comments
 (0)