PerformanceNavigationTiming: notRestoredReasons 屬性

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

PerformanceNavigationTiming 介面的只讀屬性 notRestoredReasons 返回一個 NotRestoredReasons 物件,該物件提供有關當前文件因何種原因被阻止使用後退/前進快取(bfcache)進行導航的報告資料。

當關聯的 PerformanceNavigationTiming 物件表示歷史導航時,notRestoredReasons 返回一個 NotRestoredReasons 物件。

PerformanceNavigationTiming 物件不表示歷史導航時,notRestoredReasons 將返回 null。這有助於確定 bfcache 是否與特定導航無關(而不是 notRestoredReasons 不被支援,在這種情況下它會返回 undefined)。

注意: 儘管導航型別被報告為後退/前進導航,notRestoredReasons 仍可能返回 null。這些情況包括在新標籤頁中複製後退/前進導航,以及在瀏覽器重啟後恢復後退/前進導航標籤頁。在這種情況下,某些瀏覽器會從原始標籤頁複製導航型別,但由於這些實際上不是後退/前進導航,因此 notRestoredReasons 返回 null

示例

可以透過 Performance.getEntriesByType()PerformanceObserver 從效能時間線獲取 PerformanceNavigationTiming 資料。

例如,您可以呼叫以下函式來返回當前存在於效能時間線中的所有 PerformanceNavigationTiming 物件,並記錄它們的 notRestoredReasons

js
function returnNRR() {
  const navEntries = performance.getEntriesByType("navigation");
  for (let i = 0; i < navEntries.length; i++) {
    console.log(`Navigation entry ${i}`);
    let navEntry = navEntries[i];
    console.log(navEntry.notRestoredReasons);
  }
}

PerformanceNavigationTiming.notRestoredReasons 屬性返回一個具有以下結構的 物件,該物件提供了當前文件因何種原因被阻止使用 bfcache 的原因。在此示例中,頂層框架沒有嵌入的子 <iframe>

json
{
  "children": [],
  "id": null,
  "name": null,
  "reasons": [{ "reason": "unload-listener" }],
  "src": "",
  "url": "example.com"
}

規範

規範
導航計時 Level 2
# dom-performancenavigationtiming-notrestoredreasons

瀏覽器相容性

另見