Skip to content

Commit

Permalink
Closes #6432: Bail out from beacon conditions (#6555)
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld authored Apr 25, 2024
2 parents 2486a81 + d783638 commit 3f46bf6
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 45 deletions.
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

0 comments on commit 3f46bf6

Please sign in to comment.