Chrome 64 版即將淘汰 chrome.loadTimes() API

chrome.loadTimes() 是非標準 API,可向開發人員公開載入指標和網路資訊,協助他們進一步瞭解網站在實際環境中的成效。

因為這個 API 是在 2009 年實作,所以它回報的所有實用資訊都可以在標準化 API 中找到,例如:

這些標準化 API 是由多個瀏覽器供應商實作。因此,chrome.loadTimes() 已在 Chrome 64 中淘汰。

已淘汰的 API

chrome.loadTimes() 函式會傳回單一物件,其中包含所有載入和網路資訊。舉例來說,下列物件是在 www.google.com 上呼叫 chrome.loadTimes() 的結果:

{
  "requestTime": 1513186741.847,
  "startLoadTime": 1513186741.847,
  "commitLoadTime": 1513186742.637,
  "finishDocumentLoadTime": 1513186742.842,
  "finishLoadTime": 1513186743.582,
  "firstPaintTime": 1513186742.829,
  "firstPaintAfterLoadTime": 0,
  "navigationType": "Reload",
  "wasFetchedViaSpdy": true,
  "wasNpnNegotiated": true,
  "npnNegotiatedProtocol": "h2",
  "wasAlternateProtocolAvailable": false,
  "connectionInfo": "h2"
}

標準化替換

您現在可以使用標準化 API 找出上述每個值。下表將每個值與其標準化 API 進行比對,下文會提供程式碼範例,說明如何透過舊版 API 取得每個值的現代等效值。

chrome.loadTimes() 功能標準化 API 替代方案
requestTime Navigation Timing 2
startLoadTime Navigation 計時 2
commitLoadTime Navigation Timing 2
finishDocumentLoadTime Navigation Timing 2
finishLoadTime Navigation Timing 2
firstPaintTime 繪製時間
firstPaintAfterLoadTime 不適用
navigationType Navigation Timing 2
wasFetchedViaSpdy Navigation Timing 2
wasNpnNegotiated Navigation Timing 2
npnNegotiatedProtocol Navigation Timing 2
wasAlternateProtocolAvailable 不適用
connectionInfo Navigation 計時 2

以下程式碼範例會傳回與 chrome.loadTimes() 傳回的值相同的值。不過,對於新程式碼,我們不建議使用這些程式碼範例。原因是 chrome.loadTimes() 會以 epoch 時間以秒為單位提供時間值,而新的成效 API 通常會以毫秒為單位,根據網頁的時間來源回報值,這對成效分析來說通常更實用。

其中幾個範例也偏好使用 Performance Timeline 2 API (例如 performance.getEntriesByType()),但會為較舊的 Navigation Timing 1 API 提供備用方案,因為後者支援的瀏覽器較多。日後,我們會優先採用 Performance Timeline API,且通常會以更精確的方式回報。

requestTime

function requestTime() {
  // If the browser supports the Navigation Timing 2 and HR Time APIs, use
  // them, otherwise fall back to the Navigation Timing 1 API.
  if (window.PerformanceNavigationTiming && performance.timeOrigin) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return (ntEntry.startTime + performance.timeOrigin) / 1000;
  } else {
    return performance.timing.navigationStart / 1000;
  }
}

startLoadTime

function startLoadTime() {
  // If the browser supports the Navigation Timing 2 and HR Time APIs, use
  // them, otherwise fall back to the Navigation Timing 1 API.
  if (window.PerformanceNavigationTiming && performance.timeOrigin) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return (ntEntry.startTime + performance.timeOrigin) / 1000;
  } else {
    return performance.timing.navigationStart / 1000;
  }
}

commitLoadTime

function commitLoadTime() {
  // If the browser supports the Navigation Timing 2 and HR Time APIs, use
  // them, otherwise fall back to the Navigation Timing 1 API.
  if (window.PerformanceNavigationTiming && performance.timeOrigin) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return (ntEntry.responseStart + performance.timeOrigin) / 1000;
  } else {
    return performance.timing.responseStart / 1000;
  }
}

finishDocumentLoadTime

function finishDocumentLoadTime() {
  // If the browser supports the Navigation Timing 2 and HR Time APIs, use
  // them, otherwise fall back to the Navigation Timing 1 API.
  if (window.PerformanceNavigationTiming && performance.timeOrigin) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return (ntEntry.domContentLoadedEventEnd + performance.timeOrigin) / 1000;
  } else {
    return performance.timing.domContentLoadedEventEnd / 1000;
  }
}

finishLoadTime

function finishLoadTime() {
  // If the browser supports the Navigation Timing 2 and HR Time APIs, use
  // them, otherwise fall back to the Navigation Timing 1 API.
  if (window.PerformanceNavigationTiming && performance.timeOrigin) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return (ntEntry.loadEventEnd + performance.timeOrigin) / 1000;
  } else {
    return performance.timing.loadEventEnd / 1000;
  }
}

firstPaintTime

function firstPaintTime() {
  if (window.PerformancePaintTiming) {
    const fpEntry = performance.getEntriesByType('paint')[0];
    return (fpEntry.startTime + performance.timeOrigin) / 1000;
  }
}

firstPaintAfterLoadTime

function firstPaintTimeAfterLoad() {
  // This was never actually implemented and always returns 0.
  return 0;
}
function navigationType() {
  if (window.PerformanceNavigationTiming) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return ntEntry.type;
  }
}

wasFetchedViaSpdy

function wasFetchedViaSpdy() {
  // SPDY is deprecated in favor of HTTP/2, but this implementation returns
  // true for HTTP/2 or HTTP2+QUIC/39 as well.
  if (window.PerformanceNavigationTiming) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return ['h2', 'hq'].includes(ntEntry.nextHopProtocol);
  }
}

wasNpnNegotiated

function wasNpnNegotiated() {
  // NPN is deprecated in favor of ALPN, but this implementation returns true
  // for HTTP/2 or HTTP2+QUIC/39 requests negotiated via ALPN.
  if (window.PerformanceNavigationTiming) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return ['h2', 'hq'].includes(ntEntry.nextHopProtocol);
  }
}

npnNegotiatedProtocol

function npnNegotiatedProtocol() {
  // NPN is deprecated in favor of ALPN, but this implementation returns the
  // HTTP/2 or HTTP2+QUIC/39 requests negotiated via ALPN.
  if (window.PerformanceNavigationTiming) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return ['h2', 'hq'].includes(ntEntry.nextHopProtocol) ?
        ntEntry.nextHopProtocol : 'unknown';
  }
}

wasAlternateProtocolAvailable

function wasAlternateProtocolAvailable() {
  // The Alternate-Protocol header is deprecated in favor of Alt-Svc
  // (https://www.mnot.net/blog/2016/03/09/alt-svc), so technically this
  // should always return false.
  return false;
}

connectionInfo

function connectionInfo() {
  if (window.PerformanceNavigationTiming) {
    const ntEntry = performance.getEntriesByType('navigation')[0];
    return ntEntry.nextHopProtocol;
  }
}

移除計畫

chrome.loadTimes() API 將於 Chrome 64 淘汰,並預計於 2018 年底移除。開發人員應盡快遷移程式碼,以免資料遺失。

意圖淘汰 | Chromestatus Tracker | Chromium Bug