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

瀏覽器相容性

另見