Keep AdSense rendering limited to anonymous visitors.

Show guest-only ad units on public pages and skip the AdSense script entirely for signed-in users.
This commit is contained in:
test
2026-09-20 14:49:34 +02:00
parent 37100ced3f
commit 17df4d0ad7
8 changed files with 135 additions and 8 deletions
@@ -0,0 +1,45 @@
import React, { useEffect, useRef } from 'react'
export default function AdSenseUnit({
slot,
client = 'ca-pub-6457864535683080',
format = 'auto',
className = '',
enabled = true,
}) {
const pushed = useRef(false)
useEffect(() => {
if (!enabled || !slot || pushed.current) {
return undefined
}
pushed.current = true
try {
window.adsbygoogle = window.adsbygoogle || []
window.adsbygoogle.push({})
} catch {
// AdSense may be blocked or not yet loaded (consent not granted).
}
return undefined
}, [enabled, slot])
if (!enabled || !slot) {
return null
}
return (
<div className={['flex w-full justify-center', className].filter(Boolean).join(' ')}>
<ins
className="adsbygoogle w-full max-w-3xl"
style={{ display: 'block', textAlign: 'center' }}
data-ad-client={client}
data-ad-slot={slot}
data-ad-format={format}
data-full-width-responsive="true"
/>
</div>
)
}