You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 5-network/06-fetch-api/article.md
+20-20
Original file line number
Diff line number
Diff line change
@@ -42,11 +42,11 @@ We fully covered `method`, `headers` and `body` in the chapter <info:fetch>.
42
42
43
43
The `signal` option is covered in <info:fetch-abort>.
44
44
45
-
Now let's explore the rest of capabilities.
45
+
Now let's explore the remaining capabilities.
46
46
47
47
## referrer, referrerPolicy
48
48
49
-
These options govern how `fetch` sets HTTP `Referer` header.
49
+
These options govern how `fetch` sets the HTTP `Referer` header.
50
50
51
51
Usually that header is set automatically and contains the url of the page that made the request. In most scenarios, it's not important at all, sometimes, for security purposes, it makes sense to remove or shorten it.
52
52
@@ -81,18 +81,18 @@ Requests are split into 3 types:
81
81
2. Request to another origin.
82
82
3. Request from HTTPS to HTTP (from safe to unsafe protocol).
83
83
84
-
Unlike `referrer` option that allows to set the exact `Referer` value, `referrerPolicy` tells the browser general rules for each request type.
84
+
Unlike the `referrer` option that allows to set the exact `Referer` value, `referrerPolicy` tells the browser general rules for each request type.
85
85
86
86
Possible values are described in the [Referrer Policy specification](https://w3c.github.io/webappsec-referrer-policy/):
87
87
88
-
-**`"no-referrer-when-downgrade"`** -- the default value: full `Referer` is sent always, unless we send a request from HTTPS to HTTP (to less secure protocol).
88
+
-**`"no-referrer-when-downgrade"`** -- the default value: full `Referer` is always sent, unless we send a request from HTTPS to HTTP (to the less secure protocol).
89
89
-**`"no-referrer"`** -- never send `Referer`.
90
90
-**`"origin"`** -- only send the origin in `Referer`, not the full page URL, e.g. only `http://site.com` instead of `http://site.com/path`.
91
-
-**`"origin-when-cross-origin"`** -- send full `Referer` to the same origin, but only the origin part for cross-origin requests (as above).
92
-
-**`"same-origin"`** -- send full `Referer` to the same origin, but no `Referer` for cross-origin requests.
93
-
-**`"strict-origin"`** -- send only origin, don't send`Referer` for HTTPS→HTTP requests.
94
-
-**`"strict-origin-when-cross-origin"`** -- for same-origin send full `Referer`, for cross-origin send only origin, unless it's HTTPS→HTTP request, then send nothing.
95
-
-**`"unsafe-url"`** -- always send full url in `Referer`, even for HTTPS→HTTP requests.
91
+
-**`"origin-when-cross-origin"`** -- send the full `Referer` to the same origin, but only the origin part for cross-origin requests (as above).
92
+
-**`"same-origin"`** -- send the full `Referer` to the same origin, but no `Referer` for cross-origin requests.
93
+
-**`"strict-origin"`** -- send only the origin, not the`Referer` for HTTPS→HTTP requests.
94
+
-**`"strict-origin-when-cross-origin"`** -- for same-origin send the full `Referer`, for cross-origin send only the origin, unless it's HTTPS→HTTP request, then send nothing.
95
+
-**`"unsafe-url"`** -- always send the full url in `Referer`, even for HTTPS→HTTP requests.
96
96
97
97
Here's a table with all combinations:
98
98
@@ -107,13 +107,13 @@ Here's a table with all combinations:
107
107
|`"strict-origin-when-cross-origin"`| full | origin | - |
108
108
|`"unsafe-url"`| full | full | full |
109
109
110
-
Let's say we have an admin zone with URL structure that shouldn't be known from outside of the site.
110
+
Let's say we have an admin zone with a URL structure that shouldn't be known from outside of the site.
111
111
112
112
If we send a `fetch`, then by default it always sends the `Referer` header with the full url of our page (except when we request from HTTPS to HTTP, then no `Referer`).
113
113
114
114
E.g. `Referer: https://javascript.info/admin/secret/paths`.
115
115
116
-
If we'd like other websites know only the origin part, not URL-path, we can set the option:
116
+
If we'd like other websites know only the origin part, not the URL-path, we can set the option:
117
117
118
118
```js
119
119
fetch('https://another.com/page', {
@@ -129,7 +129,7 @@ Its only difference compared to the default behavior is that for requests to ano
129
129
```smart header="Referrer policy is not only for `fetch`"
130
130
Referrer policy, described in the [specification](https://w3c.github.io/webappsec-referrer-policy/), is not just for `fetch`, but more global.
131
131
132
-
In particular, it's possible to set the default policy for the whole page using `Referrer-Policy` HTTP header, or per-link, with `<a rel="noreferrer">`.
132
+
In particular, it's possible to set the default policy for the whole page using the `Referrer-Policy` HTTP header, or per-link, with `<a rel="noreferrer">`.
133
133
```
134
134
135
135
## mode
@@ -152,13 +152,13 @@ The `credentials` option specifies whether `fetch` should send cookies and HTTP-
152
152
153
153
## cache
154
154
155
-
By default, `fetch` requests make use of standard HTTP-caching. That is, it honors `Expires`, `Cache-Control` headers, sends `If-Modified-Since`, and so on. Just like regular HTTP-requests do.
155
+
By default, `fetch` requests make use of standard HTTP-caching. That is, it respects the `Expires` and `Cache-Control` headers, sends `If-Modified-Since` and so on. Just like regular HTTP-requests do.
156
156
157
157
The `cache` options allows to ignore HTTP-cache or fine-tune its usage:
158
158
159
159
- **`"default"`** -- `fetch` uses standard HTTP-cache rules and headers,
160
160
- **`"no-store"`** -- totally ignore HTTP-cache, this mode becomes the default if we set a header `If-Modified-Since`, `If-None-Match`, `If-Unmodified-Since`, `If-Match`, or `If-Range`,
161
-
- **`"reload"`** -- don't take the result from HTTP-cache (if any), but populate cache with the response (if response headers allow),
161
+
- **`"reload"`** -- don't take the result from HTTP-cache (if any), but populate the cache with the response (if the response headers permit this action),
162
162
- **`"no-cache"`** -- create a conditional request if there is a cached response, and a normal request otherwise. Populate HTTP-cache with the response,
163
163
- **`"force-cache"`** -- use a response from HTTP-cache, even if it's stale. If there's no response in HTTP-cache, make a regular HTTP-request, behave normally,
164
164
- **`"only-if-cached"`** -- use a response from HTTP-cache, even if it's stale. If there's no response in HTTP-cache, then error. Only works when `mode` is `"same-origin"`.
@@ -177,7 +177,7 @@ The `redirect` option allows to change that:
177
177
178
178
The `integrity` option allows to check if the response matches the known-ahead checksum.
179
179
180
-
As described in the [specification](https://w3c.github.io/webappsec-subresource-integrity/), supported hash-functions are SHA-256, SHA-384, and SHA-512, there might be others depending on a browser.
180
+
As described in the [specification](https://w3c.github.io/webappsec-subresource-integrity/), supported hash-functions are SHA-256, SHA-384, and SHA-512, there might be others depending on the browser.
181
181
182
182
For example, we're downloading a file, and we know that it's SHA-256 checksum is "abcdef" (a real checksum is longer, of course).
183
183
@@ -195,11 +195,11 @@ Then `fetch` will calculate SHA-256 on its own and compare it with our string. I
195
195
196
196
The `keepalive` option indicates that the request may "outlive" the webpage that initiated it.
197
197
198
-
For example, we gather statistics about how the current visitor uses our page (mouse clicks, page fragments he views), to analyze and improve user experience.
198
+
For example, we gather statistics on how the current visitor uses our page (mouse clicks, page fragments he views), to analyze and improve the user experience.
199
199
200
-
When the visitor leaves our page -- we'd like to save the data at our server.
200
+
When the visitor leaves our page -- we'd like to save the data to our server.
Normally, when a document is unloaded, all associated network requests are aborted. But `keepalive` option tells the browser to perform the request in background, even after it leaves the page. So this option is essential for our request to succeed.
216
+
Normally, when a document is unloaded, all associated network requests are aborted. But the `keepalive` option tells the browser to perform the request in the background, even after it leaves the page. So this option is essential for our request to succeed.
217
217
218
218
It has a few limitations:
219
219
220
220
- We can't send megabytes: the body limit for `keepalive` requests is 64KB.
221
221
- If we need to gather a lot of statistics about the visit, we should send it out regularly in packets, so that there won't be a lot left for the last `onunload` request.
222
222
- This limit applies to all `keepalive` requests together. In other words, we can perform multiple `keepalive` requests in parallel, but the sum of their body lengths should not exceed 64KB.
223
223
- We can't handle the server response if the document is unloaded. So in our example `fetch` will succeed due to `keepalive`, but subsequent functions won't work.
224
-
- In most cases, such as sending out statistics, it's not a problem, as server just accepts the data and usually sends an empty response to such requests.
224
+
- In most cases, such as sending out statistics, it's not a problem, as the server just accepts the data and usually sends an empty response to such requests.
0 commit comments