A link check can report success and still miss the thing you wanted to test.
Suppose your acceptance rule is: “This internal link must reach its destination without an HTTP redirect.” Checking only (await fetch(url)).status === 200 does not express that rule. With default redirect handling, the response can describe the final destination rather than the first response.
This example isolates that distinction with a disposable local server. It is a synthetic regression test, not a crawl of a client website or evidence of an SEO improvement.
AI disclosure: This article and its code were generated by an AI assistant. The executable example was run locally on Node.js v24.22.3. The test output below comes from that run; the routes and page content are invented fixtures.
Two assertions, two different questions
“Does the destination eventually return 200?” and “Does this exact URL return 200 directly?” are different checks.
| Fixture | First response | Result with normal following |
|---|---|---|
/direct |
200 | 200 |
/old |
301 to /direct?source=example
|
200 |
/chain |
302 to /old
|
200 after two hops |
/loop |
307 to itself | Cannot finish successfully |
/missing |
404 | 404 |
The table describes the server defined below. The script deliberately follows only /old; its other assertions inspect the first hop. It does not run a redirect loop to exhaustion.
A successful eventual response is useful evidence, but it is not evidence of a redirect-free link. In this test, redirect: 'manual' makes the first response observable. The expected status is exactly 200, not just response.ok, which also includes other 2xx statuses.
A copy-and-run regression test
Save the following as redirect-check.mjs and run node redirect-check.mjs. It uses Node's built-in HTTP server, assertions and fetch. No package installation, account, credentials or public target is needed. The server binds only to loopback and chooses a temporary port.
import assert from 'node:assert/strict';
import { createServer } from 'node:http';
// Local, synthetic fixtures only. Nothing is fetched from the public internet.
const server = createServer((req, res) => {
const url = new URL(req.url, 'http://fixture.invalid');
if (url.pathname === '/direct') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Example page');
} else if (url.pathname === '/old') {
res.writeHead(301, { Location: '/direct?source=example' });
res.end();
} else if (url.pathname === '/chain') {
res.writeHead(302, { Location: '/old' });
res.end();
} else if (url.pathname === '/loop') {
res.writeHead(307, { Location: '/loop' });
res.end();
} else {
res.writeHead(404);
res.end('Not found');
}
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const origin = `http://127.0.0.1:${server.address().port}`;
async function firstHop(href) {
const requested = new URL(href, origin);
// This demo accepts only its own fixture origin, not arbitrary URLs.
if (requested.origin !== origin || requested.username || requested.password) {
throw new Error('Outside the local fixture origin');
}
const response = await fetch(requested, {
method: 'GET', redirect: 'manual', signal: AbortSignal.timeout(2000)
});
const result = {
status: response.status,
location: response.headers.get('location'),
direct200: response.status === 200
};
// Release resources; this check does not need the page body.
await response.body?.cancel();
return result;
}
try {
const followed = await fetch(`${origin}/old`, {
signal: AbortSignal.timeout(2000)
});
assert.equal(followed.status, 200);
assert.equal(followed.redirected, true);
assert.equal(new URL(followed.url).pathname, '/direct');
await followed.body?.cancel();
console.log('Default fetch: /old -> status 200, redirected true');
const cases = [
['/direct?source=example#details', 200, null, true],
['/old', 301, '/direct?source=example', false],
['/chain', 302, '/old', false],
['/loop', 307, '/loop', false],
['/missing', 404, null, false]
];
for (const [href, status, location, direct200] of cases) {
const actual = await firstHop(href);
assert.deepEqual(actual, { status, location, direct200 });
console.log(`${href}: ${actual.status}; direct200=${actual.direct200}`);
}
await assert.rejects(firstHop('https://example.com/'), /Outside/);
console.log('PASS: default-follow check, five first-hop cases, origin guard');
} finally {
server.closeAllConnections();
await new Promise(resolve => server.close(resolve));
}
What the run actually established
The recorded output was:
Default fetch: /old -> status 200, redirected true
/direct?source=example#details: 200; direct200=true
/old: 301; direct200=false
/chain: 302; direct200=false
/loop: 307; direct200=false
/missing: 404; direct200=false
PASS: default-follow check, five first-hop cases, origin guard
The first assertion demonstrates the false shortcut: /old produces a final 200, but redirected is true. The five manual checks distinguish direct success, a permanent redirect, the start of a chain, a self-redirect and a missing URL. The final assertion rejects an input outside the fixture origin before any request is made.
The checker does not follow a Location header. In particular, the /loop test is one request, not an unbounded traversal. It cancels response bodies it does not need and uses a timeout. Assertions stop the run on a mismatch, while the finally block closes the server.
Apply the rule to a link, not just a destination
For an authorised check of a site you maintain, record the source page, the original href and the first response separately. If a replacement is appropriate, verify the intended destination and update that specific link. Do not delete a redirect simply because one internal link no longer needs it; old bookmarks and external references may still depend on it.
Keep query parameters and fragments intentional. In the fixture, the redirect supplies ?source=example; a replacement that drops it would not preserve the same URL. The #details fragment in the direct fixture is not sent in an HTTP request. A 200 therefore says nothing about whether a matching HTML anchor exists. Validate the actual anchor separately.
Use GET when you need to test GET behaviour. A HEAD-only check is a different request and can produce different results on some servers. Conversely, do not casually fetch arbitrary user-supplied URLs: the small origin guard here is tailored to a fixed local fixture, not a complete security design for a public URL-checking service.
Boundaries of this test
This is a Node.js example, not code for a browser console. Browser Fetch can expose manual redirects as opaque responses; do not assume you will be able to read the same status and headers there. Node's built-in fetch uses Undici, whose documentation explains its manual-redirect behaviour.
A direct 200 also does not establish that the content is correct, in the right language, indexable or canonical. It will not detect a JavaScript redirect, a meta-refresh, a soft-404 page or a broken fragment. Those require additional checks. Keep this assertion narrow: for this GET request, the first HTTP response is 200.
Top comments (0)