Spaces:
Running
Running
Update components/GoogleAd.tsx
Browse files- components/GoogleAd.tsx +20 -9
components/GoogleAd.tsx
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import React, { useEffect, useRef } from 'react';
|
| 3 |
|
| 4 |
interface GoogleAdProps {
|
|
@@ -15,7 +14,14 @@ const GoogleAd: React.FC<GoogleAdProps> = ({
|
|
| 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 |
|
|
@@ -24,7 +30,6 @@ const GoogleAd: React.FC<GoogleAdProps> = ({
|
|
| 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;
|
|
@@ -32,19 +37,25 @@ const GoogleAd: React.FC<GoogleAdProps> = ({
|
|
| 32 |
} catch (e) {
|
| 33 |
console.error("AdSense error:", e);
|
| 34 |
}
|
| 35 |
-
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
return (
|
| 38 |
<div className={`w-full flex justify-center bg-slate-900/50 rounded overflow-hidden min-h-[100px] ${className}`}>
|
| 39 |
-
{/*
|
| 40 |
-
NOTE: Replace data-ad-slot with your actual Ad Unit ID from AdSense Dashboard.
|
| 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={
|
| 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.
|
|
@@ -53,4 +64,4 @@ const GoogleAd: React.FC<GoogleAdProps> = ({
|
|
| 53 |
);
|
| 54 |
};
|
| 55 |
|
| 56 |
-
export default GoogleAd;
|
|
|
|
|
|
|
| 1 |
import React, { useEffect, useRef } from 'react';
|
| 2 |
|
| 3 |
interface GoogleAdProps {
|
|
|
|
| 14 |
const adRef = useRef<HTMLModElement>(null);
|
| 15 |
const hasRequestedAd = useRef(false);
|
| 16 |
|
| 17 |
+
// Use the provided ID or the placeholder
|
| 18 |
+
const effectiveSlotId = slotId || "1234567890";
|
| 19 |
+
const isPlaceholder = effectiveSlotId === "1234567890";
|
| 20 |
+
|
| 21 |
useEffect(() => {
|
| 22 |
+
// If it's a placeholder, do not attempt to fetch an ad (prevents 400 errors)
|
| 23 |
+
if (isPlaceholder) return;
|
| 24 |
+
|
| 25 |
// Prevent double execution in React Strict Mode
|
| 26 |
if (hasRequestedAd.current) return;
|
| 27 |
|
|
|
|
| 30 |
const adsbygoogle = window.adsbygoogle || [];
|
| 31 |
|
| 32 |
// Safety Check: Only push ad if the element is actually visible in the DOM
|
|
|
|
| 33 |
if (adRef.current && adRef.current.offsetWidth > 0 && adRef.current.innerHTML === "") {
|
| 34 |
adsbygoogle.push({});
|
| 35 |
hasRequestedAd.current = true;
|
|
|
|
| 37 |
} catch (e) {
|
| 38 |
console.error("AdSense error:", e);
|
| 39 |
}
|
| 40 |
+
}, [isPlaceholder]);
|
| 41 |
+
|
| 42 |
+
// Render a placeholder box if no valid Slot ID is provided
|
| 43 |
+
if (isPlaceholder) {
|
| 44 |
+
return (
|
| 45 |
+
<div className={`w-full flex flex-col items-center justify-center bg-slate-900/50 rounded border border-slate-800 border-dashed min-h-[100px] p-4 text-center ${className}`}>
|
| 46 |
+
<span className="text-xs text-slate-500 font-medium">Ad Space</span>
|
| 47 |
+
<span className="text-[10px] text-slate-600">Configure Slot ID in Code</span>
|
| 48 |
+
</div>
|
| 49 |
+
);
|
| 50 |
+
}
|
| 51 |
|
| 52 |
return (
|
| 53 |
<div className={`w-full flex justify-center bg-slate-900/50 rounded overflow-hidden min-h-[100px] ${className}`}>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
<ins className="adsbygoogle"
|
| 55 |
ref={adRef}
|
| 56 |
style={{ display: 'block', width: '100%' }}
|
| 57 |
data-ad-client="ca-pub-8848704570658587"
|
| 58 |
+
data-ad-slot={effectiveSlotId}
|
| 59 |
data-ad-format={format}
|
| 60 |
// CRITICAL: Set to false to prevent the ad from expanding beyond the sidebar/panel width
|
| 61 |
// and overlaying the entire application, which blocks clicks on desktop.
|
|
|
|
| 64 |
);
|
| 65 |
};
|
| 66 |
|
| 67 |
+
export default GoogleAd;
|