|
| 1 | +--- |
| 2 | +title: "PerformanceResourceTiming: contentType property" |
| 3 | +short-title: contentType |
| 4 | +slug: Web/API/PerformanceResourceTiming/contentType |
| 5 | +page-type: web-api-instance-property |
| 6 | +browser-compat: api.PerformanceResourceTiming.contentType |
| 7 | +--- |
| 8 | + |
| 9 | +{{APIRef("Performance API")}} |
| 10 | + |
| 11 | +The **`contentType`** read-only property of the {{domxref("PerformanceResourceTiming")}} interface is a string indicating the content type of the fetched resource, formatted as a {{glossary("MIME type")}} and subtype separated by a forward slash. |
| 12 | + |
| 13 | +The content type is a minimized and "standardized" version of the MIME type that is extracted from the {{httpheader("Content-Type")}} HTTP header sent in the resource's fetch response. |
| 14 | +For JavaScript, JSON, SVG, and XML, the MIME type is replaced by a representative MIME type/subtype string. |
| 15 | +Other types supported by the browser are represented by the MIME type/subtype string in the header (other information in the header is discarded). |
| 16 | + |
| 17 | +## Value |
| 18 | + |
| 19 | +A string indicating the MIME type "essence" of the content. |
| 20 | +This may be one of the following values: |
| 21 | + |
| 22 | +- `text/javascript` |
| 23 | + - : JavaScript content. |
| 24 | +- `application/json` |
| 25 | + - : JSON content. |
| 26 | +- `image/svg+xml` |
| 27 | + - : SVG content. |
| 28 | +- `application/xml` |
| 29 | + - : XML content (other than SVG). |
| 30 | +- MIME type/subtype |
| 31 | + - : Any other MIME type/subtype supported by the user agent. |
| 32 | +- `""` (empty string) |
| 33 | + - : Returned for MIME types that are not supported by the browser, or if the resource fetch failed due to [CORS](/en-US/docs/Web/HTTP/CORS) checks. |
| 34 | + |
| 35 | +## Examples |
| 36 | + |
| 37 | +### Filtering resources |
| 38 | + |
| 39 | +The `contentType` property can be used to get specific resource timing entries only; for example, only those related to scripts. |
| 40 | + |
| 41 | +The following example uses a {{domxref("PerformanceObserver")}} to notify of new `resource` performance entries as they are recorded in the browser's performance timeline. |
| 42 | +The `buffered` option is used for accessing entries from before the observer creation. |
| 43 | + |
| 44 | +```js |
| 45 | +const observer = new PerformanceObserver((list) => { |
| 46 | + const javascriptResources = list.getEntries().filter((entry) => { |
| 47 | + return entry.contentType === "text/javascript"; |
| 48 | + }); |
| 49 | + console.log(javascriptResources); |
| 50 | +}); |
| 51 | + |
| 52 | +observer.observe({ type: "resource", buffered: true }); |
| 53 | +``` |
| 54 | + |
| 55 | +The following example uses {{domxref("Performance.getEntriesByType()")}}, which only shows `resource` performance entries present in the browser's performance timeline at the time you call the method. |
| 56 | + |
| 57 | +```js |
| 58 | +const scripts = performance.getEntriesByType("resource").filter((entry) => { |
| 59 | + return entry.contentType === "text/javascript"; |
| 60 | +}); |
| 61 | +console.log(scripts); |
| 62 | +``` |
| 63 | + |
| 64 | +## Specifications |
| 65 | + |
| 66 | +{{Specifications}} |
| 67 | + |
| 68 | +## Browser compatibility |
| 69 | + |
| 70 | +{{Compat}} |
0 commit comments