Skip to content

Commit

Permalink
perf: add ccp report timing when page unload
Browse files Browse the repository at this point in the history
  • Loading branch information
Chryseis committed Jan 13, 2022
1 parent 440893e commit 7f4964b
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions packages/web-performance/src/metrics/getCCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { proxyFetch, proxyXhr } from '../lib/proxyHandler'
import getFirstVisitedState from '../lib/getFirstVisitedState'
import metricsStore from '../lib/store'
import { IReportHandler, IScoreConfig } from '../types'
import { getApiPath, isIncludeArr, isEqualArr, isExistPath } from '../utils'
import { getApiPath, isIncludeArr, isEqualArr, isExistPath, beforeUnload } from '../utils'
import getPath from '../utils/getPath'
import { isPerformanceSupported } from '../utils/isSupported'
import { metricsName } from '../constants'
Expand Down Expand Up @@ -132,34 +132,32 @@ const afterHandler = (url, apiConfig, store, hashHistory, excludeRemotePath, sco
}
}

const reportMetrics = (store: metricsStore, report, immediately) => {
if (immediately) {
if (reportLock) {
const act = store.get(metricsName.ACT)
const ccp = store.get(metricsName.CCP)
const rl = store.get(metricsName.RL)

if (act && ccp) {
if (act.value.time < ccp.value) {
report(act)
report(ccp)

if (rl) {
report(rl)
}
}
}
const reportMetrics = (store: metricsStore, report) => {
if (reportLock) {
const act = store.get(metricsName.ACT)
const ccp = store.get(metricsName.CCP)
const rl = store.get(metricsName.RL)

if (!act && ccp) {
if (act && ccp) {
if (act.value.time < ccp.value) {
report(act)
report(ccp)

if (rl) {
report(rl)
}
}
}

if (!act && ccp) {
report(ccp)

reportLock = false
if (rl) {
report(rl)
}
}

reportLock = false
}
}

Expand Down Expand Up @@ -212,11 +210,15 @@ export const initCCP = (
{ once: true, capture: true }
)

onHidden(() => reportMetrics(store, report, immediately), true)
if (immediately) {
beforeUnload(() => reportMetrics(store, report))

onPageChange(() => reportMetrics(store, report, immediately))
onHidden(() => reportMetrics(store, report), true)

maxWaitTime4Report(() => reportMetrics(store, report, immediately), maxWaitCCPDuration)
onPageChange(() => reportMetrics(store, report))

maxWaitTime4Report(() => reportMetrics(store, report), maxWaitCCPDuration)
}

proxyXhr(
(url) => beforeHandler(url, apiConfig, hashHistory, excludeRemotePath),
Expand Down

0 comments on commit 7f4964b

Please sign in to comment.