PerformanceResourceTiming: redirectStart 屬性
注意:此功能在 Web Workers 中可用。
redirectStart 只讀屬性返回一個 timestamp,表示啟動重定向的獲取操作的開始時間。
如果在獲取資源時發生 HTTP 重定向,並且其中任何重定向不與當前文件同源,但對每個重定向的資源都通過了 timing allow check 演算法,則此屬性將返回啟動重定向的獲取操作的開始時間;否則,將返回零。
要獲取重定向次數,請參閱 PerformanceNavigationTiming.redirectCount。
值
redirectStart 屬性可以具有以下值:
- 一個
timestamp,表示啟動重定向的獲取操作的開始時間。 - 如果沒有重定向,則為
0。 - 如果資源是跨域請求且未使用
Timing-Allow-OriginHTTP 響應頭,則返回0。
示例
測量重定向時間
redirectStart 和 redirectEnd 屬性可用於測量重定向所需的時間。
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`);
}
});
跨域計時資訊
如果 redirectStart 屬性的值為 0,則該資源可能是跨域請求。為了能夠檢視跨域時序資訊,需要設定 Timing-Allow-Origin HTTP 響應頭。
例如,要允許 https://mdn.club.tw 檢視時序資源,跨域資源應傳送
http
Timing-Allow-Origin: https://mdn.club.tw
規範
| 規範 |
|---|
| 資源時序 # dom-performanceresourcetiming-redirectstart |
瀏覽器相容性
載入中…