Skip to content

Commit a944de4

Browse files
committed
fix: refactor ads
1 parent b40de61 commit a944de4

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
"use client";
2-
31
import Script from "next/script";
42
import {
53
Card,
64
CardContent,
75
CardHeader,
86
CardTitle,
97
} from "@workspace/ui/components/card";
8+
import { AdsenseAd } from "@/components/adsense-ad";
109

1110
export default function HookAside() {
1211
return (
@@ -32,24 +31,7 @@ export default function HookAside() {
3231
</CardContent>
3332
</Card>
3433

35-
<div className="mt-4">
36-
<ins
37-
className="adsbygoogle"
38-
style={{ display: "block" }}
39-
data-ad-client="ca-pub-1640905025052378"
40-
data-ad-slot="9540285659"
41-
data-ad-format="auto"
42-
data-full-width-responsive="true"
43-
/>
44-
45-
<Script
46-
id="adsense-docs-aside"
47-
strategy="afterInteractive"
48-
dangerouslySetInnerHTML={{
49-
__html: "(adsbygoogle = window.adsbygoogle || []).push({});",
50-
}}
51-
/>
52-
</div>
34+
<AdsenseAd adSlot="9540285659" className="mt-4" />
5335
</aside>
5436
);
5537
}

apps/www/app/docs/@aside/page.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
"use client";
2-
31
import Script from "next/script";
42
import {
53
Card,
64
CardHeader,
75
CardTitle,
86
CardContent,
97
} from "@workspace/ui/components/card";
8+
import { AdsenseAd } from "@/components/adsense-ad";
109

1110
export default function DocsAside() {
1211
return (
@@ -32,24 +31,7 @@ export default function DocsAside() {
3231
</CardContent>
3332
</Card>
3433

35-
<div className="mt-4">
36-
<ins
37-
className="adsbygoogle"
38-
style={{ display: "block" }}
39-
data-ad-client="ca-pub-1640905025052378"
40-
data-ad-slot="9540285659"
41-
data-ad-format="auto"
42-
data-full-width-responsive="true"
43-
/>
44-
45-
<Script
46-
id="adsense-docs-aside"
47-
strategy="afterInteractive"
48-
dangerouslySetInnerHTML={{
49-
__html: "(adsbygoogle = window.adsbygoogle || []).push({});",
50-
}}
51-
/>
52-
</div>
34+
<AdsenseAd adSlot="9540285659" className="mt-4" />
5335
</aside>
5436
);
5537
}

apps/www/components/adsense-ad.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Script from "next/script";
2+
import type { CSSProperties } from "react";
3+
4+
type AdsenseAdProps = {
5+
adSlot: string;
6+
adClient?: string;
7+
adFormat?: string;
8+
fullWidthResponsive?: boolean;
9+
className?: string;
10+
style?: CSSProperties;
11+
scriptId?: string;
12+
};
13+
14+
export function AdsenseAd({
15+
adSlot,
16+
adClient = process.env.NEXT_PUBLIC_AD_CLIENT_ID,
17+
adFormat = "auto",
18+
fullWidthResponsive = true,
19+
className,
20+
style = { display: "block" },
21+
scriptId,
22+
}: AdsenseAdProps) {
23+
// If client ID is missing, avoid rendering to prevent runtime errors
24+
if (!adClient) {
25+
return null;
26+
}
27+
28+
return (
29+
<div className={className}>
30+
<ins
31+
className="adsbygoogle"
32+
style={style}
33+
data-ad-client={adClient}
34+
data-ad-slot={adSlot}
35+
data-ad-format={adFormat}
36+
data-full-width-responsive={fullWidthResponsive ? "true" : "false"}
37+
/>
38+
39+
<Script
40+
id={scriptId ?? `adsense-${adSlot}`}
41+
strategy="afterInteractive"
42+
dangerouslySetInnerHTML={{
43+
__html: "(adsbygoogle = window.adsbygoogle || []).push({});",
44+
}}
45+
/>
46+
</div>
47+
);
48+
}

0 commit comments

Comments
 (0)