A sitemap is a promise to Google: "these are the pages I want indexed." The trouble is that sitemaps are usually generated, so they quietly fill up with pages that break that promise. A page set to noindex. A product that now redirects. A blog post whose canonical points somewhere else.
Search Console will tell you about these eventually, under Page indexing, a few days or weeks later. I'd rather know on launch day, so I keep a small script that walks the sitemap and checks every URL itself.
It needs Node 18 or newer (for built-in fetch) and no packages.
What it checks
For every URL in the sitemap:
- Status. Anything that isn't a 200. Redirects are reported with their target, because a sitemap should list final URLs, not ones that redirect.
-
noindex. In a
<meta name="robots">tag or in anX-Robots-Tagresponse header. The header one is easy to miss because you can't see it in the page source. - Canonical. Missing, or pointing to a different URL than the one in the sitemap.
It also follows sitemap index files, so it works on Shopify, WordPress (wp-sitemap.xml) and Webflow sitemaps without changes.
The script
Save this as check-sitemap.mjs:
// check-sitemap.mjs: node check-sitemap.mjs https://example.com/sitemap.xml
const sitemapUrl = process.argv[2];
if (!sitemapUrl) { console.error('Usage: node check-sitemap.mjs <sitemap url>'); process.exit(1); }
const UA = { 'user-agent': 'sitemap-check/1.0' };
const locs = (xml) => [...xml.matchAll(/<loc>\s*([^<\s]+)\s*<\/loc>/g)].map((m) => m[1]);
async function collect(url) {
const xml = await (await fetch(url, { headers: UA })).text();
// A sitemap index lists other sitemaps: read each one
if (xml.includes('<sitemapindex')) return (await Promise.all(locs(xml).map(collect))).flat();
return locs(xml);
}
function inspect(html, finalUrl, res) {
const problems = [];
const robotsMeta = html.match(/<meta[^>]+name=["']robots["'][^>]*>/i)?.[0] ?? '';
const robotsHeader = res.headers.get('x-robots-tag') ?? '';
if (/noindex/i.test(robotsMeta + robotsHeader)) problems.push('noindex');
const canonical = html.match(/<link[^>]+rel=["']canonical["'][^>]*>/i)?.[0]?.match(/href=["']([^"']+)/i)?.[1];
if (!canonical) problems.push('no canonical');
else if (new URL(canonical, finalUrl).href !== finalUrl) problems.push(`canonical -> ${canonical}`);
return problems;
}
const urls = [...new Set(await collect(sitemapUrl))];
console.log(`${urls.length} URLs in the sitemap\n`);
let bad = 0;
for (const url of urls) {
const res = await fetch(url, { headers: UA, redirect: 'manual' });
const problems = [];
if (res.status >= 300 && res.status < 400) problems.push(`${res.status} -> ${res.headers.get('location')}`);
else if (res.status !== 200) problems.push(`status ${res.status}`);
else problems.push(...inspect(await res.text(), url, res));
if (problems.length) { bad++; console.log(`${url}\n ${problems.join('\n ')}`); }
await new Promise((r) => setTimeout(r, 300)); // be gentle with the server
}
console.log(`\n${bad} of ${urls.length} URLs need a look`);
Run it:
node check-sitemap.mjs https://yoursite.com/sitemap.xml
What the output looks like
I set up a small local test site with one of each problem. This is what came back:
7 URLs in the sitemap
http://localhost:8765/noidx
noindex
http://localhost:8765/canon
canonical -> http://localhost:8765/ok
http://localhost:8765/old
301 -> /ok
http://localhost:8765/gone
status 404
http://localhost:8765/nocanon
no canonical
http://localhost:8765/hdr
noindex
6 of 7 URLs need a look
The last one, /hdr, has a clean page source. The noindex only lives in the response header. That's the case that wastes the most time when you're checking by hand.
A few decisions worth explaining
redirect: 'manual'. By default fetch follows redirects and hands you the final page with a 200, which hides the exact problem we're looking for. With manual, Node gives you the 3xx status and the Location header instead.
Canonical compared as an absolute URL. new URL(canonical, finalUrl) resolves relative canonicals like /about before comparing, so a relative tag that points to itself doesn't get flagged. It does flag trailing-slash mismatches (/about vs /about/), and I've left that on purpose: on most sites that's a real inconsistency worth fixing.
Sequential with a 300 ms pause. It's slower than firing everything at once, but it won't trip rate limits on shared hosting or a store platform. On a 200-page site it finishes in a couple of minutes.
Regex, not an HTML parser. It keeps the script dependency-free. It's fine for robots and canonical tags in the <head>, which are simple, but if your pages do something unusual (canonical injected by JavaScript, for example) this won't see it, because it reads the raw HTML like most crawlers do on the first pass.
When I run it
- Right after launching a new site or a migration, together with a redirect check for the old URLs.
- After installing an SEO plugin or app, since they're the usual source of surprise noindex tags.
- Once a month on sites I look after, because sitemaps drift as products and posts come and go.
When I ran it on our own site's sitemap this morning, all 197 URLs came back clean, which was a relief, since I'd have had some explaining to do.
If you'd rather get a full report than run scripts, we do a free SEO audit that covers this and more: https://rizehub.ph/services/seo-audit
Top comments (3)
Checking X-Robots-Tag alongside the HTML head is the kind of detail that catches costly false assumptions. I would emit machine-readable JSON as well as console output, with the final URL and normalized canonical, so the audit can feed a release gate or a tracked remediation queue.
Good call. A --json flag that prints the sitemap URL, status, final URL and resolved canonical would be a small change, and it makes runs easy to diff. I kept it to console output so the script stays short, but that's the first thing I'd add if it were feeding a CI check.
search console tells you weeks later. the script tells you on launch day.