Repository navigation
fix(content): read a fetched page in time near its length, and turn away one nested deeper than a browser keeps - #1595
Merged
Merged
Conversation
…way one nested deeper than a browser keeps - html-depth.js: nestsTooDeep counts how deep a page's elements nest in one pass (htmlparser2) that stops at 512, the depth browsers keep. fetchPage answers 'too_deep' for such a web page, and fetchViaScrapeDo comes back not ok, so it isn't parsed. The URL import answers 422, Site Audit says why, and a link check counts the page as there. - extractPageContent reads a page in time near its length, whatever its shape: elements are found from the element itself (css-select, compiled once), removed and unwrapped siblings together, each block above asked once, and a block's text length and links counted once, children first. Its output is the same: 33,000 generated pages and the hand-made edge cases compare equal with the previous version. - htmlparser2 and css-select, already installed under cheerio, are listed as the server's own dependencies.
7 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reading a fetched page now takes time near its length, whatever its shape, and a page nested deeper than any browser keeps is turned away before it's parsed. This covers the URL import (Optimize › Page URL), Site Audit's page read, the cited pages' lengths, brand facts, Site trust and link checks.
Nesting depth (
server/src/lib/html-depth.js)nestsTooDeep(html)counts how deep the elements nest in one pass with htmlparser2, with the implied closes a browser makes (an unclosed<p>or<li>), and stops at the limit: 512, the depth browsers keep.fetchPageanswersPageFetchError('too_deep')for such a web page (other types, such as a feed or robots.txt, aren't counted), andfetchViaScrapeDocomes back not ok witherror: 'too_deep'.too_deep; the dialog shows its general "couldn't bring it in" text), Site Audit says "The page nests its elements too deeply to read" (422), a link check counts the page as there (ok, as fornot_htmlandtoo_large), and brand facts and Site trust treat it as a page they couldn't use.extractPageContent(server/src/lib/page-content.js)findsorts an element's children out again andremovesearches its siblings, each in time growing with the square of their number; asking each paragraph or image about every block above it grows with the page's length times its depth. Now:findInfinds elements from the element itself, each tested once against the selector, compiled once (css-select);removeAll,unwrapAll), each parent's children rebuilt once;upward);blockReader), and its text built only while it's short enough to be a byline.Dependencies:
htmlparser2(^10.1.0) andcss-select(^5.2.2) were already installed under cheerio; they're now listed as the server's own, and the lockfile's root entry names them. Nothing new is downloaded.Related issue
Follow-up from the reviews of #1563 and #1565.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
cd server npx vitest run src/lib/html-depth.test.js src/lib/page-content.test.js src/lib/safe-fetch.test.js src/lib/link-check.test.jshtml-depth.test.js: the limit, the implied closes, and a 600,000-element page refused quickly.page-content.test.js: seven page shapes of 1 MB, each read in under 4 s; the previous version fails the first.safe-fetch.test.js: a nested page served as HTML istoo_deep; served as text it's read.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdnpm run lint,npm run format:checkandnpm testpass (run fromserver/; 2,952 tests)🤖 Generated with Claude Code