PerformanceResourceTiming: encodedBodySize 屬性
注意:此功能在 Web Workers 中可用。
encodedBodySize 只讀屬性表示在移除任何應用的 Content-Encoding(例如 gzip 或 Brotli)之前,從 fetch(HTTP 或快取)接收到的有效負載主體的大小(以位元組為單位)。如果資源是從應用程式快取或本地資源檢索的,則必須返回移除任何應用的 Content-Encoding 之前的有效負載主體的大小。
值
encodedBodySize 屬性可以具有以下值
- 一個數字,表示在移除任何應用的 Content-Encoding 之前,從 fetch(HTTP 或快取)接收到的有效負載主體的大小(以位元組為單位)。
- 如果資源是跨域請求且未使用
Timing-Allow-OriginHTTP 響應頭,則返回0。
示例
檢查內容是否被壓縮
如果 encodedBodySize 和 decodedBodySize 屬性是非空的且不相等,則表示內容被壓縮了(例如,gzip 或 Brotli)。
使用 PerformanceObserver 的示例,它會在瀏覽器效能時間線中記錄新的 resource 效能條目時通知。使用 buffered 選項可以訪問觀察者建立之前的條目。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const uncompressed =
entry.decodedBodySize && entry.decodedBodySize === entry.encodedBodySize;
if (uncompressed) {
console.log(`${entry.name} was not compressed!`);
}
});
});
observer.observe({ type: "resource", buffered: true });
使用 Performance.getEntriesByType() 的示例,它只顯示在呼叫此方法時瀏覽器效能時間線中存在的 resource 效能條目
js
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
const uncompressed =
entry.decodedBodySize && entry.decodedBodySize === entry.encodedBodySize;
if (uncompressed) {
console.log(`${entry.name} was not compressed!`);
}
});
跨域內容大小資訊
如果 encodedBodySize 屬性的值為 0,則該資源可能是跨域請求。要公開跨域內容大小資訊,需要設定 Timing-Allow-Origin HTTP 響應頭。
例如,要允許 https://mdn.club.tw 檢視內容大小,跨域資源應傳送
http
Timing-Allow-Origin: https://mdn.club.tw
規範
| 規範 |
|---|
| 資源時序 # dom-performanceresourcetiming-encodedbodysize |
瀏覽器相容性
載入中…