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

Performance 2022: CWV gaming #37

Merged
merged 13 commits into from
May 31, 2022
63 changes: 62 additions & 1 deletion dist/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,66 @@ function getWebVitalsJS() {
}).map(har => har.url);
}

function getGamingMetrics(rawDoc) {
let returnObj = {};
const regexForCheckChromeLH = new RegExp(/.{1}userAgent.{1,100}(?:Chrome-Lighthouse|Google Lighthouse).{2}/)
const regexForCheckGTmetrix = new RegExp(/.{1}userAgent.{1,100}(?:GTmetrix|gtmetrix.com).{2}/)
const regexForCheckPageSpeed = new RegExp(/.{1}userAgent.{1,100}(?:PageSpeed).{2}/)
let scripts = rawDoc.getElementsByTagName('script');
rviscomi marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < scripts.length; i++) {
if (scripts[i].src) {
continue;
}
let scriptTagCode = scripts[i].innerHTML;
if (regexForCheckChromeLH.test(scriptTagCode)){
returnObj['detectUA-ChromeLH'] = true;
}
if(regexForCheckGTmetrix.test(scriptTagCode)){
returnObj['detectUA-GTmetrix'] = true;
}
if(regexForCheckPageSpeed.test(scriptTagCode)){
returnObj['detectUA-PageSpeed'] = true;
}
}

//https://www.debugbear.com/blog/optimizing-web-vitals-without-improving-performance
//catch lcp animation / cls animation & overlay hack
const regexForCheckfadeInAnimation = new RegExp(/this.style.animation.{1,10}.fadein.{1,20}.forwards/)
mel-ada marked this conversation as resolved.
Show resolved Hide resolved
let elements = rawDoc.getElementsByTagName('img');
for (let i = 0; i < elements.length; i++) {
let el = elements[i];
let onloadVal = el.getAttribute('onload');
if (onloadVal !== null) {
if(regexForCheckfadeInAnimation.test(onloadVal)) {
returnObj['lcpAnimation'] = true;
mel-ada marked this conversation as resolved.
Show resolved Hide resolved
}
}
let styleObj = el.style;
if (styleObj['pointer-events'] == 'none' &&
mel-ada marked this conversation as resolved.
Show resolved Hide resolved
styleObj['position'] == 'absolute' &&
styleObj['width'] == '99vw' &&
styleObj['height'] == '99vh') {
returnObj['lcpOverlay'] = true;
mel-ada marked this conversation as resolved.
Show resolved Hide resolved
}
}

//add logic for svg & body overlay
let svgTags = rawDoc.getElementsByTagName('svg');
for (let i = 0; i < svgTags.length; i++) {
const svg = svgTags[i];
if (svg.clientHeight == 99999 &&
mel-ada marked this conversation as resolved.
Show resolved Hide resolved
svg.clientWidth == 99999 &&
svg.clientLeft == 0 &&
svg.clientTop == 0) {
//additional check required
returnObj['lcpSvgOverlay'] = true;
mel-ada marked this conversation as resolved.
Show resolved Hide resolved
}
}
//fid iframe hack
rviscomi marked this conversation as resolved.
Show resolved Hide resolved

return returnObj;
}

return Promise.all([getLcpElement()]).then(([lcp_elem_stats]) => {
const lcpUrl = lcp_elem_stats.url;
const rawDoc = getRawHtmlDocument();
Expand Down Expand Up @@ -121,7 +181,8 @@ return Promise.all([getLcpElement()]).then(([lcp_elem_stats]) => {
lcp_resource: responseObject,
is_lcp_statically_discoverable: isLcpStaticallyDiscoverable,
is_lcp_preloaded: isLcpPreloaded,
web_vitals_js: getWebVitalsJS()
web_vitals_js: getWebVitalsJS(),
gamingMetrics: getGamingMetrics(rawDoc)
};
}).catch(error => {
return {error};
Expand Down