Skip to content

Commit 8f209e5

Browse files
committed
Added ads.txt Generation
1 parent 8df217e commit 8f209e5

File tree

5 files changed

+113
-193
lines changed

5 files changed

+113
-193
lines changed

README.md

+50-27
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Give me a ⭐ if you like it.
1616
- Support TypeScript
1717
- Zero Dependencies
1818
- Theoretically support all AdSense AD types (see [🎨 Create a custom layout](#🎨-create-a-custom-layout) for more details)
19+
- Create `ads.txt` automatically (see [Initialization / Verification](#initialization--verification-🍀) for more details
1920

2021
## 📑 Table of Contents
2122

@@ -24,7 +25,7 @@ Give me a ⭐ if you like it.
2425
- [📦 Requirements](#📦-requirements)
2526
- [🚀 Getting Started](#🚀-getting-started)
2627
- [Installation](#installation)
27-
- [Initialization](#initialization)
28+
- [Initialization / Verification](#initialization--verification-🍀)
2829
- [Usage](#usage)
2930
- [Auto Ads](#auto-ads)
3031
- [Manual Ads](#manual-ads)
@@ -43,14 +44,15 @@ Give me a ⭐ if you like it.
4344
## 👾 Why next-google-adsense?
4445

4546
| | next-google-adsense (this) | [nextjs-google-adsense](https://github.com/btk/nextjs-google-adsense/) |
46-
| -------------------------- | -------------------------- | --------------------- |
47-
| TypeScript |||
48-
| Support Auto Ads |||
49-
| Support Display Ad |||
50-
| Support In-feed Ad |||
51-
| Support In-article Ad |||
52-
| Support Matched Content Ad |||
53-
| Multiple ADs on one page || ⚠️\*1 |
47+
| -------------------------- | -------------------------- | ---------------------------------------------------------------------- |
48+
| TypeScript |||
49+
| Support Auto Ads |||
50+
| Support Display Ad |||
51+
| Support In-feed Ad |||
52+
| Support In-article Ad |||
53+
| Support Matched Content Ad |||
54+
| Dynamic `ads.txt` |||
55+
| Multiple ADs on one page || ⚠️\*1 |
5456

5557
\*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.
5658

@@ -69,29 +71,50 @@ npm install next-google-adsense
6971

7072
Visit the [npm](https://www.npmjs.com/package/next-google-adsense) page.
7173

72-
### Initialization 🍀
74+
### Initialization / Verification 🍀
7375

74-
```typescript
75-
// pages/_app.tsx
76+
There are two ways to verify your site (of course you can implement both):
7677

77-
// import the module
78-
import { GoogleAdSense } from "next-google-adsense";
78+
1. AdSense code snippet
7979

80-
const App = ({ Component, pageProps }) => {
81-
return (
82-
<>
83-
<GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" /> {/* 👈 16 digits */}
84-
{/* or */}
85-
<GoogleAdSense /> {/* if NEXT_PUBLIC_ADSENSE_PUBLISHER_ID is set */}
86-
<Component {...pageProps} />
87-
</>
88-
);
89-
};
80+
```typescript
81+
// pages/_app.tsx
82+
83+
// import the module
84+
import { GoogleAdSense } from "next-google-adsense";
85+
86+
const App = ({ Component, pageProps }) => {
87+
return (
88+
<>
89+
<GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" /> {/* 👈 16 digits */}
90+
{/* or */}
91+
<GoogleAdSense /> {/* if NEXT_PUBLIC_ADSENSE_PUBLISHER_ID is set */}
92+
<Component {...pageProps} />
93+
</>
94+
);
95+
};
96+
97+
export default App;
98+
```
99+
You can also add the `publisherId` as environment variable as `NEXT_PUBLIC_ADSENSE_PUBLISHER_ID`. The environment variable will override the prop if both are set.
100+
101+
2. Ads.txt snippet
102+
```json
103+
// package.json
104+
105+
// ...
106+
"scripts": {
107+
"build": "next build && create-ads-txt", // 👈 if you want to create ads.txt automatically, recommended
108+
"create-ads-txt": "create-ads-txt" // 👈 if you want to create ads.txt manually
109+
},
110+
// ...
111+
```
112+
113+
> ⚠️ Your old `ads.txt` will be overwritten during the generation process.
114+
115+
You must set `NEXT_PUBLIC_ADSENSE_PUBLISHER_ID` as environment variable. The environment variable will be used to generate the `ads.txt`.
90116

91-
export default App;
92-
```
93117

94-
You can also add the `publisherId` as environment variable as `NEXT_PUBLIC_ADSENSE_PUBLISHER_ID`. The environment variable will override the prop if both are set.
95118

96119
### Usage 🎉
97120

bin/cli.mjs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
import envLoader from "@next/env";
4+
import { promises as fs } from "fs";
5+
import path from "path";
6+
import { isPublisherId } from "../dist/utils.js";
7+
8+
const publicPath = path.join(process.cwd(), "public");
9+
const adsTxtPath = path.join(publicPath, "ads.txt");
10+
11+
const env = envLoader.loadEnvConfig(process.cwd()).combinedEnv;
12+
const adsTxtContent = `google.com, ${env.NEXT_PUBLIC_GOOGLE_ADSENSE_PUBLISHER_ID}, DIRECT, f08c47fec0942fa0`;
13+
14+
// create "ads.txt" file to "public" folder
15+
// if public folder not exist, create it
16+
const createAdsTxt = async () => {
17+
if (!isPublisherId(env.NEXT_PUBLIC_GOOGLE_ADSENSE_PUBLISHER_ID)) {
18+
throw new Error(
19+
`Invalid publisher ID: ${env.NEXT_PUBLIC_GOOGLE_ADSENSE_PUBLISHER_ID}`
20+
);
21+
}
22+
23+
try {
24+
await fs.access(publicPath);
25+
} catch (err) {
26+
await fs.mkdir(publicPath);
27+
}
28+
29+
await fs.writeFile(adsTxtPath, adsTxtContent);
30+
};
31+
32+
createAdsTxt();

package-lock.json

+5-144
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-google-adsense",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Next.js Google AdSense",
55
"main": "dist/index.js",
66
"type": "commonjs",
@@ -38,5 +38,8 @@
3838
"jest": "^29.5.0",
3939
"ts-jest": "^29.0.5",
4040
"typescript": "^5.0.2"
41+
},
42+
"bin": {
43+
"create-ads-txt": "./bin/cli.mjs"
4144
}
4245
}

tsconfig.json

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
{
2-
"compilerOptions": {
3-
"declaration": true,
4-
"strict": true,
5-
"allowJs": true,
6-
"module": "commonjs",
7-
"outDir": "dist",
8-
"target": "es6",
9-
"rootDir": "./src",
10-
"jsx": "react",
11-
"removeComments": true,
12-
"importHelpers": true,
13-
"esModuleInterop": true,
14-
"skipLibCheck": true,
15-
},
16-
"exclude": [
17-
"node_modules",
18-
"dist",
19-
"jest.config.js",
20-
"jest.config.d.ts",
21-
]
22-
}
2+
"compilerOptions": {
3+
"declaration": true,
4+
"strict": true,
5+
"allowJs": true,
6+
"module": "commonjs",
7+
"outDir": "dist",
8+
"target": "es6",
9+
"rootDir": "./src",
10+
"jsx": "react",
11+
"removeComments": true,
12+
"importHelpers": true,
13+
"esModuleInterop": true,
14+
"skipLibCheck": true
15+
},
16+
"exclude": [
17+
"node_modules",
18+
"dist",
19+
"jest.config.js",
20+
"jest.config.d.ts",
21+
"bin"
22+
]
23+
}

0 commit comments

Comments
 (0)