Spaces:
Running
Running
Update components/GoogleAd.tsx
Browse files- components/GoogleAd.tsx +27 -6
components/GoogleAd.tsx
CHANGED
|
@@ -1,16 +1,34 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
interface GoogleAdProps {
|
| 4 |
slotId?: string;
|
| 5 |
className?: string;
|
|
|
|
| 6 |
}
|
| 7 |
|
| 8 |
-
const GoogleAd: React.FC<GoogleAdProps> = ({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
useEffect(() => {
|
|
|
|
|
|
|
|
|
|
| 10 |
try {
|
| 11 |
// @ts-ignore
|
| 12 |
const adsbygoogle = window.adsbygoogle || [];
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
} catch (e) {
|
| 15 |
console.error("AdSense error:", e);
|
| 16 |
}
|
|
@@ -23,13 +41,16 @@ const GoogleAd: React.FC<GoogleAdProps> = ({ slotId, className }) => {
|
|
| 23 |
Currently using a placeholder.
|
| 24 |
*/}
|
| 25 |
<ins className="adsbygoogle"
|
|
|
|
| 26 |
style={{ display: 'block', width: '100%' }}
|
| 27 |
data-ad-client="ca-pub-8848704570658587"
|
| 28 |
data-ad-slot={slotId || "1234567890"}
|
| 29 |
-
data-ad-format=
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
</div>
|
| 32 |
);
|
| 33 |
};
|
| 34 |
|
| 35 |
-
export default GoogleAd;
|
|
|
|
| 1 |
+
|
| 2 |
+
import React, { useEffect, useRef } from 'react';
|
| 3 |
|
| 4 |
interface GoogleAdProps {
|
| 5 |
slotId?: string;
|
| 6 |
className?: string;
|
| 7 |
+
format?: 'auto' | 'rectangle' | 'vertical' | 'horizontal';
|
| 8 |
}
|
| 9 |
|
| 10 |
+
const GoogleAd: React.FC<GoogleAdProps> = ({
|
| 11 |
+
slotId,
|
| 12 |
+
className,
|
| 13 |
+
format = 'auto'
|
| 14 |
+
}) => {
|
| 15 |
+
const adRef = useRef<HTMLModElement>(null);
|
| 16 |
+
const hasRequestedAd = useRef(false);
|
| 17 |
+
|
| 18 |
useEffect(() => {
|
| 19 |
+
// Prevent double execution in React Strict Mode
|
| 20 |
+
if (hasRequestedAd.current) return;
|
| 21 |
+
|
| 22 |
try {
|
| 23 |
// @ts-ignore
|
| 24 |
const adsbygoogle = window.adsbygoogle || [];
|
| 25 |
+
|
| 26 |
+
// Safety Check: Only push ad if the element is actually visible in the DOM
|
| 27 |
+
// "No slot size for availableWidth=0" error fix.
|
| 28 |
+
if (adRef.current && adRef.current.offsetWidth > 0 && adRef.current.innerHTML === "") {
|
| 29 |
+
adsbygoogle.push({});
|
| 30 |
+
hasRequestedAd.current = true;
|
| 31 |
+
}
|
| 32 |
} catch (e) {
|
| 33 |
console.error("AdSense error:", e);
|
| 34 |
}
|
|
|
|
| 41 |
Currently using a placeholder.
|
| 42 |
*/}
|
| 43 |
<ins className="adsbygoogle"
|
| 44 |
+
ref={adRef}
|
| 45 |
style={{ display: 'block', width: '100%' }}
|
| 46 |
data-ad-client="ca-pub-8848704570658587"
|
| 47 |
data-ad-slot={slotId || "1234567890"}
|
| 48 |
+
data-ad-format={format}
|
| 49 |
+
// CRITICAL: Set to false to prevent the ad from expanding beyond the sidebar/panel width
|
| 50 |
+
// and overlaying the entire application, which blocks clicks on desktop.
|
| 51 |
+
data-full-width-responsive="false"></ins>
|
| 52 |
</div>
|
| 53 |
);
|
| 54 |
};
|
| 55 |
|
| 56 |
+
export default GoogleAd;
|