Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Discard loading time when page is hidden #2965

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
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,9 +17,15 @@ 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) {
if (
firstHidden.timeStamp !== 0 &&
!isWaitingForActivityLoadingTime &&
!isWaitingForLoadEvent &&
loadingTimeCandidates.length > 0
) {
callback(Math.max(...loadingTimeCandidates) as Duration)
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -34,7 +41,10 @@ export function trackLoadingTime(
})

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