PerformanceResourceTiming: redirectEnd 属性

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本使用。自 2017 年 9 月以来,它已在浏览器中提供。

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

redirectEnd 只读属性返回一个 timestamp,该时间戳表示在接收到最后一个重定向的最后一个字节的响应后立即记录的时间。

在获取资源时,如果存在多个 HTTP 重定向,并且其中任何重定向的来源与当前文档不同,并且每个重定向资源的“时序允许检查”算法都通过了,则此属性将返回最后一个重定向的响应最后一个字节接收完毕后的立即时间;否则,将返回零。

要获取重定向的数量,请参阅 PerformanceNavigationTiming.redirectCount

redirectEnd 属性可以具有以下值

  • 一个 timestamp,表示在接收到最后一个重定向的最后一个字节的响应后立即记录的时间。
  • 如果没有重定向,则为 0
  • 如果资源是跨域请求且未使用 Timing-Allow-Origin HTTP 响应头,则返回 0

示例

测量重定向时间

redirectEndredirectStart 属性可用于测量重定向所花费的时间。

js
const redirect = entry.redirectEnd - entry.redirectStart;

使用 PerformanceObserver 的示例,它会在浏览器性能时间线中记录新的 resource 性能条目时通知。使用 buffered 选项可以访问观察者创建之前的条目。

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    const redirect = entry.redirectEnd - entry.redirectStart;
    if (redirect > 0) {
      console.log(`${entry.name}: Redirect time: ${redirect}ms`);
    }
  });
});

observer.observe({ type: "resource", buffered: true });

使用 Performance.getEntriesByType() 的示例,它只显示在调用此方法时浏览器性能时间线中存在的 resource 性能条目

js
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
  const redirect = entry.redirectEnd - entry.redirectStart;
  if (redirect > 0) {
    console.log(`${entry.name}: Redirect time: ${redirect}ms`);
  }
});

跨域计时信息

如果 redirectEnd 属性的值为 0,则该资源可能是跨域请求。为了允许查看跨域时序信息,需要设置 Timing-Allow-Origin HTTP 响应头。

例如,要允许 https://mdn.org.cn 查看时序资源,跨域资源应发送

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

规范

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

浏览器兼容性

另见