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

浏览器兼容性

另见