Skip to content

Commit 3846e93

Browse files
committed
Handle adblock properly
1 parent 289929b commit 3846e93

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"jest-environment-jsdom-fourteen": "0.1.0",
9191
"jest-resolve": "24.9.0",
9292
"jest-watch-typeahead": "0.4.2",
93+
"just-detect-adblock": "^1.1.0",
9394
"katex": "^0.11.1",
9495
"koa": "^2.11.0",
9596
"koa-better-http-proxy": "^0.2.4",

src/components/common/AdFeed.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ import React, { useEffect, useState } from 'react';
22
import styled from 'styled-components';
33
import { mediaQuery } from '../../lib/styles/media';
44

5+
declare global {
6+
interface Window {
7+
adsbygoogle: { [key: string]: unknown }[];
8+
}
9+
}
10+
511
function AdFeed() {
6-
const [adBlocked, setAdBlocked] = useState(false);
712
useEffect(() => {
8-
if (((window as any)?.adsbygoogle || [])?.push) {
9-
(window as any).adsbygoogle = ((window as any).adsbygoogle || []).push(
10-
{},
11-
);
12-
} else {
13-
setAdBlocked(true);
14-
}
13+
(window.adsbygoogle = window.adsbygoogle || []).push({});
1514
}, []);
1615

17-
if (adBlocked) return null;
18-
1916
return (
2017
<Block>
2118
<ins

src/components/common/PostCardGrid.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useMemo, useState, useEffect } from 'react';
22
import styled from 'styled-components';
33
import PostCard, { PostCardSkeleton } from './PostCard';
44
import { PartialPost } from '../../lib/graphql/post';
55
import { mediaQuery } from '../../lib/styles/media';
66
import AdFeed from './AdFeed';
7+
import { detectAnyAdblocker } from 'just-detect-adblock';
78

89
export type PostCardGridProps = {
910
posts: PartialPost[];
@@ -12,14 +13,18 @@ export type PostCardGridProps = {
1213
};
1314

1415
function PostCardGrid({ posts, loading, forHome }: PostCardGridProps) {
15-
// const [adVisible, setAdVisible] = useState(true);
16-
// useEffect(() => {
17-
// if (localStorage.getItem('SHOW_AD') === 'true') {
18-
// setAdVisible(true);
19-
// }
20-
// }, []);
16+
const [adBlocked, setAdBlocked] = useState(false);
17+
18+
useEffect(() => {
19+
detectAnyAdblocker().then((detected: boolean) => {
20+
if (detected) {
21+
setAdBlocked(true);
22+
}
23+
});
24+
}, []);
2125

2226
const postsWithAds = useMemo(() => {
27+
if (adBlocked) return posts;
2328
if (!forHome) return posts;
2429
if (posts.length === 0) return posts;
2530
const cloned: (PartialPost | undefined)[] = [...posts];
@@ -37,7 +42,7 @@ function PostCardGrid({ posts, loading, forHome }: PostCardGridProps) {
3742
cloned.splice(62, 0, undefined);
3843
}
3944
return cloned;
40-
}, [posts, forHome]);
45+
}, [posts, forHome, adBlocked]);
4146

4247
return (
4348
<Block>

src/types/missingTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ declare module 'rehype-stringify';
1212
declare module 'rehype-raw';
1313
declare module 'unist-util-visit';
1414
declare module 'strip-markdown';
15+
declare module 'just-detect-adblock';

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7879,6 +7879,11 @@ jsx-ast-utils@^2.2.1:
78797879
array-includes "^3.0.3"
78807880
object.assign "^4.1.0"
78817881

7882+
just-detect-adblock@^1.1.0:
7883+
version "1.1.0"
7884+
resolved "https://registry.yarnpkg.com/just-detect-adblock/-/just-detect-adblock-1.1.0.tgz#01910678247394b8d8b146afc869055eda2edd2e"
7885+
integrity sha512-xtgnwBBZaLtKspGo6jDX/H0FDsHrn41mQVWhNHge7pZe6Nj2gU2izfC09O0rPU/i97iMcJFVjbecFiTAvmNhLQ==
7886+
78827887
katex@^0.11.0, katex@^0.11.1:
78837888
version "0.11.1"
78847889
resolved "https://registry.yarnpkg.com/katex/-/katex-0.11.1.tgz#df30ca40c565c9df01a466a00d53e079e84ffaa2"

0 commit comments

Comments
 (0)