|
1 |
| -import React, { useEffect } from 'react'; |
| 1 | +import React, { useEffect, useRef } from 'react'; |
2 | 2 | interface Props {
|
3 | 3 | isMobile?: boolean;
|
4 | 4 | }
|
5 | 5 |
|
6 | 6 | function RelatedPostAd({ isMobile }: Props) {
|
| 7 | + const initializedRef = useRef(false); |
| 8 | + const ref = useRef<HTMLDivElement>(null); |
| 9 | + |
7 | 10 | useEffect(() => {
|
| 11 | + if (!initializedRef.current) return; |
8 | 12 | try {
|
9 | 13 | (window.adsbygoogle = window.adsbygoogle || []).push({});
|
10 | 14 | } catch (e) {}
|
11 | 15 | }, [isMobile]);
|
12 | 16 |
|
| 17 | + useEffect(() => { |
| 18 | + const observer = new IntersectionObserver( |
| 19 | + (entries) => { |
| 20 | + entries.forEach((entry) => { |
| 21 | + if (entry.isIntersecting && !initializedRef.current) { |
| 22 | + initializedRef.current = true; |
| 23 | + (window.adsbygoogle = window.adsbygoogle || []).push({}); |
| 24 | + console.log('initialized!'); |
| 25 | + } |
| 26 | + }); |
| 27 | + }, |
| 28 | + { |
| 29 | + rootMargin: '90px', |
| 30 | + threshold: 0, |
| 31 | + }, |
| 32 | + ); |
| 33 | + observer.observe(ref.current!); |
| 34 | + return () => { |
| 35 | + observer.disconnect(); |
| 36 | + }; |
| 37 | + }, []); |
| 38 | + |
13 | 39 | if (isMobile) {
|
14 | 40 | return (
|
| 41 | + <div ref={ref}> |
| 42 | + <ins |
| 43 | + className="adsbygoogle" |
| 44 | + style={{ display: 'block' }} |
| 45 | + data-ad-format="fluid" |
| 46 | + data-ad-layout-key="-gh+5l+k-cu+l4" |
| 47 | + data-ad-client="ca-pub-5574866530496701" |
| 48 | + data-ad-slot="8237449336" |
| 49 | + /> |
| 50 | + </div> |
| 51 | + ); |
| 52 | + } |
| 53 | + return ( |
| 54 | + <div ref={ref}> |
15 | 55 | <ins
|
16 | 56 | className="adsbygoogle"
|
17 | 57 | style={{ display: 'block' }}
|
18 | 58 | data-ad-format="fluid"
|
19 |
| - data-ad-layout-key="-gh+5l+k-cu+l4" |
| 59 | + data-ad-layout-key="-7n+eu-z-1k+6t" |
20 | 60 | data-ad-client="ca-pub-5574866530496701"
|
21 |
| - data-ad-slot="8237449336" |
| 61 | + data-ad-slot="9497725960" |
22 | 62 | />
|
23 |
| - ); |
24 |
| - } |
25 |
| - return ( |
26 |
| - <ins |
27 |
| - className="adsbygoogle" |
28 |
| - style={{ display: 'block' }} |
29 |
| - data-ad-format="fluid" |
30 |
| - data-ad-layout-key="-7n+eu-z-1k+6t" |
31 |
| - data-ad-client="ca-pub-5574866530496701" |
32 |
| - data-ad-slot="9497725960" |
33 |
| - /> |
| 63 | + </div> |
34 | 64 | );
|
35 | 65 | }
|
36 | 66 |
|
|
0 commit comments