Skip to content

Commit f975634

Browse files
committed
Rephrase to be more howto style
1 parent 79931e9 commit f975634

File tree

1 file changed

+65
-49
lines changed

1 file changed

+65
-49
lines changed

fetch.bs

+65-49
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,9 @@ these steps:
15081508

15091509
<h4 id=requests>Requests</h4>
15101510

1511+
<p class=note>This section documents how requests work in detail. To get started with populating a
1512+
request, see <a href="fetch-elsewhere-request">populating a request</a>.
1513+
15111514
<p>The input to <a for=/>fetch</a> is a
15121515
<dfn export id=concept-request>request</dfn>.
15131516

@@ -1694,10 +1697,10 @@ and "<code>webidentity</code>" as fetches with those destinations skip service w
16941697
not always relevant and might require different behavior.
16951698

16961699
<div class=note>
1697-
<p>The following table illustrates the relationship between a <a for=/>request</a>'s
1698-
<a for=request>initiator</a>, <a for=request>destination</a>, CSP directives, and features. It is
1699-
not exhaustive with respect to features. Features need to have the relevant values defined in their
1700-
respective standards.
1700+
<p>The following <dfn lt="destination table">table</dfn> illustrates the relationship between a
1701+
<a for=/>request</a>'s <a for=request>initiator</a>, <a for=request>destination</a>, CSP
1702+
directives, and features. It is not exhaustive with respect to features. Features need to have the
1703+
relevant values defined in their respective standards.
17011704

17021705
<table>
17031706
<tbody><tr>
@@ -8385,55 +8388,68 @@ correctly. This section aims to give some advice.
83858388

83868389
<p class=XXX>This is a work in progress.
83878390

8388-
<h3 id=fetch-elsewhere-request>Constructing a request</h3>
8391+
<h3 id=fetch-elsewhere-request>Populating a request</h3>
83898392

83908393
<p>The first step in <a for=/>fetching</a> is to create a <a for=/>request</a>, and populate its
83918394
multiple associated parameters.
83928395

8393-
<p>A <a for=/>request</a> oftenmost has a <a for=request>client</a>, which is the
8394-
<a>environment settings object</a> that initiatated the request, and the receiver of the different
8395-
<a for=/>response</a> callbacks. It then expects a <a for=request>URL</a>, and an HTTP
8396-
<a for=request>method</a>, `<code>GET</code>` by default. `<code>POST</code>` and
8397-
`<code>PUT</code>` requests also accept a <a for=request>body</a>, which is sent together with the
8398-
request.
8399-
8400-
<p>Requests accept an optional <a for=request>header list</a> parameter, custom HTTP headers to be
8401-
sent alongside the <a for=/>request</a>. Note that sending custom headers may have implications,
8402-
such as requiring a <a>CORS-preflight fetch</a>, so handle with care.
8403-
8404-
<p>One of the most important things when constructing a <a for=/>request</a> is understanding how
8405-
it's supposed to handle cross-origin resources. This is expressed in the <a for=/>request</a>'s
8406-
<a for=request>mode</a>. Some features are supposed to work only in the context of the
8407-
<a>same origin</a>, in which case the "<code>same-origin</code>" <a for=request>mode</a> is
8408-
appropriate. For new features that require accessing cross-origin resources, "<code>cors</code>"
8409-
should be used, alongside a <a for=request>credentials mode</a>, which defines whether the
8410-
<a for=/>request</a> is anonymous or authenticated (e.g. with cookies and client certificates). The
8411-
"<code>no-cors</code>" <a for=request>mode</a> is used for backwards compatibility with features
8412-
that existed before the CORS mechanism was introduced. To use "<code>no-cors</code>" in a new
8413-
feature, please discuss on the <a href=https://github.com/whatwg/fetch>Fetch Github repository</a>.
8414-
"<code>navigate</code>" and "<code>websocket</code>" are reserved for specific features.
8415-
8416-
<p>A <a for=/>request</a> accepts a few security-related parameters that are often configured, such
8417-
as <a for=request>integrity metadata</a>, a string hash of the actual data that verifies that the
8418-
received resource matches an expectation, a <a for=request>cryptographic nonce metadata</a> and
8419-
<a for=request>parser metadata</a>, which allows more fine-grained security via
8420-
<cite>Content Security Policy</cite>, and <a for=request>referrer policy</a>, which allows the
8421-
<a for=/>request</a> to use a different <a for=/>referrer policy</a> from the one provided by the
8422-
<a for=request>client</a>. [[!CSP]] [[!REFERRER]]
8423-
8424-
<p>A <a for=/>request</a> also accepts a few metadata parameters, the important one being
8425-
<a for=request>destination</a>. <a for=request>destinations</a> affect
8426-
<cite>Content Security Policy</cite> and have other implications such as the
8427-
`<code>sec-fetch-dest</code>` header, so they are much more than informative metadata. If a new
8428-
feature requires a <a for=request>destination</a> that's not in the list of
8429-
<a for=request>destinations</a>, please file an issue in the
8430-
<a href=https://github.com/whatwg/fetch>Fetch Github repository</a>. An aditional metadata
8431-
paramater is <a for=request>initiator type</a>, which specifies that the <a for=/>fetching</a>
8432-
should <a for=/>mark resource timing</a> automatically once the <a for=/>response</a>'s
8433-
<a for=response>body</a> is fully downloaded. [[!CSP]] [[RESOURCE-TIMING]]
8434-
8435-
<p>The rest of the parameters for <a for=/>request</a> are used less frequently, and they are
8436-
documented in detail in the <a href="#requests">Requests</a> section of this standard.
8396+
<p>Start by setting the <a for=/>request</a>'s <a for=request>URL</a> and <a for=request>method</a>,
8397+
as defined by HTTP. If your `<code>POST</code>` or `<code>PUT</code>` <a for=/>request</a> needs a
8398+
body, you can assign or stream it to <a for=/>request</a>'s <a for=request>body</a>. [[HTTP]]
8399+
8400+
<p>Set your <a for=/>request</a>'s <a for=request>client</a> to the
8401+
<a>environment settings object</a> you're operating in. Web-exposed APIs usually operate on
8402+
<a>platform objects</a> (e.g. a <cite>Web IDL</cite> based API), which have a
8403+
<a>relevant settings object</a>. For example, a <a for=/>request</a> associated with a DOM
8404+
<a for=/>element</a> would set the <a for=/>request</a>'s <a for=request>client</a> to the element's
8405+
<a>node document</a>'s <a>relevant settings object</a>. [[WEBIDL]]
8406+
8407+
<p>Choose your <a for=/>request</a>'s <a for=request>destination</a>.
8408+
<a for=request>destinations</a> affect <cite>Content Security Policy</cite> and have other
8409+
implications such as the `<code>sec-fetch-dest</code>` header, so they are much more than
8410+
informative metadata. If a new feature requires a <a for=request>destination</a> that's not in the
8411+
<a>destination table</a>, please file an issue in the
8412+
<a href=https://github.com/whatwg/fetch>Fetch Github repository</a> to discuss. [[!CSP]]
8413+
8414+
<p>Think through the way you intend to handle cross-origin resources. Some features may only work in
8415+
the <a>same origin</a>, in which case set your <a for=/>request</a> <a for=request>mode</a> to
8416+
"<code>same-origin</code>". As a general rule, modern web-exposed features that fetch cross-origin
8417+
resources should use "<code>cors</code>" and decide on the <a for=request>credentials mode</a> on a
8418+
case by case basis. For example, font loading uses "<code>same-origin</code>" (credentials are only
8419+
sent in same-origin requests), video posters use "<code>include</code>" (credentials are always
8420+
sent), and many features make this configurable in the API. If your feature is not web-exposed, or
8421+
you think there is another reason for it to fetch cross-origin resources without CORS, discuss this
8422+
with your security team and ping contributors of the
8423+
<a href=https://github.com/whatwg/fetch>Fetch Standard</a>.
8424+
8425+
<p>Figure out if your fetch needs to be reported to <cite>Resource Timing</cite>, and with which
8426+
<a for=request>initiator type</a>. By passing an <a for=request>initiator type</a> to the
8427+
<a for=/>request</a>, reporting to <cite>Resource Timing</cite> will be done automatically once the
8428+
fetch is done and the <a for=/>response</a> is fully downloaded. [[RESOURCE-TIMING]]
8429+
8430+
<p>If your request requires additional HTTP headers, <a for="header list">append</a> them to your
8431+
<a for=/>request</a>'s <a for=request>header list</a>. Note that sending custom headers may have
8432+
implications, such as requiring a <a>CORS-preflight fetch</a>, so handle with care.
8433+
8434+
<p>Check if your request needs one or more of the additional security-related parameters. The
8435+
following ones are often configurable, e.g. by an HTML attribute:
8436+
8437+
<dl>
8438+
<dt><a for=request>integrity metadata</a>
8439+
<dd><p>Verifies that the content of the resource matches a pre-calculated string hash.
8440+
8441+
<dt><a for=request>referrer policy</a>
8442+
<dd><p>Allows using a different <a for=/>referrer policy</a> from the one provided by the
8443+
<a for=/>environment settings object</a>. [[!REFERRER]]
8444+
8445+
<dt><a ror=request>cryptographic nonce metadata</a>
8446+
<dd><p>Together with <cite>Content Security Policy</cite>, ensures that only elements that provide
8447+
this predetermined string can fetch this resource.
8448+
</dl>
8449+
8450+
<p>Browse through the rest of the parameters for <a for=/>request</a> to see if something else is
8451+
relevant to you. The rest of the parameters are used less frequently, often for special purposes,
8452+
and they are documented in detail in the <a href="#requests">Requests</a> section of this standard.
84378453

84388454

84398455
<h3 id=fetch-elsewhere-fetch class=no-num>Invoking fetch</h3>

0 commit comments

Comments
 (0)