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(basic): stop SitemapRequestLoader from looping on cyclic sitemap indexes (#4172)

brekenec5efcc5006e
diff --git a/packages/basic-crawler/src/internals/sitemap-request-loader.ts b/packages/basic-crawler/src/internals/sitemap-request-loader.ts
index 2bfe9163f..19ba8c945 100644
--- a/packages/basic-crawler/src/internals/sitemap-request-loader.ts
+++ b/packages/basic-crawler/src/internals/sitemap-request-loader.ts
@@ -124,6 +124,7 @@ interface SitemapParsingProgress {
inProgressSitemapUrl: string | null;
inProgressEntries: Set<string>;
pendingSitemapUrls: Set<string>;
+ processedSitemapUrls: Set<string>;
}
interface SitemapRequestLoaderState {
@@ -169,6 +170,10 @@ export class SitemapRequestLoader implements IRequestLoader {
* Set of sitemap URLs that have not been parsed yet. If the set is empty and `inProgressSitemapUrl` is `null`, the sitemap loading is finished.
*/
pendingSitemapUrls: new Set<string>(),
+ /**
+ * Set of sitemap URLs that have already been parsed. Keeps sitemap indexes that reference each other from being loaded again.
+ */
+ processedSitemapUrls: new Set<string>(),
};
/**
@@ -368,7 +373,9 @@ export class SitemapRequestLoader implements IRequestLoader {
})) {
if (!item.originSitemapUrl) {
// This is a nested sitemap
- this.#sitemapParsingProgress.pendingSitemapUrls.add(item.loc);
+ if (!this.#sitemapParsingProgress.processedSitemapUrls.has(item.loc)) {
+ this.#sitemapParsingProgress.pendingSitemapUrls.add(item.loc);
+ }
continue;
}
@@ -382,6 +389,7 @@ export class SitemapRequestLoader implements IRequestLoader {
}
this.#sitemapParsingProgress.pendingSitemapUrls.delete(sitemapUrl);
+ this.#sitemapParsingProgress.processedSitemapUrls.add(sitemapUrl);
this.#sitemapParsingProgress.inProgressEntries.clear();
this.#sitemapParsingProgress.inProgressSitemapUrl = null;
}
@@ -521,6 +529,7 @@ export class SitemapRequestLoader implements IRequestLoader {
pendingSitemapUrls: Array.from(this.#sitemapParsingProgress.pendingSitemapUrls),
inProgressSitemapUrl: this.#sitemapParsingProgress.inProgressSitemapUrl,
inProgressEntries: Array.from(this.#sitemapParsingProgress.inProgressEntries),
+ processedSitemapUrls: Array.from(this.#sitemapParsingProgress.processedSitemapUrls),
},
// Re-queue in-progress requests to the front so they are retried if the state is restored.
urlQueue: [...this.inProgress, ...urlQueue],
@@ -548,6 +557,7 @@ export class SitemapRequestLoader implements IRequestLoader {
pendingSitemapUrls: new Set(state.sitemapParsingProgress.pendingSitemapUrls),
inProgressSitemapUrl: state.sitemapParsingProgress.inProgressSitemapUrl,
inProgressEntries: new Set(state.sitemapParsingProgress.inProgressEntries),
+ processedSitemapUrls: new Set(state.sitemapParsingProgress.processedSitemapUrls ?? []),
};
this.#requestData = new Map(state.requestData ?? []);
diff --git a/test/basic-crawler/sitemap-request-loader.test.ts b/test/basic-crawler/sitemap-request-loader.test.ts
index 8093cda4f..d899ca6b9 100644
--- a/test/basic-crawler/sitemap-request-loader.test.ts
+++ b/test/basic-crawler/sitemap-request-loader.test.ts
@@ -201,6 +201,32 @@ beforeAll(async () => {
res.end();
});
+ // Two sitemap indexes referencing each other; only `cycle-a` also points at an actual urlset.
+ app.get('/cycle-a.xml', async (req, res) => {
+ res.setHeader('content-type', 'text/xml');
+ res.end(
+ [
+ '<?xml version="1.0" encoding="UTF-8"?>',
+ '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
+ `<sitemap><loc>${url}/cycle-b.xml</loc></sitemap>`,
+ `<sitemap><loc>${url}/sitemap.xml</loc></sitemap>`,
+ '</sitemapindex>',
+ ].join('\n'),
+ );
+ });
+
+ app.get('/cycle-b.xml', async (req, res) => {
+ res.setHeader('content-type', 'text/xml');
+ res.end(
+ [
+ '<?xml version="1.0" encoding="UTF-8"?>',
+ '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
+ `<sitemap><loc>${url}/cycle-a.xml</loc></sitemap>`,
+ '</sitemapindex>',
+ ].join('\n'),
+ );
+ });
+
// --- Fixtures for the enqueue-strategy filtering tests ---
// The server answers on both `localhost` and `127.0.0.1` (distinct hostnames), so the `127.0.0.1`
// variant is a reachable "cross-host" target — a dropped entry is distinguishable from a failed fetch.
@@ -684,6 +710,28 @@ describe('SitemapRequestLoader', () => {
expect(CrawlingRequest.fromSchema(request!).enqueueStrategy).toBe('same-hostname');
});
+ test('sitemap indexes referencing each other are each loaded only once', async () => {
+ const list = await SitemapRequestLoader.open({ sitemapUrls: [`${url}/cycle-a.xml`], enqueueStrategy: 'all' });
+
+ const urls: string[] = [];
+ for await (const request of list) {
+ urls.push(request.url);
+ await list.markRequestAsHandled(request);
+ // Guard against the loader cycling between the two indexes forever.
+ if (urls.length > 20) break;
+ }
+
+ expect(list.isSitemapFullyLoaded()).toBe(true);
+ expect(urls).toEqual([
+ 'http://not-exists.com/',
+ 'http://not-exists.com/catalog?item=12&desc=vacation_hawaii',
+ 'http://not-exists.com/catalog?item=73&desc=vacation_new_zealand',
+ 'http://not-exists.com/catalog?item=74&desc=vacation_newfoundland',
+ 'http://not-exists.com/catalog?item=83&desc=vacation_usa',
+ ]);
+ await list.teardown();
+ }, 10_000);
+
test('persistState does not deadlock a backpressured sitemap load', async () => {
// `maxBufferSize: 1` makes the background loader block on backpressure right after
// pushing the first URL. Persisting the state at that moment swaps the underlying stream,