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

Closes #6432: Bail out from beacon conditions #6555

41 changes: 41 additions & 0 deletions assets/js/lcp-beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,47 @@ function getElementInfo(element) {
let performance_images = [];

function main() {

// AJAX call to check if there are any records for the current URL
let data_check = new FormData();
data_check.append('action', 'rocket_check_lcp');
data_check.append('rocket_lcp_nonce', rocket_lcp_data.nonce);
data_check.append('url', rocket_lcp_data.url);
data_check.append('is_mobile', rocket_lcp_data.is_mobile);

const response = fetch(rocket_lcp_data.ajax_url, {
method: "POST",
credentials: 'same-origin',
body: data_check
});

if ( true === lcp_data.success ) {
console.log('Bailing out because data is already available');
return;
}

// Check screen size
const screenWidth = window.innerWidth || document.documentElement.clientWidth;
const screenHeight = window.innerHeight || document.documentElement.clientHeight;
if ( ( rocket_lcp_data.is_mobile && ( screenWidth > rocket_lcp_data.width_threshold || screenHeight > rocket_lcp_data.height_threshold ) ) ||
( ! rocket_lcp_data.is_mobile && ( screenWidth < rocket_lcp_data.width_threshold || screenHeight < rocket_lcp_data.height_threshold ) ) )
{
console.log('Bailing out because screen size is not acceptable');
return;
}

// Filter the array based on the condition imageURL is not null
const filteredArray = LCPCandidates(1)
if (filteredArray.length !== 0) {
console.log("Estimated LCP element:", filteredArray);
performance_images = filteredArray.map((item) => ({
src: item.imageURL,
label: "lcp",
}));
} else {
console.log("No LCP candidate found.");
}

// Use LCPCandidates function to get all the elements in the viewport
const above_the_fold_images = LCPCandidates(Infinity);

Expand Down
2 changes: 1 addition & 1 deletion assets/js/lcp-beacon.js.min.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/lcp-beacon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading