PerformanceResourceTiming: redirectEnd 屬性
注意:此功能在 Web Workers 中可用。
redirectEnd 只讀屬性返回一個 timestamp,該時間戳表示在接收到最後一個重定向的最後一個位元組的響應後立即記錄的時間。
在獲取資源時,如果存在多個 HTTP 重定向,並且其中任何重定向的來源與當前文件不同,並且每個重定向資源的“時序允許檢查”演算法都通過了,則此屬性將返回最後一個重定向的響應最後一個位元組接收完畢後的立即時間;否則,將返回零。
要獲取重定向的數量,請參閱 PerformanceNavigationTiming.redirectCount。
值
redirectEnd 屬性可以具有以下值
- 一個
timestamp,表示在接收到最後一個重定向的最後一個位元組的響應後立即記錄的時間。 - 如果沒有重定向,則為
0。 - 如果資源是跨域請求且未使用
Timing-Allow-OriginHTTP 響應頭,則返回0。
示例
測量重定向時間
redirectEnd 和 redirectStart 屬性可用於測量重定向所花費的時間。
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.club.tw 檢視時序資源,跨域資源應傳送
http
Timing-Allow-Origin: https://mdn.club.tw
規範
| 規範 |
|---|
| 資源時序 # dom-performanceresourcetiming-redirectend |
瀏覽器相容性
載入中…