|
// `scripts/index-now.mjs` |
|
/* eslint-disable no-console */ |
|
import fetch from 'cross-fetch'; |
|
import convert from 'xml-js'; |
|
|
|
const host = 'www.yourdomain.com'; |
|
const key = '******'; |
|
const sitemapUrl = `https://${host}/sitemap/sitemap-0.xml`; |
|
const keyLocation = `https://${host}/${key}.txt`; |
|
|
|
const readRemoteSitemapXml = async (url) => { |
|
console.info('Reading remote sitemap', url); |
|
const res = await fetch(url); |
|
if (res.status >= 400) throw new Error('Bad response from server'); |
|
return res.text(); |
|
}; |
|
|
|
/** |
|
* Parse a sitemap.xml file & get an array of URLs |
|
* @param {*} xml |
|
* @returns |
|
*/ |
|
const getUrlListFromXmlText = (xml) => { |
|
const result = convert.xml2json(xml, { compact: true, spaces: 4 }); |
|
const siteMapJson = JSON.parse(result); |
|
|
|
// get an array of just URLs |
|
// eslint-disable-next-line no-underscore-dangle |
|
return siteMapJson.urlset.url.map((item) => item.loc._text); |
|
}; |
|
|
|
/** |
|
* POST https://www.bing.com/indexnow HTTP/1.1 |
|
* Content-Type: application/json; charset=utf-8 |
|
* { |
|
* "host": "www.example.org", |
|
* "key": "e6677129dd464fc4a5439591f2ab2eb7", |
|
* "keyLocation": "https://www.example.org/e6677129dd464fc4a5439591f2ab2eb7.txt", |
|
* "urlList": [ |
|
* "https://www.example.org/url1", |
|
* "https://www.example.org/folder/url2", |
|
* "https://www.example.org/url3" |
|
* ] |
|
* } |
|
*/ |
|
const submitToBing = (payload) => { |
|
const url = 'https://www.bing.com/indexnow'; |
|
fetch(url, { |
|
method: 'post', |
|
headers: { |
|
Accept: 'application/json', |
|
'Content-Type': 'application/json', |
|
}, |
|
body: JSON.stringify(payload), |
|
}).then((response) => { |
|
console.log( |
|
`Submitted to IndexNow: ${url} • HTTP Response Code: ${response.status}` |
|
); |
|
}); |
|
}; |
|
|
|
(async () => { |
|
try { |
|
const xmlText = await readRemoteSitemapXml(sitemapUrl); |
|
|
|
// get an array of just URLs from the sitemap text |
|
const urlList = getUrlListFromXmlText(xmlText); |
|
|
|
console.log('Number of URLs: ', urlList.length); |
|
|
|
// the payload in IndexNow format |
|
const payload = { |
|
host, |
|
key, |
|
keyLocation, |
|
urlList, |
|
}; |
|
|
|
submitToBing(payload); |
|
} catch (err) { |
|
console.error(err); |
|
} |
|
})(); |