Skip to content

Commit 5aa1f76

Browse files
committed
Custom metric for content-visibility
1 parent 57829a0 commit 5aa1f76

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

dist/almanac.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,5 +777,39 @@ return JSON.stringify({
777777

778778
'length_of_h1s': (() => {
779779
return [...document.querySelectorAll('h1')].map(node => node.innerText.length);
780+
})(),
781+
782+
'content_visibility': (() => {
783+
// Detects elements using the content-visibility CSS property
784+
const elements = document.querySelectorAll('*');
785+
const contentVisibilityElements = [];
786+
const contentVisibilityValues = {};
787+
788+
for (const element of elements) {
789+
try {
790+
const computedStyle = getComputedStyle(element);
791+
const contentVisibility = computedStyle.getPropertyValue('content-visibility');
792+
793+
if (contentVisibility && contentVisibility !== 'visible') {
794+
contentVisibilityElements.push({
795+
tagName: element.tagName.toLowerCase(),
796+
contentVisibility: contentVisibility.trim(),
797+
className: element.className || '',
798+
id: element.id || ''
799+
});
800+
801+
const value = contentVisibility.trim();
802+
contentVisibilityValues[value] = (contentVisibilityValues[value] || 0) + 1;
803+
}
804+
} catch (e) {
805+
// continue regardless of error
806+
}
807+
}
808+
809+
return {
810+
total: contentVisibilityElements.length,
811+
elements: contentVisibilityElements,
812+
values: contentVisibilityValues
813+
};
780814
})()
781815
});

metric-summary.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,36 @@ Example response:
723723
[5, 12, 11]
724724
```
725725

726+
## content_visibility
727+
728+
Detects elements using the `content-visibility` CSS property for performance optimization analysis. Returns information about elements that have content-visibility set to values other than 'visible' (such as 'auto', 'hidden', or 'skip').
729+
730+
Example response:
731+
732+
```json
733+
{
734+
"total": 15,
735+
"elements": [
736+
{
737+
"tagName": "div",
738+
"contentVisibility": "auto",
739+
"className": "lazy-section",
740+
"id": "section-1"
741+
},
742+
{
743+
"tagName": "section",
744+
"contentVisibility": "hidden",
745+
"className": "hidden-content",
746+
"id": ""
747+
}
748+
],
749+
"values": {
750+
"auto": 12,
751+
"hidden": 3
752+
}
753+
}
754+
```
755+
726756
## [Images.js](https://github.com/HTTPArchive/custom-metrics/blob/main/dist/Images.js) metrics
727757

728758
A JSON array of `<img>` elements on the page.

0 commit comments

Comments
 (0)