Skip to content

Commit

Permalink
Only preload resources with matching type/as attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
noamr authored Apr 26, 2022
1 parent d4115a1 commit c795467
Showing 1 changed file with 101 additions and 29 deletions.
130 changes: 101 additions & 29 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,11 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<dfn data-x-href="https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match">JavaScript MIME type essence match</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#json-mime-type">JSON MIME type</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#xml-mime-type">XML MIME type</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#image-mime-type">image MIME type</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#audio-or-video-mime-type">audio or video MIME type</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#font-mime-type">font MIME type</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#parse-a-mime-type">parse a MIME type</dfn></li>
<li><dfn data-x-href="https://mimesniff.spec.whatwg.org/#supported-by-the-user-agent">is MIME type supported by the user agent?</dfn></li>
</ul>
</dd>

Expand Down Expand Up @@ -14559,12 +14564,14 @@ data-x="rel-preload">preload</span>; <span data-x="attr-link-as">as</span>=font<
data-x="">target_attributes</code>"].</p>

<p class="note">Only the <code data-x="attr-link-as">as</code>, <code
data-x="attr-link-crossorigin">crossorigin</code>, and <code
data-x="attr-link-integrity">integrity</code> attributes are handled as part of early hint
processing. The other ones, in particular <code data-x="attr-link-media">media</code>, <code
data-x="attr-link-crossorigin">crossorigin</code>, <code
data-x="attr-link-integrity">integrity</code>, and <code data-x="attr-link-type">type</code>
attributes are handled as part of early hint processing. The other ones, in particular <code
data-x="attr-link-blocking">blocking</code>, <code
data-x="attr-link-imagesrcset">imagesrcset</code>, <code
data-x="attr-link-imagesizes">imagesizes</code>, and "<code data-x="">blocking</code>" are
only applicable once a <code>Document</code> is created.</p>
data-x="attr-link-imagesizes">imagesizes</code>, and <code
data-x="attr-link-media">media</code> are only applicable once a <code>Document</code> is
created.</p>
</li>

<li><p>Let <var>crossorigin</var> be <var>attribs</var>["<code
Expand Down Expand Up @@ -14601,6 +14608,12 @@ data-x="rel-preload">preload</span>; <span data-x="attr-link-as">as</span>=font<
<li><p>Set <var>earlyRequest</var>'s <span data-x="concept-request-policy-container">policy
container</span> to <var>earlyPolicyContainer</var>.</p></li>

<li><p>Let <var>type</var> be the empty string.</p></li>

<li><p>If <var>attribs</var>["<code data-x="attr-link-type">type</code>"] <span
data-x="map exists">exists</span>, then set <var>type</var> to
<var>attribs</var>["<code data-x="attr-link-type">type</code>"].</p></li>

<li><p>If <var>attribs</var>["<code data-x="attr-link-integrity">integrity</code>"] <span
data-x="map exists">exists</span>, then set <var>earlyRequest</var>'s <span
data-x="concept-request-integrity-metadata">integrity metadata</span> to
Expand Down Expand Up @@ -14648,10 +14661,10 @@ data-x="rel-preload">preload</span>; <span data-x="attr-link-as">as</span>=font<
</ol>
</li>

<li><p><span data-x="preload a resource">Preload</span> <var>earlyRequest</var> with the
following steps given <span data-x="concept-response">response</span> <var>res</var>: set
<var>unsafeEndTime</var> to the <span>unsafe shared current time</span> and call
<var>processResponse</var> with <var>res</var>.</p></li>
<li><p><span data-x="preload a resource">Preload</span> <var>earlyRequest</var> given
<var>type</var> and the following steps given <span data-x="concept-response">response</span>
<var>res</var>: set <var>unsafeEndTime</var> to the <span>unsafe shared current time</span> and
call <var>processResponse</var> with <var>res</var>.</p></li>
</ol>
</li>

Expand Down Expand Up @@ -26233,30 +26246,82 @@ document.body.appendChild(wbr);</code></pre>
</ol>

<p>To <dfn>preload a resource</dfn> given a <span data-x="concept-request">request</span>
<var>request</var> and <var>processResponse</var>, which is an algorithm accepting a <span
data-x="concept-response">response</span>: <!--FETCH--><span data-x="concept-fetch">fetch</span>
<var>request</var>, with <i data-x="processResponseConsumeBody">processResponseConsumeBody</i>
set to the following steps given <span data-x="concept-response">response</span>
<var>response</var> and null or <span>byte sequence</span> <var>bytesOrNull</var>:</p>
<var>request</var>, a string <var>type</var> and <var>processResponse</var>, which is an algorithm
accepting a <span data-x="concept-response">response</span>:</p>

<ol>
<li><p>If <var>type</var> doesn't <span data-x="match preload type">match</span>
<var>request</var>'s <span data-x="concept-request-destination">destination</span>, then
return.</p></li>

<li><p><!--FETCH--><span data-x="concept-fetch">Fetch</span> <var>request</var>, with
<i data-x="processResponseConsumeBody">processResponseConsumeBody</i>
set to the following steps given <span data-x="concept-response">response</span>
<var>response</var> and null or <span>byte sequence</span> <var>bytesOrNull</var>:</p>

<ol>
<li>
<p>If <var>bytesOrNull</var> is a <span>byte sequence</span>, then set <var>response</var>'s
<span data-x="concept-response-body">body</span> to the first return value of <span
data-x="body safely extract">safely extracting</span> <var>bytesOrNull</var>.</p>

<p class=note>By using <i data-x="processResponseConsumeBody">processResponseConsumeBody</i>,
we have <span data-x="body safely extract">extracted</span> the entire <span
data-x="concept-response-body">body</span>. This is necessary to ensure the preloader loads the
entire body from the network, regardless of whether the preload will be consumed (which is
uncertain at this point). This step then resets the request's body to a new body containing the
same bytes, so that other specifications can read from it at the time of actual consumption,
despite us having already done so once.</p>
</li>

<li><p>Otherwise, set <var>response</var> to a <span>network error</span>.</p></li>

<li><p>Call <var>processResponse</var> with <var>response</var>.</p>
</ol>
</li>
</ol>

<p>For the purposes of the above algorithm, a string <var>type</var>
<dfn data-x="match preload type">matches</dfn> a string <var>destination</var> if the following
algorithm returns true:</p>
<ol>
<li><p>If <var>type</var> is an empty string, then return true.</p></li>

<li><p>If <var>destination</var> is "<code data-x="">fetch</code>", then return true.</p></li>

<li><p>Let <var>mimeTypeRecord</var> be the result of
<span data-x="Parse a mime type">parsing</span> <var>type</var>.</p></li>

<li><p>If <var>mimeTypeRecord</var> is failure, then return false.</p></li>

<li><p>If <var>mimeTypeRecord</var> is not <span
data-x="Is MIME type supported by the user agent?">supported by the user agent</span>, then
return false.</p>

<li>
<p>If <var>bytesOrNull</var> is a <span>byte sequence</span>, then set <var>response</var>'s
<span data-x="concept-response-body">body</span> to the first return value of <span
data-x="body safely extract">safely extracting</span> <var>bytesOrNull</var>.</p>
<p>Return true if any of the following conditions are true:</p>

<p class=note>By using <i data-x="processResponseConsumeBody">processResponseConsumeBody</i>,
we have <span data-x="body safely extract">extracted</span> the entire <span
data-x="concept-response-body">body</span>. This is necessary to ensure the preloader loads the
entire body from the network, regardless of whether the preload will be consumed (which is
uncertain at this point). This step then resets the request's body to a new body containing the
same bytes, so that other specifications can read from it at the time of actual consumption,
despite us having already done so once.</p>
<ul class="brief">
<li><p><var>destination</var> is "<code data-x="">audio</code>" or "<code
data-x="">video</code>", and <var>mimeTypeRecord</var> is an
<span>audio or video MIME type</span></p></li>
<li><p><var>destination</var> is a
<span data-x="concept-script-like-destination">script-like destination</span> and
<var>mimeTypeRecord</var> is a <span>JavaScript MIME type</span></p></li>
<li><p><var>destination</var> is "<code data-x="">image</code>" and
<var>mimeTypeRecord</var> is an <span>image MIME type</span></p></li>
<li><p><var>destination</var> is "<code data-x="">font</code>" and
<var>mimeTypeRecord</var> is a <span>font MIME type</span></p></li>
<li><p><var>destination</var> is "<code data-x="">style</code>" and
<var>mimeTypeRecord</var>'s <span data-x="MIME type essence">essence</span> is
<code>text/css</code></p></li>
<li><p><var>destination</var> is "<code data-x="">track</code>" and
<var>mimeTypeRecord</var>'s <span data-x="MIME type essence">essence</span> is
<code>text/vtt</code></p></li>
</ul>
</li>

<li><p>Otherwise, set <var>response</var> to a <span>network error</span>.</p></li>

<li><p>Call <var>processResponse</var> with <var>response</var>.</p>
<li><p>Return false.</p></li>
</ol>

<p>To <dfn>create a preload key</dfn> for a <span data-x="concept-request">request</span>
Expand Down Expand Up @@ -26316,9 +26381,16 @@ document.body.appendChild(wbr);</code></pre>
<li><p>If <var>el</var> is <span>render-blocking</span>, then <span>block rendering</span>
on <var>el</var>.</p></li>

<li><p>Let <var>type</var> be the empty string.</p></li>

<li><p>If <var>el</var> has a <code data-x="attr-link-type">type</code> attribute, then set
<var>type</var> to the value of <var>el</var>'s <code data-x="attr-link-type">type</code>
attribute.</p></li>

<li>
<p><span data-x="preload a resource">Preload</span> <var>request</var>, with the following
steps given <span data-x="concept-response">response</span> <var>response</var>:</p>
<p><span data-x="preload a resource">Preload</span> <var>request</var>, given <var>type</var>
and the following steps given <span data-x="concept-response">response</span>
<var>response</var>:</p>

<ol>
<li><p><span>Finalize and report timing</span> with <var>response</var>, given <var>el</var>'s
Expand Down

0 comments on commit c795467

Please sign in to comment.