Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: respect `maxCrawlDepth` in `JSDOM/LinkeDOM` context `enqueueLinks` (#4021)

Martin Adámek4d34aeab6d78
diff --git a/packages/jsdom-crawler/src/internals/jsdom-crawler.ts b/packages/jsdom-crawler/src/internals/jsdom-crawler.ts
index 627c0ce72..e17aaa331 100644
--- a/packages/jsdom-crawler/src/internals/jsdom-crawler.ts
+++ b/packages/jsdom-crawler/src/internals/jsdom-crawler.ts
@@ -296,6 +296,8 @@ export class JSDOMCrawler extends HttpCrawler<JSDOMCrawlingContext> {
}
}
+ const originalEnqueueLinks = crawlingContext.enqueueLinks;
+
return {
window,
get body() {
@@ -306,13 +308,16 @@ export class JSDOMCrawler extends HttpCrawler<JSDOMCrawlingContext> {
},
enqueueLinks: async (enqueueOptions?: EnqueueLinksOptions) => {
return domCrawlerEnqueueLinks({
- options: { ...enqueueOptions, limit: this.calculateEnqueuedRequestLimit(enqueueOptions?.limit) },
+ // `originalEnqueueLinks` clamps `limit` by the remaining `maxRequestsPerCrawl` budget itself;
+ // pre-clamping it here would make the crawler log the internal limit as a user-provided one
+ options: enqueueOptions,
window,
requestQueue: await this.getRequestQueue(),
robotsTxtFile: await this.getRobotsTxtFileForUrl(crawlingContext.request.url),
onSkippedRequest: this.handleSkippedRequest,
originalRequestUrl: crawlingContext.request.url,
finalRequestUrl: crawlingContext.request.loadedUrl,
+ enqueueLinks: originalEnqueueLinks,
});
},
};
diff --git a/packages/linkedom-crawler/src/internals/linkedom-crawler.ts b/packages/linkedom-crawler/src/internals/linkedom-crawler.ts
index fbc2bc1e9..a918021a3 100644
--- a/packages/linkedom-crawler/src/internals/linkedom-crawler.ts
+++ b/packages/linkedom-crawler/src/internals/linkedom-crawler.ts
@@ -178,6 +178,8 @@ export class LinkeDOMCrawler extends HttpCrawler<LinkeDOMCrawlingContext> {
const document = LinkeDOMCrawler.parser.parseFromString(body.toString(), isXml ? 'text/xml' : 'text/html');
+ const originalEnqueueLinks = crawlingContext.enqueueLinks;
+
return {
window: document.defaultView,
get body() {
@@ -189,13 +191,16 @@ export class LinkeDOMCrawler extends HttpCrawler<LinkeDOMCrawlingContext> {
},
enqueueLinks: async (enqueueOptions?: LinkeDOMCrawlerEnqueueLinksOptions) => {
return linkedomCrawlerEnqueueLinks({
- options: { ...enqueueOptions, limit: this.calculateEnqueuedRequestLimit(enqueueOptions?.limit) },
+ // `originalEnqueueLinks` clamps `limit` by the remaining `maxRequestsPerCrawl` budget itself;
+ // pre-clamping it here would make the crawler log the internal limit as a user-provided one
+ options: enqueueOptions,
window: document.defaultView,
requestQueue: await this.getRequestQueue(),
robotsTxtFile: await this.getRobotsTxtFileForUrl(crawlingContext.request.url),
onSkippedRequest: this.handleSkippedRequest,
originalRequestUrl: crawlingContext.request.url,
finalRequestUrl: crawlingContext.request.loadedUrl,
+ enqueueLinks: originalEnqueueLinks,
});
},
};
diff --git a/test/core/crawlers/dom_crawler.test.ts b/test/core/crawlers/dom_crawler.test.ts
index 81b65bb27..b2bd97d0a 100644
--- a/test/core/crawlers/dom_crawler.test.ts
+++ b/test/core/crawlers/dom_crawler.test.ts
@@ -2,6 +2,7 @@ import http from 'node:http';
import type { AddressInfo } from 'node:net';
import { JSDOMCrawler } from '@crawlee/jsdom';
+import { LinkeDOMCrawler } from '@crawlee/linkedom';
import { MemoryStorageEmulator } from '../../shared/MemoryStorageEmulator';
@@ -11,6 +12,15 @@ router.set('/', (req, res) => {
res.end(`<!DOCTYPE html><html><head><title>Example Domain</title></head><body><p>Hello, world!</p></body></html>`);
});
+for (const depth of [0, 1, 2]) {
+ router.set(`/depth-${depth}`, (req, res) => {
+ res.setHeader('content-type', 'text/html; charset=utf-8');
+ res.end(
+ `<!DOCTYPE html><html><head><title>Depth ${depth}</title></head><body><a href="/depth-${depth + 1}">link</a></body></html>`,
+ );
+ });
+}
+
let server: http.Server;
let url: string;
@@ -60,3 +70,37 @@ test('works', async () => {
expect(results).toStrictEqual(['Example Domain', 'Hello, world!']);
});
+
+test('JSDOMCrawler enqueueLinks should respect maxCrawlDepth', async () => {
+ const titles: string[] = [];
+
+ const crawler = new JSDOMCrawler({
+ maxCrawlDepth: 1,
+ maxRequestsPerCrawl: 10, // to avoid accidental runaway
+ requestHandler: async ({ window, enqueueLinks }) => {
+ titles.push(window.document.title);
+ await enqueueLinks();
+ },
+ });
+
+ await crawler.run([`${url}/depth-0`]);
+
+ expect(titles).toEqual(['Depth 0', 'Depth 1']);
+});
+
+test('LinkeDOMCrawler enqueueLinks should respect maxCrawlDepth', async () => {
+ const titles: string[] = [];
+
+ const crawler = new LinkeDOMCrawler({
+ maxCrawlDepth: 1,
+ maxRequestsPerCrawl: 10, // to avoid accidental runaway
+ requestHandler: async ({ document, enqueueLinks }) => {
+ titles.push(document.title);
+ await enqueueLinks();
+ },
+ });
+
+ await crawler.run([`${url}/depth-0`]);
+
+ expect(titles).toEqual(['Depth 0', 'Depth 1']);
+});