/** * One IndexNow POST for the URLs in the sitemap. Run after the prod files are published. */ const fs = require('fs'); const path = require('path'); const { INDEXNOW_KEY, indexNowBody, sitemapEntries } = require('./sitemap'); async function pingIndexNow(fetchImpl = fetch, urls) { const keyPath = path.join(__dirname, '..', 'public', `${INDEXNOW_KEY}.txt`); const key = fs.readFileSync(keyPath, 'utf8').trim(); if (key !== INDEXNOW_KEY) { throw new Error(`IndexNow key file does not match ${INDEXNOW_KEY}`); } const urlList = urls || sitemapEntries().map((entry) => entry.loc); const body = indexNowBody(key, urlList); const response = await fetchImpl('https://api.indexnow.org/indexnow', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); const text = await response.text(); if (response.status !== 200 && response.status !== 202) { throw new Error(`IndexNow ${response.status}: ${text}`); } return { status: response.status, body }; } async function main() { const result = await pingIndexNow(); process.stdout.write(`IndexNow ${result.status}\n`); } module.exports = { pingIndexNow }; if (require.main === module) { main().catch((error) => { process.stderr.write(`${error.message}\n`); process.exit(1); }); }