PerformanceResourceTiming: nextHopProtocol 属性

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有浏览器中可用。

注意:此功能在 Web Workers 中可用。

nextHopProtocol 只读属性是一个字符串,表示获取资源的使用的网络协议,该协议由 ALPN 协议标识符 (RFC7301) 识别。

当使用代理时,如果建立了隧道连接,此属性将返回隧道协议的 ALPN 协议标识符。否则,此属性将返回与代理的第一个跳的 ALPN 协议标识符。

nextHopProtocol 属性可以具有以下值

  • 一个字符串,表示获取资源的使用的网络协议,该协议由 ALPN 协议标识符 (RFC7301) 识别。典型值包括
    • "http/0.9"
    • "http/1.0"
    • "http/1.1"
    • "h2"
    • "h2c"
    • "h3"
  • 如果资源是跨域请求且未使用 Timing-Allow-Origin HTTP 响应头,则返回空字符串。

示例

记录不使用 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.org.cn 查看网络协议信息,跨域资源应发送

http
Timing-Allow-Origin: https://mdn.org.cn

规范

规范
资源时序
# dom-performanceresourcetiming-nexthopprotocol

浏览器兼容性

另见