Skip to content

fix: add mutation observer to hide ads with unfilled status #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion web/src/modules/shared/components/client/ads/AdSlots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const AdTemplate = ({
showCloseButton?: boolean;
}) => {
const pubId = useAdSenseClient();

const [isHidden, setIsHidden] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -85,6 +84,21 @@ const AdTemplate = ({
{!isHidden && (
<>
<ins
ref={(el) => {
// detect the ad has data-ad-status="unfilled" data
if (el) {
const observer = new MutationObserver(() => {
if (el.getAttribute('data-ad-status') === 'unfilled') {
setIsHidden(true);
Copy link
Preview

Copilot AI Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutation observer is not disconnected after changing the state, which might lead to a memory leak. Consider disconnecting the observer after it has fulfilled its purpose.

Copilot uses AI. Check for mistakes.

}
});

observer.observe(el, {
attributes: true,
attributeFilter: ['data-ad-status'],
});
}
}}
className={cn('adsbygoogle', isHidden ? 'hidden' : '')}
style={{
display: 'block',
Expand Down