Skip to content

Commit 2039994

Browse files
committed
Consider an LCP element as common if common in first and last groups and other groups are empty
Signed-off-by: Shyamsundar Gadde <[email protected]>
1 parent 20a45a3 commit 2039994

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

plugins/optimization-detective/class-od-url-metric-group-collection.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -437,38 +437,41 @@ public function get_common_lcp_element(): ?OD_Element {
437437

438438
$result = ( function () {
439439

440-
// If every group isn't populated, then we can't say whether there is a common LCP element across every viewport group.
441-
if ( ! $this->is_every_group_populated() ) {
440+
// Ensure both the narrowest (first) and widest (last) viewport groups are populated.
441+
$first_group = $this->get_first_group();
442+
$last_group = $this->get_last_group();
443+
if ( $first_group->count() === 0 || $last_group->count() === 0 ) {
442444
return null;
443445
}
444446

445-
// Look at the LCP elements across all the viewport groups.
446-
$groups_by_lcp_element_xpath = array();
447-
$lcp_elements_by_xpath = array();
448-
$group_has_unknown_lcp_element = false;
449-
foreach ( $this->groups as $group ) {
450-
$lcp_element = $group->get_lcp_element();
451-
if ( $lcp_element instanceof OD_Element ) {
452-
$groups_by_lcp_element_xpath[ $lcp_element->get_xpath() ][] = $group;
453-
$lcp_elements_by_xpath[ $lcp_element->get_xpath() ][] = $lcp_element;
454-
} else {
455-
$group_has_unknown_lcp_element = true;
456-
}
457-
}
447+
$first_group_lcp_element = $first_group->get_lcp_element();
448+
$last_group_lcp_element = $last_group->get_lcp_element();
458449

450+
// Validate LCP elements exist and have matching XPaths in the extreme viewport groups.
459451
if (
460-
// All breakpoints share the same LCP element.
461-
1 === count( $groups_by_lcp_element_xpath )
462-
&&
463-
// The breakpoints don't share a common lack of a detected LCP element.
464-
! $group_has_unknown_lcp_element
452+
! $first_group_lcp_element instanceof OD_Element
453+
||
454+
! $last_group_lcp_element instanceof OD_Element
455+
||
456+
$first_group_lcp_element->get_xpath() !== $last_group_lcp_element->get_xpath()
465457
) {
466-
$xpath = key( $lcp_elements_by_xpath );
458+
return null; // No common LCP element across the narrowest and widest viewports.
459+
}
467460

468-
return $lcp_elements_by_xpath[ $xpath ][0];
461+
// Check intermediate viewport groups for conflicting LCP elements.
462+
$num_groups = count( $this->groups );
463+
for ( $i = 1; $i < $num_groups - 1; $i++ ) {
464+
$group_lcp_element = $this->groups[ $i ]->get_lcp_element();
465+
if (
466+
$group_lcp_element instanceof OD_Element
467+
&&
468+
$group_lcp_element->get_xpath() !== $first_group_lcp_element->get_xpath()
469+
) {
470+
return null; // Conflicting LCP element found in an intermediate group.
471+
}
469472
}
470473

471-
return null;
474+
return $first_group_lcp_element;
472475
} )();
473476

474477
$this->result_cache[ __FUNCTION__ ] = $result;

0 commit comments

Comments
 (0)