Skip to content

Commit 9799852

Browse files
PerformanceResourceTiming.contentType - add docs (mdn#35235)
* PerformanceResourceTimeing.contentType - add docs * Make it more clear that content type is not same as mime type of file in some cases * Update files/en-us/web/api/performanceresourcetiming/contenttype/index.md --------- Co-authored-by: sideshowbarker <[email protected]>
1 parent e03b13c commit 9799852

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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}}

files/en-us/web/api/performanceresourcetiming/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ The interface supports the following timestamp properties which you can see in t
8484

8585
Additionally, this interface exposes the following properties containing more information about a resource:
8686

87+
- {{domxref("PerformanceResourceTiming.contentType")}} {{ReadOnlyInline}}
88+
- : A string representing a minimized and standardized version of the MIME-type of the fetched resource.
8789
- {{domxref('PerformanceResourceTiming.decodedBodySize')}} {{ReadOnlyInline}}
8890
- : A number that is the size (in octets) received from the fetch (HTTP or cache) of the message body, after removing any applied content encoding.
91+
- {{domxref("PerformanceResourceTiming.deliveryType")}} {{experimental_inline}} {{ReadOnlyInline}}
92+
- : Indicates how the resource was delivered — for example from the cache or from a navigational prefetch.
8993
- {{domxref('PerformanceResourceTiming.encodedBodySize')}} {{ReadOnlyInline}}
9094
- : A number representing the size (in octets) received from the fetch (HTTP or cache), of the payload body, before removing any applied content encodings.
9195
- {{domxref('PerformanceResourceTiming.initiatorType')}} {{ReadOnlyInline}}
@@ -100,8 +104,6 @@ Additionally, this interface exposes the following properties containing more in
100104
- : A number representing the size (in octets) of the fetched resource. The size includes the response header fields plus the response payload body.
101105
- {{domxref('PerformanceResourceTiming.serverTiming')}} {{ReadOnlyInline}}
102106
- : An array of {{domxref("PerformanceServerTiming")}} entries containing server timing metrics.
103-
- {{domxref("PerformanceResourceTiming.deliveryType")}} {{experimental_inline}} {{ReadOnlyInline}}
104-
- : Indicates how the resource was delivered — for example from the cache or from a navigational prefetch.
105107

106108
## Instance methods
107109

0 commit comments

Comments
 (0)