Skip to content

Commit

Permalink
perf(ccp): 调整customContentfulPaint由,宏任务改为微任务
Browse files Browse the repository at this point in the history
  • Loading branch information
Chryseis committed Dec 9, 2021
1 parent d826f21 commit 41c9c35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/web-performance/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class WebVitals implements IWebVitals {
}

customContentfulPaint() {
setTimeout(() => WebVitals.dispatchCustomEvent())
Promise.resolve().then(() => {
WebVitals.dispatchCustomEvent()
})
}
}

Expand Down
25 changes: 16 additions & 9 deletions packages/web-performance/src/metrics/getCCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
},
Expand Down

0 comments on commit 41c9c35

Please sign in to comment.