From 41c9c35bdc71b0257eddffe9195a412f03dad064 Mon Sep 17 00:00:00 2001 From: allen <294931812@qq.com> Date: Thu, 9 Dec 2021 14:58:10 +0800 Subject: [PATCH] =?UTF-8?q?perf(ccp):=20=E8=B0=83=E6=95=B4customContentful?= =?UTF-8?q?Paint=E7=94=B1=EF=BC=8C=E5=AE=8F=E4=BB=BB=E5=8A=A1=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=BE=AE=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/web-performance/src/index.ts | 4 ++- .../web-performance/src/metrics/getCCP.ts | 25 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/web-performance/src/index.ts b/packages/web-performance/src/index.ts index 565270b7..c20ca2bd 100644 --- a/packages/web-performance/src/index.ts +++ b/packages/web-performance/src/index.ts @@ -126,7 +126,9 @@ class WebVitals implements IWebVitals { } customContentfulPaint() { - setTimeout(() => WebVitals.dispatchCustomEvent()) + Promise.resolve().then(() => { + WebVitals.dispatchCustomEvent() + }) } } diff --git a/packages/web-performance/src/metrics/getCCP.ts b/packages/web-performance/src/metrics/getCCP.ts index 9a86b59c..d7ad0cbf 100644 --- a/packages/web-performance/src/metrics/getCCP.ts +++ b/packages/web-performance/src/metrics/getCCP.ts @@ -25,10 +25,8 @@ let isDone = false let reportLock = true const storeMetrics = (name, value, store) => { - if (value < getFirstHiddenTime().timeStamp) { - const metrics = { name, value } - store.set(name, metrics) - } + const metrics = { name, value } + store.set(name, metrics) } const computeCCPAndRL = (store) => { @@ -95,15 +93,21 @@ const afterHandler = (url, apiConfig, store, needCCP, hashHistory) => { if (isIncludeArr(remoteQueue.queue, completeQueue) && !remoteQueue.hasStoreMetrics) { console.log('api list = ', remoteQueue.queue) remoteQueue.hasStoreMetrics = true - storeMetrics(metricsName.ACT, performance.now(), store) - computeCCPAndRL(store) + const now = performance.now() + if (now < getFirstHiddenTime().timeStamp) { + storeMetrics(metricsName.ACT, now, store) + computeCCPAndRL(store) + } } } else { if (isIncludeArr(remoteQueue.queue, completeQueue) && !remoteQueue.hasStoreMetrics && isDone && needCCP) { console.log('api list = ', remoteQueue.queue) remoteQueue.hasStoreMetrics = true - storeMetrics(metricsName.ACT, performance.now(), store) - computeCCPAndRL(store) + const now = performance.now() + if (now < getFirstHiddenTime().timeStamp) { + storeMetrics(metricsName.ACT, now, store) + computeCCPAndRL(store) + } } } } @@ -165,7 +169,10 @@ export const initCCP = ( if (!firstVisitedState) { isDone = true if (isPerformanceSupported()) { - computeCCPAndRL(store) + const now = performance.now() + if (now < getFirstHiddenTime().timeStamp) { + computeCCPAndRL(store) + } } } },