fix(DocsCrawler): Add response.ok check and improve link splitting

- Added skip for 404 errors in the page parsing logic
- Resolved incorrect handling of relative URLs in link splitting
This commit is contained in:
minicx 2024-10-10 05:34:25 +03:00
parent bbd29eab80
commit d613b5a7da

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 };
}