Merge pull request #2499 from loss-and-quick/fix-CheerioCrawler

fix(DocsCrawler): Add response.ok check and improve link splitting
This commit is contained in:
Nate Sesti 2024-10-12 22:48:24 -07:00 • committed by GitHub
commit 00968c2d54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -261,7 +261,9 @@ class CheerioCrawler {
console.error(error);
return { html: "", links: [] };
}
if (!response.ok) {
return { html: "", links: [] };
}
const html = await response.text();
let links: string[] = [];
@ -277,7 +279,7 @@ class CheerioCrawler {
return;
}
const parsedUrl = new URL(href, url);
const parsedUrl = new URL(href, location);
if (parsedUrl.hostname === baseUrl.hostname) {
links.push(parsedUrl.pathname);
}
@ -289,14 +291,11 @@ class CheerioCrawler {
!this.IGNORE_PATHS_ENDING_IN.some((ending) => link.endsWith(ending))
);
});
return { html, links };
}
private splitUrl(url: URL) {
const baseUrl = `${url.protocol}//${url.hostname}${
url.port ? ":" + url.port : ""
}`;
const baseUrl = new URL('/', url).href;
const basePath = url.pathname;
return { baseUrl, basePath };
}