-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
fbd15c7
commit 5e00489
Showing
7 changed files
with
67 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 18 additions & 20 deletions
38
packages/rum-core/src/domain/view/viewMetrics/trackFirstContentfulPaint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,35 @@ | ||
import type { RelativeTime } from '@datadog/browser-core' | ||
import { ONE_MINUTE, find } from '@datadog/browser-core' | ||
import type { LifeCycle } from '../../lifeCycle' | ||
import { LifeCycleEventType } from '../../lifeCycle' | ||
import type { RumPerformancePaintTiming } from '../../../browser/performanceObservable' | ||
import { RumPerformanceEntryType } from '../../../browser/performanceObservable' | ||
import { createPerformanceObservable, RumPerformanceEntryType } from '../../../browser/performanceObservable' | ||
import type { RumConfiguration } from '../../configuration' | ||
import type { FirstHidden } from './trackFirstHidden' | ||
|
||
// Discard FCP timings above a certain delay to avoid incorrect data | ||
// It happens in some cases like sleep mode or some browser implementations | ||
export const FCP_MAXIMUM_DELAY = 10 * ONE_MINUTE | ||
|
||
export function trackFirstContentfulPaint( | ||
lifeCycle: LifeCycle, | ||
configuration: RumConfiguration, | ||
firstHidden: FirstHidden, | ||
callback: (fcpTiming: RelativeTime) => void | ||
) { | ||
const { unsubscribe: unsubscribeLifeCycle } = lifeCycle.subscribe( | ||
LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, | ||
(entries) => { | ||
const fcpEntry = find( | ||
entries, | ||
(entry): entry is RumPerformancePaintTiming => | ||
entry.entryType === RumPerformanceEntryType.PAINT && | ||
entry.name === 'first-contentful-paint' && | ||
entry.startTime < firstHidden.timeStamp && | ||
entry.startTime < FCP_MAXIMUM_DELAY | ||
) | ||
if (fcpEntry) { | ||
callback(fcpEntry.startTime) | ||
} | ||
const performanceSubscription = createPerformanceObservable(configuration, { | ||
type: RumPerformanceEntryType.PAINT, | ||
buffered: true, | ||
}).subscribe((entries) => { | ||
const fcpEntry = find( | ||
entries, | ||
(entry): entry is RumPerformancePaintTiming => | ||
entry.name === 'first-contentful-paint' && | ||
entry.startTime < firstHidden.timeStamp && | ||
entry.startTime < FCP_MAXIMUM_DELAY | ||
) | ||
if (fcpEntry) { | ||
callback(fcpEntry.startTime) | ||
} | ||
) | ||
}) | ||
return { | ||
stop: unsubscribeLifeCycle, | ||
stop: performanceSubscription.unsubscribe, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters