PerformanceResourceTiming: nextHopProtocol 屬性
注意:此功能在 Web Workers 中可用。
nextHopProtocol 只讀屬性是一個字串,表示獲取資源的使用的網路協議,該協議由 ALPN 協議識別符號 (RFC7301) 識別。
當使用代理時,如果建立了隧道連線,此屬性將返回隧道協議的 ALPN 協議識別符號。否則,此屬性將返回與代理的第一個跳的 ALPN 協議識別符號。
值
nextHopProtocol 屬性可以具有以下值
- 一個字串,表示獲取資源的使用的網路協議,該協議由 ALPN 協議識別符號 (RFC7301) 識別。典型值包括
"http/0.9""http/1.0""http/1.1""h2""h2c""h3"
- 如果資源是跨域請求且未使用
Timing-Allow-OriginHTTP 響應頭,則返回空字串。
示例
記錄不使用 HTTP/2 或 HTTP/3 的資源
nextHopProtocol 屬性可用於檢視未使用 HTTP/2 或 HTTP/3 協議的資源。
使用 PerformanceObserver 的示例,它會在瀏覽器效能時間線中記錄新的 resource 效能條目時通知。使用 buffered 選項可以訪問觀察者建立之前的條目。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const protocol = entry.nextHopProtocol;
if (protocol && !(protocol === "h2" || protocol === "h3")) {
console.log(`${entry.name} uses ${protocol}.`);
}
});
});
observer.observe({ type: "resource", buffered: true });
使用 Performance.getEntriesByType() 的示例,它只顯示在呼叫此方法時瀏覽器效能時間線中存在的 resource 效能條目
js
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
const protocol = entry.nextHopProtocol;
if (protocol && !(protocol === "h2" || protocol === "h3")) {
console.log(`${entry.name} uses ${protocol}.`);
}
});
跨域網路協議資訊
如果 nextHopProtocol 屬性的值為空字串,則該資源可能是跨域請求。要公開跨域網路協議資訊,需要設定 Timing-Allow-Origin HTTP 響應頭。
例如,要允許 https://mdn.club.tw 檢視網路協議資訊,跨域資源應傳送
http
Timing-Allow-Origin: https://mdn.club.tw
規範
| 規範 |
|---|
| 資源時序 # dom-performanceresourcetiming-nexthopprotocol |
瀏覽器相容性
載入中…