PerformanceResourceTiming: contentType 屬性
注意:此功能在 Web Workers 中可用。
PerformanceResourceTiming 介面的只讀屬性 contentType 是一個字串,表示已獲取資源的 MIME 型別,格式為以斜槓分隔的 MIME 型別 和子型別。
此內容型別是 MIME 型別的精簡和“標準化”版本,該 MIME 型別從資源獲取響應中傳送的 Content-Type HTTP 標頭中提取。對於 JavaScript、JSON、SVG 和 XML,MIME 型別會被替換為代表性的 MIME 型別/子型別字串。瀏覽器支援的其他型別則由標頭中的 MIME 型別/子型別字串表示(標頭中的其他資訊將被丟棄)。
值
一個字串,表示內容的 MIME 型別“本質”。這可能是以下值之一:
text/javascript-
JavaScript 內容。
application/json-
JSON 內容。
image/svg+xml-
SVG 內容。
application/xml-
XML 內容(SVG 除外)。
- MIME 型別/子型別
-
使用者代理支援的任何其他 MIME 型別/子型別。
""(空字串)-
當 MIME 型別不受瀏覽器支援時,或當資源獲取因 CORS 檢查而失敗時返回。
示例
過濾資源
contentType 屬性可用於僅獲取特定的資源計時條目;例如,僅獲取與指令碼相關的條目。
以下示例使用 PerformanceObserver 來通知新記錄在瀏覽器效能時間線中的 resource 效能條目。buffered 選項用於訪問觀察者建立之前的條目。
js
const observer = new PerformanceObserver((list) => {
const javascriptResources = list
.getEntries()
.filter((entry) => entry.contentType === "text/javascript");
console.log(javascriptResources);
});
observer.observe({ type: "resource", buffered: true });
以下示例使用 Performance.getEntriesByType(),它僅顯示在呼叫方法時存在於瀏覽器效能時間線中的 resource 效能條目。
js
const scripts = performance
.getEntriesByType("resource")
.filter((entry) => entry.contentType === "text/javascript");
console.log(scripts);
規範
| 規範 |
|---|
| 資源時序 # dom-performanceresourcetiming-contenttype |
瀏覽器相容性
載入中…