Skip to content

Commit 1f53058

Browse files
authored
Merge pull request #24 from soranoo/beta
fix: refresh ads on pathname change #18
2 parents 21a39e2 + d033c7a commit 1f53058

8 files changed

Lines changed: 321 additions & 43 deletions

File tree

README.md

Lines changed: 115 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Give me a ⭐ if you like it.
1515
- Support SSR (Server-Side Rendering), SSG (Static Site Generation) and CSR (Client-Side Rendering)
1616
- Support TypeScript
1717
- Zero Dependencies
18+
- Dummy Ad Support for Development - Preview ads locally without real AdSense integration
1819
- Theoretically support all AdSense AD types (see [🎨 Create a custom layout](#-create-a-custom-layout) for more details)
1920
- Create `ads.txt` automatically (see [Initialization / Verification](#initialization--verification-) for more details)
2021

@@ -28,10 +29,14 @@ Give me a ⭐ if you like it.
2829
- [Usage](#usage-)
2930
- [Auto Ads](#auto-ads)
3031
- [Manual Ads](#manual-ads)
32+
- [Dummy Ads for Development](#dummy-ads-for-development-)
3133
- [📖 API Reference](#-api-reference)
3234
- [Components](#components)
3335
- [GoogleAdSense](#initializes-the-google-adsense)
3436
- [AdUnit](#manual-ad)
37+
- [Ad Sizes](#ad-sizes)
38+
- [Display Ad Sizes](#display-ad-sizes)
39+
- [In-Article Ad Sizes](#in-article-ad-sizes)
3540
- [🎨 Create a custom layout](#-create-a-custom-layout)
3641
- [How to convert the given html to a custom layout?](#how-to-convert-the-given-html-to-a-custom-layout)
3742
- [🐛 Known Issues](#-known-issues)
@@ -52,6 +57,7 @@ Give me a ⭐ if you like it.
5257
| Support Matched Content Ad |||
5358
| Dynamic `ads.txt` |||
5459
| Multiple ADs on one page || ⚠️\*1 |
60+
| Dummy Ad for Development |||
5561

5662
\*1: According to the their [documentation](https://github.com/btk/nextjs-google-adsense/blob/master/README.md) seems it is ok to use multiple ADs on one page. But I found that it will cause an error.
5763

@@ -125,6 +131,9 @@ If you decide to use Auto Ads, you are good to go. The ads will be automatically
125131

126132
#### Manual Ads
127133

134+
> [!NOTE]\
135+
> Google AdSense does't work in local environment. You need to test it in production or use [Dummy Ads for Development](#dummy-ads-for-development-).
136+
128137
```typescript
129138
import { AdUnit } from "next-google-adsense";
130139

@@ -150,6 +159,54 @@ const Page = () => {
150159
export default Page;
151160
```
152161

162+
#### Dummy Ads for Development 🧪
163+
164+
Perfect for development and testing! Show realistic ad placeholders without needing actual AdSense approval.
165+
166+
```typescript
167+
import { AdUnit, DISPLAY_AD_SIZES, ARTICLE_AD_SIZES } from "next-google-adsense";
168+
169+
const Page = () => {
170+
return (
171+
<>
172+
{/* Using predefined sizes */}
173+
<AdUnit
174+
slotId="1234567890"
175+
layout="display"
176+
dummySize="LEADERBOARD" {/* 728x90 */}
177+
/>
178+
179+
<AdUnit
180+
slotId="1234567890"
181+
layout="in-article"
182+
dummySize="MEDIUM_RECTANGLE" {/* 300x250 */}
183+
/>
184+
185+
{/* Using custom dimensions */}
186+
<AdUnit
187+
slotId="1234567890"
188+
layout="display"
189+
dummySize={{ width: 600, height: 400 }}
190+
/>
191+
192+
{/* Using size objects directly */}
193+
<AdUnit
194+
slotId="1234567890"
195+
layout="display"
196+
dummySize={DISPLAY_AD_SIZES.BANNER} {/* 468x60 */}
197+
/>
198+
199+
<YourContent />
200+
</>
201+
);
202+
};
203+
204+
export default Page;
205+
```
206+
207+
> [!NOTE]\
208+
> Dummy ads only appear when the `dummySize` prop is provided. In production (when `NODE_ENV`/`NEXT_PUBLIC_ENV` is not "development"), real ads will be displayed instead.
209+
153210
## 📖 API Reference
154211

155212
### Components
@@ -167,15 +224,65 @@ export default Page;
167224
#### Manual AD
168225

169226
```typescript
170-
<AdUnit publisherId={string} slotId={string} layout={"display" | "in-article" | "custom"} customLayout={JSX.Element}>
227+
<AdUnit publisherId={string} slotId={string} layout={"display" | "in-article" | "custom"} customLayout={JSX.Element} dummySize={DisplayAdSize | ArticleAdSize | {width: number, height: number}}>
171228
```
172229

173-
| Parameter | Optional | Type | Default | Description |
174-
| ------------ | -------- | ------------------------------------- | --------- | --------------------------------------------------------------------------------------------------- |
175-
| publisherId | Yes | string | n/a | You can skip this parameter if you set the environment variable `NEXT_PUBLIC_ADSENSE_PUBLISHER_ID`. |
176-
| slotId | No | string | n/a | Google AdSense Slot ID. |
177-
| layout | Yes | "display" \| "in-article" \| "custom" | "display" | The layout of the AD. |
178-
| customLayout | Yes | JSX.Element | n/a | The custom layout of the AD. This parameter is required if `layout` is set to "custom". |
230+
| Parameter | Optional | Type | Default | Description |
231+
| ------------ | -------- | --------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------- |
232+
| publisherId | Yes | string | n/a | You can skip this parameter if you set the environment variable `NEXT_PUBLIC_ADSENSE_PUBLISHER_ID`. |
233+
| slotId | No | string | n/a | Google AdSense Slot ID. |
234+
| layout | Yes | "display" \| "in-article" \| "custom" | "display" | The layout of the AD. |
235+
| customLayout | Yes | JSX.Element | n/a | The custom layout of the AD. This parameter is required if `layout` is set to "custom". |
236+
| dummySize | Yes | DisplayAdSize \| ArticleAdSize \| {width: number, height: number} | n/a | Show dummy ad for development. Only appears when this prop is provided. |
237+
238+
### Dummy Ad Sizes
239+
240+
All available ad sizes for the `dummySize` prop:
241+
242+
#### Display Ad Sizes
243+
244+
For `layout="display"` ads:
245+
246+
| Size Name | Key | Dimensions | Best For |
247+
| -------------------- | ---------------------- | ----------- | --------------------------- |
248+
| Leaderboard | `"LEADERBOARD"` | 728 × 90 | Top of page, headers |
249+
| Banner | `"BANNER"` | 468 × 60 | Above content |
250+
| Half Banner | `"HALF_BANNER"` | 234 × 60 | Small horizontal spaces |
251+
| Medium Rectangle | `"MEDIUM_RECTANGLE"` | 300 × 250 | Within content, sidebars |
252+
| Large Rectangle | `"LARGE_RECTANGLE"` | 336 × 280 | Above the fold |
253+
| Vertical Banner | `"VERTICAL_BANNER"` | 120 × 240 | Narrow sidebars |
254+
| Wide Skyscraper | `"WIDE_SKYSCRAPER"` | 160 × 600 | Wide sidebars |
255+
| Skyscraper | `"SKYSCRAPER"` | 120 × 600 | Narrow sidebars |
256+
| Mobile Banner | `"MOBILE_BANNER"` | 320 × 50 | Mobile devices |
257+
| Large Mobile Banner | `"LARGE_MOBILE_BANNER"` | 320 × 100 | Mobile devices, larger |
258+
259+
#### In-Article Ad Sizes
260+
261+
For `layout="in-article"` ads:
262+
263+
| Size Name | Key | Dimensions | Best For |
264+
| ---------------- | -------------------- | ---------- | ---------------------------- |
265+
| Small Square | `"SMALL_SQUARE"` | 200 × 200 | Small content breaks |
266+
| Square | `"SQUARE"` | 250 × 250 | Content breaks |
267+
| Medium Rectangle | `"MEDIUM_RECTANGLE"` | 300 × 250 | Between paragraphs |
268+
| Large Rectangle | `"LARGE_RECTANGLE"` | 336 × 280 | Longer content breaks |
269+
270+
#### Usage Examples
271+
272+
```typescript
273+
import { AdUnit, DISPLAY_AD_SIZES, ARTICLE_AD_SIZES } from "next-google-adsense";
274+
275+
// Using predefined size keys
276+
<AdUnit slotId="123" layout="display" dummySize="LEADERBOARD" />
277+
<AdUnit slotId="456" layout="in-article" dummySize="SQUARE" />
278+
279+
// Using size objects
280+
<AdUnit slotId="789" layout="display" dummySize={DISPLAY_AD_SIZES.BANNER} />
281+
<AdUnit slotId="101" layout="in-article" dummySize={ARTICLE_AD_SIZES.MEDIUM_RECTANGLE} />
282+
283+
// Using custom dimensions
284+
<AdUnit slotId="112" layout="display" dummySize={{ width: 400, height: 300 }} />
285+
```
179286

180287
## 🎨 Create a custom layout
181288

@@ -198,7 +305,7 @@ export const InFeedAd = () => {
198305
};
199306
```
200307

201-
#### How to convert the given html to a custom layout?
308+
### How to convert the given html to a custom layout?
202309

203310
Example (provided by Google AdSense):
204311

package-lock.json

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-google-adsense",
3-
"version": "1.0.14",
3+
"version": "1.1.0-beta.3",
44
"description": "Next.js Google AdSense",
55
"main": "dist/index.js",
66
"type": "commonjs",
@@ -18,6 +18,7 @@
1818
"pub": "npm run test && npm run build && npm publish",
1919
"lint": "biome check",
2020
"fmt": "biome format",
21+
"fmt:fix": "biome format --write",
2122
"pub@beta": "npm run test && npm run build && npm publish --tag beta",
2223
"semantic-release": "semantic-release"
2324
},
@@ -47,5 +48,8 @@
4748
},
4849
"bin": {
4950
"create-ads-txt": "./bin/cli.mjs"
51+
},
52+
"dependencies": {
53+
"assert-never": "^1.4.0"
5054
}
5155
}

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AdLayout.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { DetailedHTMLProps } from "react";
22
// biome-ignore lint/style/useImportType: needed in the <ins> tag
33
import React from "react";
4+
import { DummyAd } from "./DummyAd";
5+
import { isDevelopment } from "./utils";
46

57
export type Layout = "display" | "in-article" | "custom";
68

@@ -11,6 +13,7 @@ interface AdLayoutProps
1113
> {
1214
dataAdClient: string;
1315
dataAdSlot: string;
16+
dummySize?: { width: number; height: number };
1417
}
1518

1619
interface DisplayProps extends AdLayoutProps {
@@ -21,8 +24,13 @@ export const Display = ({
2124
responsive,
2225
dataAdClient,
2326
dataAdSlot,
27+
dummySize,
2428
...props
2529
}: DisplayProps) => {
30+
if (dummySize && isDevelopment()) {
31+
return <DummyAd label="Display Ad" size={dummySize} {...props} />;
32+
}
33+
2634
return (
2735
<ins
2836
className="adsbygoogle"
@@ -41,8 +49,13 @@ interface InArticleProps extends AdLayoutProps {}
4149
export const InArticle = ({
4250
dataAdClient,
4351
dataAdSlot,
52+
dummySize,
4453
...props
4554
}: InArticleProps) => {
55+
if (dummySize && isDevelopment()) {
56+
return <DummyAd label="In-Article Ad" size={dummySize} {...props} />;
57+
}
58+
4659
return (
4760
<ins
4861
className="adsbygoogle"

0 commit comments

Comments
 (0)