Skip to content
R.

Catch redirects and noindex in sitemap URLs

Use a small Node validator to catch redirects and noindex headers on sitemap URLs before release, and keep HTML robots tags as a separate parsing step.

About 11 min readComments

A sitemap is not just a list of paths. It is a claim that those URLs are the ones you want search engines to crawl, and Google’s documentation is clear that the sitemap is only a hint. That makes it worth checking the deployed responses for those URLs before release, especially when a route now redirects or a header quietly blocks indexing.

This article shows a small Node.js validator built around an injected fetch function. It uses `redirect: 'manual'` so redirects stay visible, inspects the HTTP status and `X-Robots-Tag`, and keeps HTML robots meta checks as a separate parsing step. The point is to catch a deployment mismatch, not to predict Google’s indexing decision.

What this check is really guarding

A sitemap is a promise about the URLs you want search engines to crawl, not a guarantee that every URL is healthy. Google says a sitemap lists canonical URLs and that submitting it is only a hint. That is why a deployment check can still be useful even when Search Console eventually accepts the file: the sitemap may point at a redirecting path, a page that now emits `noindex`, or a URL that no longer behaves like the canonical page you meant to publish.

The validator in this article works as a deployment gate. It inspects the live response for each listed URL and reports three things that matter at release time: the HTTP status, whether the response redirects, and whether the response carries an `X-Robots-Tag` directive that blocks indexing. That is a narrower job than “is Google indexing this page?” and that narrowness is the point. It gives you a fast answer about what your server is serving right now, before you wait for Google to revisit the page.

The example also keeps the HTML robots meta tag out of the same check on purpose. Google supports both a header and a meta tag for `noindex`, but they are not discovered the same way. A response header can be seen as part of the fetch, while a meta tag requires parsing the page HTML. Treating those as separate checks keeps the deployment gate honest and makes the code easier to reason about.

main.mjs / javascript
/**
 * Checks the response currently served for each sitemap URL. This is a deployment
 * check only: it cannot determine whether Google has crawled or indexed a URL.
 * HTML robots meta tags require fetching and parsing the HTML separately.
 */
export async function checkSitemapUrls(urls, fetchFn) {
  if (!Array.isArray(urls)) throw new TypeError('urls must be an array');
  if (typeof fetchFn !== 'function') throw new TypeError('fetchFn must be a function');

  return Promise.all(urls.map(async (url) => {
    const response = await fetchFn(url, { redirect: 'manual' });
    const xRobotsTag = response.headers?.get?.('x-robots-tag') ?? '';
    const directives = xRobotsTag.toLowerCase().split(/[\s,]+/);
    const problems = [];

    if (response.status >= 300 && response.status < 400) {
      problems.push('redirect');
    } else if (response.status < 200 || response.status >= 300) {
      problems.push(`http-${response.status}`);
    }

    if (directives.includes('noindex') || directives.includes('none')) {
      problems.push('noindex-header');
    }

    return {
      url,
      status: response.status,
      xRobotsTag,
      problems,
      ok: problems.length === 0,
    };
  }));
}

SourcesGoogle Search Central: Build and submit a sitemap (opens a new tab)

Why redirects are a deployment problem, not just a crawl detail

Google’s sitemap guidance says it will attempt to crawl the URLs exactly as listed and that you should include the canonical URLs you want shown in search results. That means a redirect in a sitemap is already a mismatch between intent and deployment state. You may still get indexed eventually, but the sitemap is no longer describing the URL you meant to ship, and that is the first thing a release check should surface.

The easiest way to miss that mismatch is to let the fetch API follow redirects automatically. In normal browser or server code, a follow-up request can hide the original 301 or 302 and leave you looking at the final page as if nothing happened. For a deployment check, that behavior is too forgiving. The check needs the original response, not the destination the client ended up at, because the original response is what tells you whether the sitemap entry is stale, renamed, or accidentally pointing through a vanity path.

That is why the validator uses `redirect: 'manual'`. MDN documents `manual` as one of the supported redirect modes on `Request.redirect`, and this is the mode that lets the caller inspect the redirect response instead of following it away. The point is not to block all redirects everywhere. The point is to make a deliberate release-time decision: if a sitemap URL redirects, the check should say so clearly and let the operator decide whether the sitemap, the routing, or the canonical URL needs to change.

SourcesGoogle Search Central: Build and submit a sitemap (opens a new tab)MDN: Request.redirect (opens a new tab)

The validator only needs one injected dependency

The sample validator is intentionally small. It takes a list of URLs and a `fetchFn`, then calls that function with `{ redirect: 'manual' }` for each URL. That injected fetch is the whole seam that makes the check testable: the validator does not know whether the transport is Node’s global fetch, a stub, a recorded fixture, or a custom HTTP client. It only cares about the response shape it gets back.

Each result records the URL, the HTTP status, the raw `X-Robots-Tag` header, a problem list, and a boolean `ok`. The problem list is more useful than a single pass/fail bit because it distinguishes the causes. A 301 is a redirect problem. A 200 with `X-Robots-Tag: noindex` is a noindex problem. A response outside the 2xx range is a transport or application error. That separation matters when the deployment owner has to decide whether the fix belongs in routing, headers, content generation, or the sitemap itself.

The `X-Robots-Tag` check is also intentionally header-only. Google documents `noindex` as a response header or a meta tag, but the header is the one you can inspect without parsing HTML. That makes it a practical first pass for every sitemap URL, including non-HTML resources. If you need to check the HTML robots meta tag too, do that in a second step with an HTML parser, not by broadening this validator until it does two different jobs at once.

SourcesMDN: Request.redirect (opens a new tab)Google Search Central: Block search indexing with noindex (opens a new tab)

Checking an HTML robots meta tag is a separate step

Google’s noindex documentation is explicit that the rule can live in a meta tag or an HTTP response header. That flexibility is useful, but it also creates two different inspection paths. The validator in this article intentionally checks the header path only. When the content type is HTML and you also need to verify a meta tag, the next step is to fetch the document body and parse the `<head>` element for `<meta name="robots" content="noindex">` or the Googlebot-specific equivalent.

Keeping that parser separate helps for two reasons. First, it avoids mixing transport validation with document parsing in the same function. A page can be reachable, return 200, and still contain a noindex meta tag; that is not the same failure as a redirect. Second, it lets you decide whether a page should be accepted by header, by HTML, or by both. Some teams want both checks on HTML pages because the header can come from infrastructure while the meta tag comes from the app template. Others only need one of them for a given deployment boundary.

The important limitation is that an HTML parser can only prove what the server returned in that response. It cannot prove that Google has already crawled the page, and it cannot prove how long it will take for a change to appear in search results. Google says recrawling may take time, and that makes this parser a deployment diagnostic, not an indexing oracle. That distinction keeps the operator from overreading a local success as an immediate search result change.

SourcesGoogle Search Central: Block search indexing with noindex (opens a new tab)

What the fixture proves, and what it does not

The local tests cover three concrete cases: a direct 200 response, a redirect response, and a 200 response that includes `X-Robots-Tag: googlebot: noindex, nofollow`. Those three examples are enough to show the validator’s intended branch structure. A clean 200 returns `ok: true` and an empty problem list. A 301 returns `ok: false` with a `redirect` problem. A 200 with a noindex header returns `ok: false` with a `noindex-header` problem. The tests also verify that the injected fetch receives `redirect: 'manual'`, which is the key contract that keeps the redirect visible.

That proof boundary is deliberately narrow. The tests do not prove Google will index or de-index anything. They do not prove how Search Console will eventually report the URL. They do not parse HTML robots meta tags, because that is a different mechanism with a different dependency. They also do not try to simulate Googlebot, which would be the wrong standard for a deployment check anyway. The purpose is simply to catch a release that would ship an obviously mismatched sitemap entry or indexing header.

The fixture is still useful because it tests the branch that is most likely to be accidentally removed in a refactor. If someone changes the validator to follow redirects automatically, the redirect case loses the evidence it was meant to catch. If someone narrows the header check to only exact `noindex`, the `googlebot: noindex, nofollow` case will still pass because the parser splits the header into directives and checks for `noindex` or `none` anywhere in the value.

SourcesGoogle Search Central: Build and submit a sitemap (opens a new tab)Google Search Central: Block search indexing with noindex (opens a new tab)

Where this check stops, and what to do next

This kind of validator is best treated as a release gate for known URLs, not as a whole-search-visibility system. It tells you what the origin server returns at the moment of deployment. It does not tell you whether your sitemap file is well formed, whether Search Console has processed it, whether robots.txt blocks the crawler, or whether Google has already cached an older response. Those are separate layers with separate tools.

The check is also conservative about what counts as success. A sitemap URL that redirects is not a clean sitemap URL even if the destination is correct. In many teams, that is enough reason to either update the sitemap entry or update the site so the canonical URL is listed directly. Likewise, a page that emits `noindex` may be correct for staging or private content, but it is a release problem if the same URL is supposed to appear in search. The validator is valuable because it makes those distinctions visible before the release crosses the boundary into indexing behavior you do not directly control.

The practical workflow is simple: keep the sitemap URLs absolute, check the response for each URL with redirect following disabled, inspect the status and `X-Robots-Tag`, and then separately parse HTML when the URL is a page and you need to verify a meta tag. That gives you a deployment check that matches the actual contract of the sitemap: the URLs listed there should already look like the URLs you want crawled.

main.test.mjs / javascript
import test from 'node:test';
import assert from 'node:assert/strict';
import { checkSitemapUrls } from './main.mjs';

function response(status, headers = {}) {
  const normalized = new Map(
    Object.entries(headers).map(([name, value]) => [name.toLowerCase(), value]),
  );
  return {
    status,
    headers: { get: (name) => normalized.get(name.toLowerCase()) ?? null },
  };
}

test('accepts a sitemap URL served directly with HTTP 200', async () => {
  const calls = [];
  const fetchFn = async (url, options) => {
    calls.push({ url, options });
    return response(200);
  };

  const [result] = await checkSitemapUrls(['https://example.test/products'], fetchFn);

  assert.equal(result.ok, true);
  assert.deepEqual(result.problems, []);
  assert.deepEqual(calls, [{
    url: 'https://example.test/products',
    options: { redirect: 'manual' },
  }]);
});

test('reports a redirect instead of following a sitemap URL', async () => {
  const fetchFn = async (url, options) => {
    assert.equal(url, 'https://example.test/old-product');
    assert.equal(options.redirect, 'manual');
    return response(301, { location: 'https://example.test/products' });
  };

  const [result] = await checkSitemapUrls(['https://example.test/old-product'], fetchFn);

  assert.equal(result.ok, false);
  assert.deepEqual(result.problems, ['redirect']);
  assert.equal(result.status, 301);
});

test('reports noindex in an X-Robots-Tag header on an otherwise successful response', async () => {
  const fetchFn = async () => response(200, {
    'X-Robots-Tag': 'googlebot: noindex, nofollow',
  });

  const [result] = await checkSitemapUrls(['https://example.test/private-guide'], fetchFn);

  assert.equal(result.ok, false);
  assert.deepEqual(result.problems, ['noindex-header']);
  assert.equal(result.xRobotsTag, 'googlebot: noindex, nofollow');
});

SourcesGoogle Search Central: Build and submit a sitemap (opens a new tab)Google Search Central: Block search indexing with noindex (opens a new tab)

Share LinkedIn Email Subscribe

Discussion

Leave a comment

Comments appear after review. No email needed.

Image preview

Follow the blog

Choose email updates or your favorite RSS reader.

The weekly email

New articles on code, design, and AI. Fridays, only when there is something new.

By subscribing, you agree to weekly blog emails.
Confirm in your inbox. Unsubscribe anytime. Privacy

Use OpenRSS

Preview the feed, then choose a reader to subscribe.

Open in OpenRSS (opens a new tab)

Already have a reader?

Paste this link into your reader's Add feed option.

Prefer a reader? RSS does not require an email address. View XML feed