Skip to content

Commit

Permalink
Define storage of preloaded resources
Browse files Browse the repository at this point in the history
A document keeps a list of preloaded resources, each with relevant
parameters from the request, and the response once available.

Once a <link rel=preload> element starts fetching a resource, that
entry is added, and once the response is fully loaded, the fetch
consuming the resource receives the response.

See whatwg/fetch#590
  • Loading branch information
noamr committed Nov 7, 2021
1 parent f8cb0cc commit ef64446
Showing 1 changed file with 125 additions and 5 deletions.
130 changes: 125 additions & 5 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li><dfn data-x-href="https://infra.spec.whatwg.org/#scalar-value">scalar value</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#tuple">tuple</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#noncharacter">noncharacter</dfn></li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#byte-sequence">byte sequence</dfn><li>
<li><dfn data-x-href="https://infra.spec.whatwg.org/#string">string</dfn>,
<dfn data-x-href="https://infra.spec.whatwg.org/#code-unit">code unit</dfn>,
<dfn data-x-href="https://infra.spec.whatwg.org/#string-length">length</dfn>, and
Expand Down Expand Up @@ -2485,7 +2486,10 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#requestcredentials"><code>RequestCredentials</code></dfn> enumeration</li>
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#requestdestination"><code>RequestDestination</code></dfn> enumeration</li>
<li>the <dfn data-x-href="https://fetch.spec.whatwg.org/#dom-global-fetch"><code>fetch()</code></dfn> method</li>
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#finalize-and-report-timing">finalize and report timing</dfn></li>
<li><dfn data-x="serialize-a-response-url-for-reporting" data-x-href="https://fetch.spec.whatwg.org/#serialize-a-response-url-for-reporting">serialize a response URL for reporting</dfn></li>
<li><dfn data-x="body safely extract" data-x-href="https://fetch.spec.whatwg.org/#bodyinit-safely-extract">safely extracting a body</dfn></li>
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#process-response-end-of-body">processResponseEndOfBody</dfn></li>
<li>
<dfn data-x="concept-response"
data-x-href="https://fetch.spec.whatwg.org/#concept-response">response</dfn> and its
Expand Down Expand Up @@ -25420,14 +25424,89 @@ document.body.appendChild(wbr);</code></pre>
data-x="concept-request-priority">priority</span> associated with the <span
data-x="concept-potential-destination-translate">corresponding</span> <span
data-x="concept-request-destination">destination</span>), as it is highly likely that the user
will require this resource for the current navigation. <span w-nodev>User agents must implement
the processing model of the <code data-x="rel-preload">preload</code> keyword described in
<cite>Preload</cite>, as well as in this specification's <span>fetch and process the linked
resource</span> algorithm.</span> <ref spec=PRELOAD></p>
will require this resource for the current navigation. <ref spec=PRELOAD></p>

<p>There is no default type for resources given by the <code data-x="rel-preload">preload</code>
keyword.</p>

<p>A <code>Document</code> has a <dfn>list of preloaded resources</dfn> (a <span>list</span> of
<span>preload entry</span>), which is initially empty.</p>

<p>A <dfn>preload entry</dfn> is a <span>struct</span>. It has the following
<span data-x="struct item">items</span>:</p>

<dl>
<dt><dfn data-x="preload entry URL">URL</dfn>
<dd>A <span>URL</span>.

<dt><dfn data-x="preload entry destination">destination</dfn>
<dd>A string

<dt><dfn data-x="preload entry mode">mode</dfn>
<dd>"<code data-x="">same-origin</code>", "<code data-x="">cors</code>", or
"<code data-x="">no-cors</code>"

<dt><dfn data-x="preload entry credentials mode">credentials mode</dfn>
<dd>"<code data-x="">omit</code>", "<code data-x="">same-origin</code>" or
"<code data-x="">include</code>"

<dt><dfn data-x="preload entry response">response</dfn> (default null)
<dd>Null or a <span data-x="concept-response">response</span>

<dt><dfn data-x="preload entry on response available">on response available</dfn> (default null)
<dd>Null or n algorithm accepting a <span data-x="concept-response">response</span>
</dl>

<p>To <dfn export>consume a preloaded resource</dfn> for <code>Window</code> <var>win</var>,
given a <span>URL</span> <var>url</var>, a string <var>destination</var>, a string
<var>mode</var>, a string <var>credentialsMode</var>, and <var>onResponseAvailable</var>, which is
an algorithm accepting a <span data-x="concept-response">response</span>, perform the following
step: <span data-x="list iterate">For each</span> <var>entry</var> in <var>win</var>'s
<span data-x="concept-document-window">associated <code>Document</code></span>'s
<span>list of preloaded resources</span>, if all of the following are true:</p>

<ul>
<li><p><var>entry</var>'s <span data-x="preload entry URL">URL</span> is
<var>url</var></p></li>

<li><p><var>entry</var>'s <span data-x="preload entry destination">destination</span> is
<var>destination</var></p></li>

<li><p><var>entry</var>'s <span data-x="preload entry mode">mode</span> is
<var>mode</var></p></li>

<li><p><var>entry</var>'s <span data-x="preload entry credentials mode">credentials mode</span>
is <var>credentialsMode</var></p></li>
</ul>

<p>then:</p>

<ol>
<li>
<p><span data-x="list remove">Remove</span> <var>entry</var> from <var>win</var>'s
<span data-x="concept-document-window">associated <code>Document</code></span>'s
<span>list of preloaded resources</span>.</p>

<p class="note">Removing the resource from the <span>list of preloaded resources</span> does
not necessarily mean that user-agents have to remove it from their implementaion-specific
caches, rather that the observable behavior specific to preloads no longer applies. For
example, user agents can add a preloaded image to the <span>list of available images</span>
and take care of the different cache behaviors at a different layer.</p>
</li>

<li><p>If <var>entry</var>'s <span data-x="preload entry response">response</span> is null,
then set <var>entry</var>'s
<span data-x="preload entry on response available">on response available</span> to
<var>onResponseAvailable</var>.</p></li>

<li><p>Otherwise, call <var>onResponseAvailable</var> with <var>entry</var>'s
<span data-x="preload entry response">response</span>.</p></li>

<li><p>Return true.</p></li>
</ol>

<p>Otherwise, return false.</p>

<p>The <span>linked resource fetch setup steps</span> for this type of linked resource, given a
<code>link</code> element <var>el</var> and <span data-x="concept-request">request</span>
<var>request</var>, are:</p>
Expand Down Expand Up @@ -25461,7 +25540,48 @@ document.body.appendChild(wbr);</code></pre>
</ol>
</li>

<li><p>Return true.</p></li>
<li><p>Let <var>preloadEntry</var> be a <span>preload entry</span>
whose <span data-x="preload entry URL">URL</span> is <var>request</var>'s
<span data-x="concept-request-url">URL</span>,
<span data-x="preload entry destination">destination</span> is <var>request</var>'s
<span data-x="concept-request-destination">destination</span>,
<span data-x="preload entry mode">mode</span> is <var>request</var>'s
<span data-x="concept-request-mode">mode</span>, and
<span data-x="preload entry credentials mode">credentials mode</span> is <var>request</var>'s
<span data-x="concept-request-credentials-mode">credentials mode</span>.</p></li>

<li><p><span data-x="list append">Append</span> <var>preloadEntry</var> to <var>el</var>'s
<span>node document</span>'s <span>list of preloaded resources</span>.</p></li>

<li>
<p><span data-x="concept-fetch">Fetch</span> <var>request</var>, with
<span>processResponseEndOfBody</span> set to the following steps given
<span data-x="concept-response">response</span> <var>response</var> and
null, failure or <span>byte sequence</span> <var>potentialBytes</var>:</p>

<ol>
<li><p>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>potentialBytes</var>.</p></li>

<li><p><span>Finalize and report timing</span> with <var>response</var>,
given <var>el</var>'s <span>relevant global object</span>, and "link".</p></li>

<li><p><span data-x="concept-event-fire">Fire</span> a <code data-x="event-load">load</code>
event at <var>el</var>.</p></li>

<li><p>If <var>preloadEntry</var>'s
<span data-x="preload entry on response available">on response available</span> is null, then
set <var>preloadEntry</var>'s <span data-x="preload entry response">response</span> to
<var>response</var>.</p></li>

<li><p>Otherwise, call <var>preloadEntry</var>'s
<span data-x="preload entry on response available">on response available</span> with
<var>response</var>.</p></li>
</ol>
</li>

<li><p>Return false.</p></li>
</ol>


Expand Down

0 comments on commit ef64446

Please sign in to comment.