Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cheerio issues with HTML serialization #1113

Merged
merged 4 commits into from Aug 12, 2021

Conversation

mnmkng
Copy link
Member

@mnmkng mnmkng commented Aug 11, 2021

After update to cheerio@1.0.0-rc.10 the $.html() calls started failing with the following error:

ERROR CheerioCrawler: Request failed and reached maximum retries {"url":"https://www.example.com/","method":"GET","uniqueKey":"https://www.example.com"}
  TypeError: Cannot read property 'charset' of undefined
      at Object.exports.getAttrList (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5-htmlparser2-tree-adapter/lib/index.js:283:53)
      at Serializer._serializeAttributes (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:99:40)
      at Serializer._serializeElement (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:67:14)
      at Serializer._serializeChildNodes (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:50:26)
      at Serializer._serializeElement (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:93:18)
      at Serializer._serializeChildNodes (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:50:26)
      at Serializer._serializeElement (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:93:18)
      at Serializer._serializeChildNodes (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:50:26)
      at Serializer.serialize (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/serializer/index.js:36:14)
      at Object.exports.serialize (/Users/mnmkng/Projects/Apify/Actors/bug-test/node_modules/parse5/lib/index.js:28:23)

It's caused by some incompatibility between htmlparser2 which is the streaming parser we use and which was originally used by cheerio and parse5, which is the new parser they use. We can easily override the parser, but not the serializer. It has some sort of compatibility layer, but it seems to be broken and the maintainer of parse5 says he doesn't have time to maintain the library. So let's use this hack to keep using htmlparser2 for serialization too.

@mnmkng mnmkng requested a review from szmarczak August 11, 2021 12:26
@mnmkng mnmkng merged commit e0ae762 into master Aug 12, 2021
@mnmkng mnmkng deleted the fix/cheerio-serialization-issues branch August 12, 2021 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants