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: sitemap `content-type` check breaks on `content-type` parameters (#2442)

Jindřich Bärdb7d37256a49
diff --git a/packages/utils/package.json b/packages/utils/package.json
index 14c3d87ce..cc5727376 100644
--- a/packages/utils/package.json
+++ b/packages/utils/package.json
@@ -56,6 +56,10 @@
"ow": "^0.28.1",
"robots-parser": "^3.0.1",
"sax": "^1.3.0",
- "tslib": "^2.4.0"
+ "tslib": "^2.4.0",
+ "whatwg-mimetype": "^4.0.0"
+ },
+ "devDependencies": {
+ "@types/whatwg-mimetype": "^3.0.2"
}
}
diff --git a/packages/utils/src/internals/sitemap.ts b/packages/utils/src/internals/sitemap.ts
index b8f94f164..be443452b 100644
--- a/packages/utils/src/internals/sitemap.ts
+++ b/packages/utils/src/internals/sitemap.ts
@@ -6,6 +6,7 @@ import { createGunzip } from 'node:zlib';
import log from '@apify/log';
import type { SAXStream } from 'sax';
import sax from 'sax';
+import MIMEType from 'whatwg-mimetype';
class ParsingState {
sitemapUrls: string[] = [];
@@ -170,12 +171,19 @@ export class Sitemap {
const parser = (() => {
const contentType = sitemapStream.response!.headers['content-type'];
+ let mimeType: MIMEType | null;
- if (['text/xml', 'application/xml'].includes(contentType ?? '') || sitemapUrl.pathname.endsWith('.xml')) {
+ try {
+ mimeType = new MIMEType(contentType ?? '');
+ } catch (e) {
+ mimeType = null;
+ }
+
+ if (mimeType?.isXML() || sitemapUrl.pathname.endsWith('.xml')) {
return Sitemap.createXmlParser(parsingState, () => resolve(undefined), reject);
}
- if (contentType === 'text/plain' || sitemapUrl.pathname.endsWith('.txt')) {
+ if (mimeType?.essence === 'text/plain' || sitemapUrl.pathname.endsWith('.txt')) {
return new SitemapTxtParser(parsingState, () => resolve(undefined));
}
diff --git a/yarn.lock b/yarn.lock
index 2e0849a11..13fe40cde 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -731,12 +731,14 @@ __metadata:
"@apify/ps-tree": "npm:^1.2.0"
"@crawlee/types": "npm:3.9.2"
"@types/sax": "npm:^1.2.7"
+ "@types/whatwg-mimetype": "npm:^3.0.2"
cheerio: "npm:^1.0.0-rc.12"
got-scraping: "npm:^4.0.3"
ow: "npm:^0.28.1"
robots-parser: "npm:^3.0.1"
sax: "npm:^1.3.0"
tslib: "npm:^2.4.0"
+ whatwg-mimetype: "npm:^4.0.0"
languageName: unknown
linkType: soft
@@ -2261,6 +2263,13 @@ __metadata:
languageName: node
linkType: hard
+"@types/whatwg-mimetype@npm:^3.0.2":
+ version: 3.0.2
+ resolution: "@types/whatwg-mimetype@npm:3.0.2"
+ checksum: 10c0/dad39d1e4abe760a0a963c84bbdbd26b1df0eb68aff83bdf6ecbb50ad781ead777f6906d19a87007790b750f7500a12e5624d31fc6a1529d14bd19b5c3a316d1
+ languageName: node
+ linkType: hard
+
"@types/yargs-parser@npm:*":
version: 21.0.3
resolution: "@types/yargs-parser@npm:21.0.3"