Skip to content

Commit 51f3915

Browse files
authored
Merge pull request #2390 from RapTho/patch-5
small reformulation
2 parents c67f8b8 + bcbb5d5 commit 51f3915

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

5-network/06-fetch-api/article.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ We fully covered `method`, `headers` and `body` in the chapter <info:fetch>.
4242

4343
The `signal` option is covered in <info:fetch-abort>.
4444

45-
Now let's explore the rest of capabilities.
45+
Now let's explore the remaining capabilities.
4646

4747
## referrer, referrerPolicy
4848

49-
These options govern how `fetch` sets HTTP `Referer` header.
49+
These options govern how `fetch` sets the HTTP `Referer` header.
5050

5151
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.
5252

@@ -81,18 +81,18 @@ Requests are split into 3 types:
8181
2. Request to another origin.
8282
3. Request from HTTPS to HTTP (from safe to unsafe protocol).
8383

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.
8585

8686
Possible values are described in the [Referrer Policy specification](https://w3c.github.io/webappsec-referrer-policy/):
8787

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).
8989
- **`"no-referrer"`** -- never send `Referer`.
9090
- **`"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.
9696

9797
Here's a table with all combinations:
9898

@@ -107,13 +107,13 @@ Here's a table with all combinations:
107107
| `"strict-origin-when-cross-origin"` | full | origin | - |
108108
| `"unsafe-url"` | full | full | full |
109109

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.
111111

112112
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`).
113113

114114
E.g. `Referer: https://javascript.info/admin/secret/paths`.
115115

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:
117117

118118
```js
119119
fetch('https://another.com/page', {
@@ -129,7 +129,7 @@ Its only difference compared to the default behavior is that for requests to ano
129129
```smart header="Referrer policy is not only for `fetch`"
130130
Referrer policy, described in the [specification](https://w3c.github.io/webappsec-referrer-policy/), is not just for `fetch`, but more global.
131131

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">`.
133133
```
134134
135135
## mode
@@ -152,13 +152,13 @@ The `credentials` option specifies whether `fetch` should send cookies and HTTP-
152152
153153
## cache
154154
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.
156156
157157
The `cache` options allows to ignore HTTP-cache or fine-tune its usage:
158158
159159
- **`"default"`** -- `fetch` uses standard HTTP-cache rules and headers,
160160
- **`"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),
162162
- **`"no-cache"`** -- create a conditional request if there is a cached response, and a normal request otherwise. Populate HTTP-cache with the response,
163163
- **`"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,
164164
- **`"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:
177177
178178
The `integrity` option allows to check if the response matches the known-ahead checksum.
179179
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.
181181
182182
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).
183183
@@ -195,11 +195,11 @@ Then `fetch` will calculate SHA-256 on its own and compare it with our string. I
195195

196196
The `keepalive` option indicates that the request may "outlive" the webpage that initiated it.
197197

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.
199199

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.
201201

202-
We can use `window.onunload` event for that:
202+
We can use the `window.onunload` event for that:
203203

204204
```js run
205205
window.onunload = function() {
@@ -213,12 +213,12 @@ window.onunload = function() {
213213
};
214214
```
215215

216-
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.
217217

218218
It has a few limitations:
219219

220220
- We can't send megabytes: the body limit for `keepalive` requests is 64KB.
221221
- 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.
222222
- 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.
223223
- 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

Comments
 (0)