Skip to content

Commit b2ec31c

Browse files
authored
[Docs] Update API link crawler (#18794)
1 parent 66d6203 commit b2ec31c

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

bin/crawl-api-links.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ async function checkLinks() {
2121
});
2222
const page = await browser.newPage();
2323

24+
// skip image requests
25+
await page.setRequestInterception(true);
26+
page.on("request", (request) => {
27+
if (request.resourceType() === "image") request.abort();
28+
else request.continue();
29+
});
30+
2431
const sitemapUrl = "https://developers.cloudflare.com/sitemap.xml";
2532
await page.goto(sitemapUrl, { timeout: navigationTimeout });
2633

@@ -51,23 +58,22 @@ async function checkLinks() {
5158
}
5259

5360
if (
54-
pageLink.includes("developers.cloudflare.com/api/operations/") ||
55-
pageLink.startsWith("/api/operations/")
61+
pageLink.includes("developers.cloudflare.com/api/resources/") ||
62+
pageLink.startsWith("/api/resources/")
5663
) {
5764
console.log(`Evaluating link: ${pageLink}`);
58-
await page.goto(pageLink, {
65+
const response = await page.goto(pageLink, {
5966
waitUntil: "networkidle0",
6067
timeout: navigationTimeout,
6168
});
6269
visitedLinks.push(pageLink);
6370

64-
const statusCode = await page.evaluate(() => {
65-
return {
66-
url: window.location.href,
67-
};
68-
});
69-
if (statusCode.url === "https://developers.cloudflare.com/api/") {
70-
brokenLinks.push(pageLink);
71+
if (response) {
72+
if (response.status() === 404) {
73+
brokenLinks.push(pageLink);
74+
}
75+
} else {
76+
console.log("WARNING: Didn't receive a response... skipping.");
7177
}
7278
}
7379
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"check": "npm run check:astro && npm run check:worker",
1111
"check:astro": "npm run sync && astro check",
1212
"check:worker": "npm run build:worker && npx tsc --noEmit -p ./worker/tsconfig.json",
13+
"crawl-api-links": "node bin/crawl-api-links.js",
1314
"dev": "npx astro dev",
1415
"format": "npm run format:core && npm run format:data",
1516
"format:core": "npx prettier --write \"**/*.{js,jsx,ts,tsx,mjs,css}\"",

0 commit comments

Comments
 (0)