DEV Community

Harry
Harry

Posted on Fully Autonomous

Decide when a Node.js crawler should pause after fetching robots.txt

A crawler needs a decision even when it cannot read a site's robots.txt. A missing file, a rate limit, and a network timeout deserve different treatment. If a catch block converts every failure to an empty string, your parser will quietly treat an overloaded site as open for crawling.

RFC 9309, section 2.3 distinguishes unavailable responses, such as HTTP 404, from network and server errors. The RFC permits crawling in the unavailable case and requires complete disallow for an unreachable file. A cautious crawler can allow only confirmed missing-file responses and pause on other client errors. For example, pause on 429, schedule a later retry honoring Retry-After when present, and stop without retrying automatically on 401 or 403.

Keep the fetch outcome separate from the parser

The function below takes the final HTTP status after your redirect handling, or null for a failed request. It returns a decision you can log and test without making any network requests.

function robotsDecision(status) {
  if (Number.isInteger(status) && status >= 200 && status < 300) {
    return { action: 'parse', reason: 'robots_response' };
  }
  if (status === 404 || status === 410) {
    return { action: 'eligible', reason: 'robots_missing' };
  }
  if (status === 429) {
    return { action: 'pause', reason: 'rate_limited' };
  }
  if (status === 401 || status === 403) {
    return { action: 'pause', reason: 'access_restricted' };
  }
  if (status === null || (status >= 500 && status < 600)) {
    return { action: 'pause', reason: 'robots_unreachable' };
  }
  return { action: 'pause', reason: 'unexpected_response' };
}
Enter fullscreen mode Exit fullscreen mode

Treat parse as a handoff to a maintained robots.txt parser. Evaluate the intended crawler product token and target URL against the parsed rules before scheduling a page request. The eligible result means that this check found no published file. Apply site terms and your own rate and scope limits in either case. Treat cached responses such as 304 in your HTTP/cache layer and pass the resolved result to this function.

Use a crawler identity you control. RFC 9309 gives guidance on following redirects and applying the resulting rules to the initial authority. Keep redirect limits, fetching, and caching in that layer. If your crawler separately supports the non-standard Crawl-delay directive, wait at least as long as requested or skip the origin when the wait exceeds your budget.

Verify the decision table locally

Save the function and these assertions in one check-robots.mjs file and run node check-robots.mjs.

import assert from 'node:assert/strict';

const cases = [
  [200, 'parse', 'robots_response'],
  [404, 'eligible', 'robots_missing'],
  [410, 'eligible', 'robots_missing'],
  [429, 'pause', 'rate_limited'],
  [401, 'pause', 'access_restricted'],
  [403, 'pause', 'access_restricted'],
  [503, 'pause', 'robots_unreachable'],
  [null, 'pause', 'robots_unreachable'],
  [302, 'pause', 'unexpected_response'],
  [304, 'pause', 'unexpected_response'],
];

for (const [status, action, reason] of cases) {
  assert.deepEqual(robotsDecision(status), { action, reason });
}
console.log(`Passed ${cases.length} robots outcome checks.`);
Enter fullscreen mode Exit fullscreen mode

AI disclosure: This article and its Node.js example were drafted with AI assistance and checked locally with Node.js assertions.

Top comments (0)