Skip to content

Commit

Permalink
♻️ extract monitoring logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Dec 14, 2023
1 parent 6395c3f commit decf74a
Showing 1 changed file with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface LargestContentfulPaint {

let zeroLcpReported = false
let previousNonZeroLcp: Omit<PerformanceEntry, 'toJSON'> | undefined

/**
* Track the largest contentful paint (LCP) occurring during the initial View. This can yield
* multiple values, only the most recent one should be used.
Expand Down Expand Up @@ -73,29 +74,10 @@ export function trackLargestContentfulPaint(
lcpTargetSelector = getSelectorFromElement(lcpEntry.element, configuration.actionNameAttribute)
}

if (!zeroLcpReported && lcpEntry.startTime !== 0) {
previousNonZeroLcp = lcpEntry.toJSON()
if (isExperimentalFeatureEnabled(ExperimentalFeature.ZERO_LCP_TELEMETRY)) {
monitorZeroLcpEntry(lcpEntry)
}

if (
!zeroLcpReported &&
lcpEntry.startTime === 0 &&
isExperimentalFeatureEnabled(ExperimentalFeature.ZERO_LCP_TELEMETRY)
) {
zeroLcpReported = true

addTelemetryDebug('LCP with startTime = 0', {
debug: {
entry: lcpEntry.toJSON(),
previousNonZeroLcp,
navigationStart: performance.timing.navigationStart,
timeOrigin: performance.timeOrigin,
now: relativeNow(),
visibilityState: document.visibilityState,
prerendering: (document as any).prerendering,
},
})
}
callback({
value: lcpEntry.startTime,
targetSelector: lcpTargetSelector,
Expand All @@ -111,3 +93,25 @@ export function trackLargestContentfulPaint(
},
}
}

function monitorZeroLcpEntry(lcpEntry: RumLargestContentfulPaintTiming) {
if (!zeroLcpReported && lcpEntry.startTime !== 0) {
previousNonZeroLcp = lcpEntry.toJSON()
}

if (!zeroLcpReported && lcpEntry.startTime === 0) {
zeroLcpReported = true

addTelemetryDebug('LCP with startTime = 0', {
debug: {
entry: lcpEntry.toJSON(),
previousNonZeroLcp,
navigationStart: performance.timing.navigationStart,
timeOrigin: performance.timeOrigin,
now: relativeNow(),
visibilityState: document.visibilityState,
prerendering: (document as any).prerendering,
},
})
}
}

0 comments on commit decf74a

Please sign in to comment.