From 65d6db73f9689f8a7fb665724add992ea683b336 Mon Sep 17 00:00:00 2001 From: Simon Thorley Date: Fri, 13 Nov 2026 17:08:47 +0000 Subject: [PATCH] fix(web): show compact fallback until AdSense slot is filled and cap height at 90px --- apps/web/src/components/MonetizationSlot.tsx | 48 +++++++++++++------ .../__tests__/MonetizationSlot.spec.tsx | 25 +++++++--- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/apps/web/src/components/MonetizationSlot.tsx b/apps/web/src/components/MonetizationSlot.tsx index facee6a..d24fc56 100644 --- a/apps/web/src/components/MonetizationSlot.tsx +++ b/apps/web/src/components/MonetizationSlot.tsx @@ -138,7 +138,7 @@ export const AdSenseSlot: React.FC = ({ className = '' }) => : '9700539154'; const [adBlocked, setAdBlocked] = useState(false); - const [adUnfilled, setAdUnfilled] = useState(false); + const [adFilled, setAdFilled] = useState(false); const [, setIsLoaded] = useState(false); const insRef = React.useRef(null); const pushedRef = React.useRef(false); @@ -188,16 +188,22 @@ export const AdSenseSlot: React.FC = ({ className = '' }) => } }, [mode, clientId, slotId]); - // Watch for AdSense marking the slot as unfilled so we don't leave a blank box + // Only reveal the live AdSense container once Google sets data-ad-status="filled" useEffect(() => { const el = insRef.current; if (!el || typeof MutationObserver === 'undefined') return; - const observer = new MutationObserver(() => { - if (el.getAttribute('data-ad-status') === 'unfilled') { - setAdUnfilled(true); + const checkStatus = () => { + const status = el.getAttribute('data-ad-status'); + if (status === 'filled') { + setAdFilled(true); + } else if (status === 'unfilled') { + setAdFilled(false); } - }); + }; + + checkStatus(); + const observer = new MutationObserver(checkStatus); observer.observe(el, { attributes: true, attributeFilter: ['data-ad-status'] }); return () => observer.disconnect(); }, [mode]); @@ -206,7 +212,7 @@ export const AdSenseSlot: React.FC = ({ className = '' }) => return null; } - const showFallbackCreative = mode === 'mock' || adBlocked || adUnfilled; + const showFallbackCreative = mode === 'mock' || adBlocked || !adFilled; return (
= ({ className = '' }) =>
- {showFallbackCreative ? ( - // Compact, low-profile dark-mode creative (used in mock mode or when AdSense is blocked/unfilled on preview domains) + {showFallbackCreative && ( + // Compact, low-profile dark-mode creative shown until AdSense fills an active creative
@@ -256,17 +262,29 @@ export const AdSenseSlot: React.FC = ({ className = '' }) =>
- ) : ( - // Official Google AdSense Default Ad Unit (block container ensures non-zero availableWidth on push) -
+ )} + + {mode !== 'mock' && !adBlocked && ( + // Official Google AdSense slim horizontal unit (collapsed to 0px height until data-ad-status="filled") +
diff --git a/apps/web/src/components/__tests__/MonetizationSlot.spec.tsx b/apps/web/src/components/__tests__/MonetizationSlot.spec.tsx index f98cb24..6e0bbeb 100644 --- a/apps/web/src/components/__tests__/MonetizationSlot.spec.tsx +++ b/apps/web/src/components/__tests__/MonetizationSlot.spec.tsx @@ -39,18 +39,20 @@ describe('Monetization Components', () => { }); describe('AdSenseSlot', () => { - it('renders live Google AdSense unit by default with publisher and slot IDs', () => { + it('renders live Google AdSense unit by default with publisher and slot IDs and shows fallback until filled', () => { const { container } = render(); const slotContainer = screen.getByTestId('monetization-slot-container'); expect(slotContainer).toBeDefined(); expect(screen.getByText(/SPONSOR COMM TRANSMISSION/i)).toBeDefined(); + // Fallback creative is shown while data-ad-status is not yet "filled" + expect(screen.getByText(/Aegis Flight Systems & Precision HOTAS Mounts/i)).toBeDefined(); const ins = container.querySelector('ins.adsbygoogle'); expect(ins).not.toBeNull(); expect(ins?.getAttribute('data-ad-client')).toBe('ca-pub-4414971156573564'); expect(ins?.getAttribute('data-ad-slot')).toBe('9700539154'); - expect(ins?.getAttribute('data-ad-format')).toBe('auto'); - expect(ins?.getAttribute('data-full-width-responsive')).toBe('true'); + expect(ins?.getAttribute('data-ad-format')).toBe('horizontal'); + expect(ins?.getAttribute('data-full-width-responsive')).toBe('false'); }); it('renders mock simulator creative when in mock mode without breaking layout', () => { @@ -105,19 +107,30 @@ describe('Monetization Components', () => { vi.unstubAllEnvs(); }); - it('gracefully renders compact fallback banner when AdSense marks slot as unfilled (e.g. preview domains)', async () => { + it('swaps fallback creative for live AdSense unit only when data-ad-status="filled"', async () => { vi.stubEnv('VITE_MONETIZATION_MODE', 'live'); const { container } = render(); const ins = container.querySelector('ins.adsbygoogle'); expect(ins).not.toBeNull(); + // Before fill (status null), fallback creative is displayed so there is no blank box + expect(screen.getByText(/Aegis Flight Systems & Precision HOTAS Mounts/i)).toBeDefined(); + + act(() => { + ins?.setAttribute('data-ad-status', 'filled'); + }); + + await waitFor(() => { + expect(screen.queryByText(/Aegis Flight Systems & Precision HOTAS Mounts/i)).toBeNull(); + const wrapper = screen.getByTestId('adsense-unit-wrapper'); + expect(wrapper.style.height).toBe('90px'); + }); act(() => { ins?.setAttribute('data-ad-status', 'unfilled'); }); await waitFor(() => { - expect(screen.getByTestId('monetization-slot-container')).toBeDefined(); expect(screen.getByText(/Aegis Flight Systems & Precision HOTAS Mounts/i)).toBeDefined(); }); @@ -134,7 +147,7 @@ describe('Monetization Components', () => { expect(ins).not.toBeNull(); expect(ins?.getAttribute('data-ad-client')).toBe('ca-pub-4414971156573564'); expect(ins?.getAttribute('data-ad-slot')).toBe('9700539154'); - expect(ins?.getAttribute('data-ad-format')).toBe('auto'); + expect(ins?.getAttribute('data-ad-format')).toBe('horizontal'); vi.unstubAllEnvs(); });