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 |
瀏覽器相容性
載入中…