railyard/apps/ritgram/web/scripts/indexnow-ping.mjs
caelum0x 2222a45cc3 feat(ritgram): add IndexNow key file + ping script
ritgram.ereboslab.com had no IndexNow key file live, unlike ereboslab.com
and sibling properties, so changed-URL pings for this host were
previously impossible. Serves the shared ereboslab.com IndexNow key from
this app's own public/ (baked into the Next.js build) and adds a ping
script modeled on menivor/ereboslab's existing ones.
2026-11-17 15:00:02 +03:00

115 lines
3.7 KiB
JavaScript

#!/usr/bin/env node
/**
* IndexNow ping for ritgram.ereboslab.com (Bing, Yandex, Seznam, Naver and
* other IndexNow engines share submissions via the unified endpoint).
*
* Reuses the ereboslab.com-wide key (already registered and live on
* ereboslab.com and other *.ereboslab.com properties) -- it just needs the
* matching key file served on THIS host too, which this app's own
* public/<key>.txt now provides after a deploy.
*
* Usage (from the repo root):
* node apps/ritgram/web/scripts/indexnow-ping.mjs # submit
* node apps/ritgram/web/scripts/indexnow-ping.mjs --dry-run # list only
* node apps/ritgram/web/scripts/indexnow-ping.mjs url1 url2 # submit just these
*
* Optional env: INDEXNOW_SITE (default https://ritgram.ereboslab.com),
* INDEXNOW_KEY (default the ereboslab key below). No dependencies; Node 18+.
*/
const SITE = (process.env.INDEXNOW_SITE || "https://ritgram.ereboslab.com").replace(/\/+$/, "");
const KEY = process.env.INDEXNOW_KEY || "098ae1b8127d43e28c3b5d3051dbfdf9";
const ENDPOINT = "https://api.indexnow.org/indexnow";
const BATCH_SIZE = 10_000;
const MAX_SITEMAPS = 50;
const DRY_RUN = process.argv.includes("--dry-run");
const explicitUrls = process.argv.slice(2).filter((a) => !a.startsWith("--"));
function locs(xml) {
return [...xml.matchAll(/<loc>\s*([^<\s]+)\s*<\/loc>/gi)].map((m) =>
m[1].replace(/&amp;/g, "&"),
);
}
async function fetchText(url) {
const res = await fetch(url, { headers: { "user-agent": "ritgram-indexnow-ping" } });
if (!res.ok) throw new Error(`GET ${url} failed: HTTP ${res.status}`);
return res.text();
}
async function collectUrls(rootSitemap) {
const queue = [rootSitemap];
const seenSitemaps = new Set();
const urls = new Set();
while (queue.length > 0 && seenSitemaps.size < MAX_SITEMAPS) {
const sitemapUrl = queue.shift();
if (seenSitemaps.has(sitemapUrl)) continue;
seenSitemaps.add(sitemapUrl);
const xml = await fetchText(sitemapUrl);
const entries = locs(xml);
if (/<sitemapindex[\s>]/i.test(xml)) queue.push(...entries);
else entries.forEach((u) => urls.add(u));
}
return [...urls];
}
async function submit(urlList) {
const host = new URL(SITE).host;
const res = await fetch(ENDPOINT, {
method: "POST",
headers: { "content-type": "application/json; charset=utf-8" },
body: JSON.stringify({
host,
key: KEY,
keyLocation: `${SITE}/${KEY}.txt`,
urlList,
}),
});
const body = await res.text().catch(() => "");
if (res.status !== 200 && res.status !== 202) {
throw new Error(`IndexNow POST failed: HTTP ${res.status} ${body.slice(0, 300)}`);
}
return res.status;
}
async function main() {
const host = new URL(SITE).host;
let urls;
if (explicitUrls.length > 0) {
urls = explicitUrls;
} else {
const all = await collectUrls(`${SITE}/sitemap.xml`);
urls = all.filter((u) => {
try {
return new URL(u).host === host;
} catch {
return false;
}
});
}
console.log(`Submitting ${urls.length} URL(s) on ${host}.`);
if (urls.length === 0) throw new Error("Nothing to submit.");
if (DRY_RUN) {
urls.forEach((u) => console.log(u));
return;
}
const keyRes = await fetch(`${SITE}/${KEY}.txt`);
const keyBody = keyRes.ok ? (await keyRes.text()).trim() : "";
if (keyBody !== KEY) {
throw new Error(`Key file ${SITE}/${KEY}.txt is not live (HTTP ${keyRes.status}); deploy it first.`);
}
for (let i = 0; i < urls.length; i += BATCH_SIZE) {
const batch = urls.slice(i, i + BATCH_SIZE);
const status = await submit(batch);
console.log(`Submitted ${batch.length} URLs: HTTP ${status}`);
}
}
main().catch((err) => {
console.error(err instanceof Error ? err.message : err);
process.exit(1);
});