Skip to content

Commit

Permalink
✨ Discard loading time when page is hidden (#2965)
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Boutaib authored Sep 5, 2024
1 parent b89d9d1 commit fe822e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RelativeTime, Duration } from '@datadog/browser-core'
import { addDuration, clocksOrigin, Observable } from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock } from '@datadog/browser-core/test'
import { mockClock, setPageVisibility, restorePageVisibility } from '@datadog/browser-core/test'
import { ViewLoadingType } from '../../../rawRumEvent.types'
import { createPerformanceEntry } from '../../../../test'
import { PAGE_ACTIVITY_END_DELAY, PAGE_ACTIVITY_VALIDATION_DELAY } from '../../waitPageActivityEnd'
Expand Down Expand Up @@ -47,6 +47,7 @@ describe('trackLoadingTime', () => {

afterEach(() => {
stopLoadingTimeTracking()
restorePageVisibility()
clock.cleanup()
})

Expand Down Expand Up @@ -125,4 +126,11 @@ describe('trackLoadingTime', () => {

expect(loadingTimeCallback).toHaveBeenCalledOnceWith(addDuration(BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY, CLOCK_GAP))
})

it('should discard loading time if page is hidden before activity', () => {
setPageVisibility('hidden')
startLoadingTimeTracking()

expect(loadingTimeCallback).not.toHaveBeenCalled()
})
})
12 changes: 10 additions & 2 deletions packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { waitPageActivityEnd } from '../../waitPageActivityEnd'
import type { RumConfiguration } from '../../configuration'
import type { LifeCycle } from '../../lifeCycle'
import { ViewLoadingType } from '../../../rawRumEvent.types'
import { trackFirstHidden } from './trackFirstHidden'

export function trackLoadingTime(
lifeCycle: LifeCycle,
Expand All @@ -16,10 +17,14 @@ export function trackLoadingTime(
let isWaitingForLoadEvent = loadType === ViewLoadingType.INITIAL_LOAD
let isWaitingForActivityLoadingTime = true
const loadingTimeCandidates: Duration[] = []
const firstHidden = trackFirstHidden(configuration)

function invokeCallbackIfAllCandidatesAreReceived() {
if (!isWaitingForActivityLoadingTime && !isWaitingForLoadEvent && loadingTimeCandidates.length > 0) {
callback(Math.max(...loadingTimeCandidates) as Duration)
const loadingTime = Math.max(...loadingTimeCandidates)
if (loadingTime < firstHidden.timeStamp) {
callback(loadingTime as Duration)
}
}
}

Expand All @@ -34,7 +39,10 @@ export function trackLoadingTime(
})

return {
stop,
stop: () => {
stop()
firstHidden.stop()
},
setLoadEvent: (loadEvent: Duration) => {
if (isWaitingForLoadEvent) {
isWaitingForLoadEvent = false
Expand Down

0 comments on commit fe822e0

Please sign in to comment.