MCPcopy
hub / github.com/ampproject/amphtml / tickLayoutShiftScore_

Method tickLayoutShiftScore_

src/service/performance-impl.js:592–621  ·  view source on GitHub ↗

* Tick the layout shift metric, following the latest CWV standard. This uses * a 5s maximum window with a 1s gap between events to define a "session". * We report the maximum session value over the lifetime of the page as the CLS. * * @param {!LayoutShift} entry

(entry)

Source from the content-addressed store, hash-verified

590 * @param {!LayoutShift} entry
591 */
592 tickLayoutShiftScore_(entry) {
593 if (!this.ampdoc_) {
594 return;
595 }
596
597 if (this.isVisibilityHidden_()) {
598 return;
599 }
600
601 const entries = this.layoutShiftEntries_;
602 if (entries.length > 0) {
603 const first = entries[0];
604 const last = entries[entries.length - 1];
605 if (
606 entry.startTime - last.startTime < CLS_SESSION_GAP &&
607 entry.startTime - first.startTime < CLS_SESSION_MAX
608 ) {
609 // This entry continues the current CLS window.
610 entries.push(entry);
611 return;
612 }
613 // This entry is the start of a new CLS window, but we haven't flushed the old value yet.
614 this.flushLayoutShiftScore_();
615 }
616 entries.push(entry);
617 // Ensure we report the CLS when the session closes. We're not guaranteed
618 // to get more LayoutShift entries, so we need some setTimeout magic to
619 // ensure it happens.
620 this.debouncedFlushLayoutShiftScore_();
621 }
622
623 /**
624 * Records the normalized CLS score, following the latest CWV standard.

Callers 1

processEntryMethod · 0.95

Calls 3

isVisibilityHidden_Method · 0.95
pushMethod · 0.45

Tested by

no test coverage detected