PerformanceNavigationTiming: activationStart 属性
activationStart 只读属性表示文档开始预渲染到其被激活之间的时间。
值
一个 DOMHighResTimeStamp,表示文档预渲染开始到激活之间的时间差,单位为毫秒。
如果页面尚未预渲染或仍在预渲染中,则值为 0。
示例
检测预渲染页面
当预渲染的文档被激活时,activationStart 会被设置为当前时间。以下函数可以检查页面是否正在 预渲染 或已预渲染。
js
function pagePrerendered() {
return (
document.prerendering ||
self.performance?.getEntriesByType?.("navigation")[0]?.activationStart > 0
);
}
衡量用户感知的性能里程碑
对于预渲染页面,页面可能在实际导航到之前很久就已创建。在预渲染页面上使用 Performance API 时,将返回值与 activationStart 进行比较至关重要,以避免误导性的测量。
js
// Time to when activation occurred
let activationStart =
performance.getEntriesByType("navigation")[0].activationStart;
// Time to first paint
let firstPaint = performance.getEntriesByName("first-paint")[0].startTime;
// Time to first contentful paint
let firstContentfulPaint = performance.getEntriesByName(
"first-contentful-paint",
)[0].startTime;
console.log(`time to first paint: ${firstPaint - activationStart}`);
console.log(
`time to first-contentful-paint: ${firstContentfulPaint - activationStart}`,
);
规范
| 规范 |
|---|
| 预渲染改版 # performance-navigation-timing-extension |
浏览器兼容性
加载中…