Your sitemap is lying to Google — a 30-minute audit
DEV Community

Your sitemap is lying to Google - a 30-minute audit

A sitemap is the one file on your site that is allowed to be wrong for months. Nothing breaks. No user sees it. The build passes. It just quietly tells every crawler a story about your site that stopped being true in March.

I audited one last week: 148 URLs, three duplicates, two pages that return 404, one that redirects to itself, and a single-page app answering 200 for every path anyone types. The site looked fine in a browser, which is exactly the problem. A crawler does not see what you see.

This is the 30-minute version. Four checks, one script, and the fixes that are actually worth making.

Why sitemaps drift

Almost every sitemap I have seen is generated once and then edited by hand. That works until the first time someone does one of these:

  • Adds a page to the router but not to the sitemap.
  • Renames a slug and leaves the old URL in the file.
  • Adds a redirect for a retired page but not the redirect's source to the sitemap.
  • Copies an entry to add a new one and forgets to change the <loc>.
  • Ships a prerender step that writes both /guides/thing and /guides/thing.html.

None of those are dramatic. All of them teach a crawler that your sitemap is approximate, which is the opposite of what it is for.

Check 1: is the file well formed, and is each URL unique?

This takes a minute, and half the problems are here.

import { readFileSync } from "node:fs";
const xml = readFileSync("public/sitemap.xml", "utf8");
const locs = [...xml.matchAll(/<loc>([^<]+)<\/loc>/g)].map((m) => m[1]);
const seen = new Map();
for (const loc of locs) {
  seen.set(loc, (seen.get(loc) ?? 0) + 1);
}
const duplicates = [...seen].filter(([, n]) => n > 1);
const notHttps = locs.filter((l) => !l.startsWith("https://"));
const withQuery = locs.filter((l) => l.includes("?"));
const withHash = locs.filter((l) => l.includes("#"));
console.log({ total: locs.length, duplicates, notHttps, withQuery, withHash });

Rules:

  • One <loc> per URL. A duplicate is a signal that something is generating entries twice.
  • No query strings. ?utm_source= in a sitemap is the same page telling you it does not know its own canonical URL.
  • No fragments.
  • HTTPS only, one host, no www unless it is genuinely canonical.
  • Consistent trailing slashes. Pick one form per route type and keep it.

A malformed block is worse than a missing URL. If the XML is invalid, the whole file is discarded, and you will never get a warning about it in the browser.

Check 2: does every <loc> return 200?

This is the check people skip, and it is the one that finds the real rot.

const results = [];
for (const url of locs) {
  const res = await fetch(url, { redirect: "manual", headers: { "user-agent": "sitemap-audit" } });
  results.push({ url, status: res.status, location: res.headers.get("location") });
}
const broken = results.filter((r) => r.status !== 200);
console.table(broken);

What to do with each outcome:

Status Meaning Fix
200 Fine - check the canonical next -
301 / 302 In the sitemap but moved Replace with the destination, or drop it
404 Dead entry Remove it
410 Deliberately gone Remove it
403 Blocked for this fetcher Check your bot rules - a citation with a 403 is a dead end
200, but page says "not found" Soft 404 Fix the route, not the sitemap

That last row is the one that costs real money on a single-page app, so it gets its own section.

Check 3: does the canonical agree with the sitemap?

The sitemap says "this is the URL". The page says "this is the URL". If those disagree, the sitemap loses and the page's canonical wins - and you have spent an afternoon on a file that is being ignored.

async function canonicalOf(url) {
  const html = await fetch(url).then((r) => r.text());
  const match = html.match(/<link[^>]+rel="canonical"[^>]+href="([^"]+)"/i) ?? html.match(/<link[^>]+href="([^"]+)"[^>]+rel="canonical"/i);
  return match?.[1] ?? null;
}
const mismatched = [];
for (const url of locs) {
  const canonical = await canonicalOf(url);
  if (canonical && canonical !== url) mismatched.push({ url, canonical });
}
console.table(mismatched);

Common findings:

  • A trailing-slash mismatch between the sitemap and the canonical.
  • Every page's canonical pointing at the homepage, because a default was never overridden.
  • A paginated list where page 2 canonicals to page 1, and page 2 is in the sitemap.
  • A staging host inside the canonical.

Check 4: is anything missing?

A sitemap that lists 148 URLs while the router defines 160 has an opinion problem, not a bug. Two ways to find the gap:

  • Diff against the router. Enumerate static routes from your route table, expand dynamic ones from the data, and compare with the <loc> list. Fail the build on a difference.
  • Diff against the HTML sitemap. If you have a human-readable sitemap page, it should be a superset of the XML file for public pages. Two lists that disagree is how you find the page someone added in a hurry.

The search-engine half of this is easy: submit the sitemap, then read the coverage report. The report tells you which URLs were discovered, which were excluded, and which were fetched and rejected. The gap between "submitted" and "indexed" is where the useful information lives.

The soft-404 trap on single-page apps

This is the one that survives every checklist, because every individual piece looks correct. A client-side router with a catch-all route will answer 200 for /anything-at-all. From a crawler's point of view, you have an infinite site where every made-up URL is a valid page that renders a spinner and then "Not found".

Symptoms:

  • Crawl budget disappearing into URLs nobody linked.
  • A search result that says "Not found".
  • Duplicate content warnings between real pages and their near-misses.
  • Sitemap URLs that 200, render correctly for a human, and look empty to a bot.

Fixes, in order of how much I like them:

  1. Prerender or server-render public routes. Ship real HTML for real URLs. If the fetch does not run JavaScript, the page still has to make sense.
  2. Return a real 404 status for unknown paths. On a static host, that is a 404.html and a host rule, not a client-side redirect.
  3. Do not list routes that only exist after login. The sitemap is for public pages. A login screen that 200s is not a page, it is a door.
  4. Block the noise in robots.txt. Not a substitute for the above, but it stops the bleeding while you fix it.

Retiring a URL without breaking the web

When a page is renamed, three things have to happen, and only the first one is obvious:

  1. Add a redirect from the old URL to the new one.
  2. Remove the old URL from the sitemap, add the new one.
  3. Point internal links at the new URL, so the redirect is a safety net rather than the route.

If the page is genuinely gone, return 410 and remove it from the sitemap. A redirect to the homepage for a deleted article is a worse experience than an honest "this was removed", and search engines treat it as a soft 404 anyway.

For a static site, the redirect is usually a host rule or a tiny HTML page with a <meta http-equiv="refresh">. I have started adding them as explicit routes in the app, next to the real ones:

<Route path="/guides/old-slug" element={<Navigate to="/guides/new-slug" replace />} />

It is ugly and it survives framework changes, which is more than I can say for most redirect configurations.

Automate the whole thing

A 30-minute audit is useful once. A script in CI is useful forever.

// scripts/sitemap-check.mjs
const failures = [];
if (duplicates.length) failures.push(`${duplicates.length} duplicate <loc> entries`);
if (broken.length) failures.push(`${broken.length} URLs not returning 200`);
if (mismatched.length) failures.push(`${mismatched.length} canonical mismatches`);
if (locs.length === 0) failures.push("sitemap is empty");
if (failures.length) {
  console.error(failures.join("\n"));
  process.exit(1);
}

Run it after the build and before the deploy. A sitemap that fails the build is a sitemap nobody has to remember.

What a sitemap is actually for

Not ranking. Google has said for years that a sitemap is a hint, and that a clean internal link structure matters more. What a sitemap does is tell a crawler which URLs exist, when they changed, and which ones matter - cheaply, in one request.

That makes it a contract, not a marketing asset:

  • Every URL in the file exists and returns 200.
  • Every URL in the file is canonical.
  • Every public URL that matters is in the file.
  • The file is generated from the same data as the site.

Four statements. Each one is testable in a few lines. When all four are true, you can forget about the file for a year, which is the actual goal.

Elsewhere in this series

Legible to Machines is about making a product understandable to software that is not a browser:

  • Your next customer is a retrieval step: a developer's guide to GEO
  • Your sitemap is lying to Google - a 30-minute audit
  • Docs that get quoted: AEO without the snake oil

If you want to see the file this audit came from, Revercue's sitemap is public, and so is the HTML sitemap built from the same data. Revercue keeps proposals, contracts and invoices on one client thread.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.