-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit-indexnow.js
More file actions
59 lines (51 loc) · 1.54 KB
/
submit-indexnow.js
File metadata and controls
59 lines (51 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
// Submit every URL in sitemap.xml to IndexNow (Bing/Yandex/Seznam, feeds DDG/Yahoo/Ecosia).
// Run: node submit-indexnow.js
//
// IndexNow protocol: https://www.indexnow.org/documentation
// Bing endpoint accepts up to 10,000 URLs per call.
const fs = require('fs');
const path = require('path');
const https = require('https');
const HOST = 'boardgaminghub.com';
const KEY = '7ff86ce4c72bbe1aef1ecbf822d8eb6b';
const KEY_LOCATION = `https://${HOST}/${KEY}.txt`;
const ENDPOINT = 'https://api.indexnow.org/IndexNow';
const sitemap = fs.readFileSync(path.join(__dirname, 'sitemap.xml'), 'utf8');
const urlList = [...sitemap.matchAll(/<loc>([^<]+)<\/loc>/g)].map(m => m[1]);
if (urlList.length === 0) {
console.error('No URLs found in sitemap.xml');
process.exit(1);
}
const body = JSON.stringify({
host: HOST,
key: KEY,
keyLocation: KEY_LOCATION,
urlList,
});
const req = https.request(
ENDPOINT,
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(body),
},
},
(res) => {
let data = '';
res.on('data', (c) => (data += c));
res.on('end', () => {
console.log(`Status: ${res.statusCode} ${res.statusMessage}`);
console.log(`Submitted ${urlList.length} URLs`);
if (data) console.log(`Body: ${data}`);
// 200/202 = accepted, 422 = some URLs invalid, 429 = rate limit
});
}
);
req.on('error', (e) => {
console.error('Request failed:', e.message);
process.exit(1);
});
req.write(body);
req.end();