Skip to content

fix(content): read a fetched page in time near its length, and turn away one nested deeper than a browser keeps - #1595

Merged
gkhngyk merged 1 commit into
mainfrom
fix/html-nesting-depth
Oct 27, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/html-nesting-depth

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

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.
  • fetchPage answers PageFetchError('too_deep') for such a web page (other types, such as a feed or robots.txt, aren't counted), and fetchViaScrapeDo comes back not ok with error: 'too_deep'.
  • Callers: the URL import answers 422 (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 for not_html and too_large), and brand facts and Site trust treat it as a page they couldn't use.

extractPageContent (server/src/lib/page-content.js)

  • cheerio's find sorts an element's children out again and remove searches 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:
    • findIn finds elements from the element itself, each tested once against the selector, compiled once (css-select);
    • removals and H1 unwraps are done siblings together (removeAll, unwrapAll), each parent's children rebuilt once;
    • the hidden, furniture and list checks up the page are asked once per block (upward);
    • the text area's widening sums each paragraph's text on the first step it's inside, once;
    • a block's text length and links (count, link text, in-page links, distinct targets up to 8) are counted once, children first (blockReader), and its text built only while it's short enough to be a byline.
  • Same output. The 17 existing tests pass unchanged. 33,000 generated pages (frames, hidden FAQ answers, furniture names, link lists, bylines, icons, wordless H1s, nested lists) and 8 hand-made edge cases (nested wordless H1s, nested hidden blocks, answers in and out of the root) give the same title and HTML as the previous version, byte for byte.
  • Times for a 3 MB page (the fetch cap), on this machine; each grows with the page's length:
Page Time
Paragraphs side by side 0.66 s
Paragraphs under 508 blocks 0.86 s
Script siblings to take out 0.34 s
Images under 508 named blocks 1.2 s
Wordless H1 siblings 2.2 s
Nested past the limit refused in about 1 ms

Dependencies: htmlparser2 (^10.1.0) and css-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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How 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.js
  • html-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 is too_deep; served as text it's read.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • Server only: npm run lint, npm run format:check and npm test pass (run from server/; 2,952 tests)
  • Changes are focused — one concern per PR

🤖 Generated with Claude Code

…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.
@gkhngyk
gkhngyk merged commit 15b5b5f into main Oct 27, 2026
6 checks passed
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.

1 participant