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

On certain audited pages, totalBodyElements are different from MainDocumentContent body nodes count #15920

Closed
2 tasks done
hrenaud opened this issue Apr 4, 2024 · 1 comment
Assignees

Comments

@hrenaud
Copy link

hrenaud commented Apr 4, 2024

FAQ

URL

https://www.spie.com/en/about-us/spie-around-world

What happened?

Through a plugin, I want to count SVG child elements.
To do this, I was going to use MainDocumentContent, but on the example page, the code is incomplete. When I count the number of elements in body, I get a big difference with the DOMStats data (which is good).
In my tests, I have :
allBodyNodesFromMainDocumentContent: 1542
artifacts.DOMStats.totalBodyElements: 4651
I'm using JSDOM to browse MainDocumentContent, but it's not the JSDOM that's causing the problem - I've checked the contents of MainDocumentContent.
Here's my code:

const totalBodyElements = artifacts.DOMStats.totalBodyElements
  const MainDocumentContent = artifacts.MainDocumentContent
  const dom = new JSDOM(MainDocumentContent)
  const allBodyNodesFromMainDocumentContent = dom.window.document
    .getElementsByTagName('body')[0]
    .querySelectorAll('*').length
  const svgContentBodyNodesFromMainDocumentContent = dom.window.document
    .getElementsByTagName('body')[0]
    .querySelectorAll('svg *').length

What did you expect?

I expect
allBodyNodesFromMainDocumentContent ===
artifacts.DOMStats.totalBodyElements

What have you tried?

I tried to find another gatherers that would contain the complete HTML code of the page, but I couldn't find anything other than MainDocumentContent.

How were you running Lighthouse?

node

Lighthouse Version

11.7.0

Chrome Version

linux-121.0.6167.85/chrome-linux64/ (from puppeteer)

Node Version

18.19.1

OS

Linux

Relevant log output

totalBodyElements 4651
allNodesFromMainDocumentContent 1565
allBodyNodesFromMainDocumentContent 1542
svgContentNodesFromMainDocumentContent 98
svgContentBodyNodesFromMainDocumentContent 98
@adamraine
Copy link
Member

totalBodyElements represents the number of nodes in the document's body at the end of the page load.

MainDocumentContent is just the contents of the initial HTML request the page made when it started loading.

They represent different things and are not designed to match. If your page has any javascript that adds/removes nodes from the page they will almost certainly be different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants