From cfb20f5ace19e753d773877d76204bf9217214ef Mon Sep 17 00:00:00 2001 From: Noam Rosenthal Date: Wed, 1 Dec 2021 00:06:40 +0200 Subject: [PATCH] Define behavior of in detail A document keeps a list of preloaded resources, each with relevant parameters from the request, and the response once available. Once a 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 https://github.com/whatwg/fetch/issues/590. --- source | 214 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 175 insertions(+), 39 deletions(-) diff --git a/source b/source index bb01cf84a14..ac8a64077f8 100644 --- a/source +++ b/source @@ -2152,6 +2152,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
  • scalar value
  • tuple
  • noncharacter
  • +
  • byte sequence
  • string, code unit, length, and @@ -2487,7 +2488,10 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
  • the RequestCredentials enumeration
  • the RequestDestination enumeration
  • the fetch() method
  • +
  • finalize and report timing
  • serialize a response URL for reporting
  • +
  • safely extracting a body
  • +
  • processResponseEndOfBody
  • response and its @@ -14009,37 +14013,13 @@ interface HTMLLinkElement : HTMLElement { el, is as follows:

      -
    1. If el's href attribute's value is the - empty string, then return.

    2. - -
    3. Parse a URL given el's href - attribute, relative to el's node document. If that fails, then return. - Otherwise, let url be the resulting URL record.

    4. - -
    5. Let corsAttributeState be the current state of el's crossorigin content attribute.

    6. +
    7. Let request be the result of creating a link element request given el.

    8. -
    9. Let request be the result of creating a potential-CORS request given - url, the empty string, and corsAttributeState.

    10. +
    11. If request is null, then return.

    12. Set request's synchronous flag.

    13. -
    14. Set request's client to - el's node document's relevant settings object.

    15. - -
    16. Set request's cryptographic - nonce metadata to the current value of el's [[CryptographicNonce]] - internal slot.

    17. - -
    18. Set request's integrity - metadata to the current value of el's integrity content attribute.

    19. - -
    20. Set request's - referrer policy to the current state of - el's referrerpolicy attribute.

    21. -
    22. Run the linked resource fetch setup steps, given el and request. If the result is false, then return.

    23. @@ -14079,6 +14059,43 @@ interface HTMLLinkElement : HTMLElement {
    +

    To create a link element request + given a link element el and an optional string destination + (default the empty string):

    + +
      +
    1. If el's href attribute's value is the + empty string, then return null.

    2. + +
    3. Parse a URL given el's href + attribute, relative to el's node document. If that fails, then return + null. Otherwise, let url be the resulting URL record.

    4. + +
    5. Let corsAttributeState be the current state of el's crossorigin content attribute.

    6. + +
    7. Let request be the result of creating a potential-CORS request given url, destination, + and corsAttributeState.

    8. + +
    9. Set request's client to + el's node document's relevant settings object.

    10. + +
    11. Set request's cryptographic + nonce metadata to the current value of el's + [[CryptographicNonce]] internal slot.

    12. + +
    13. Set request's integrity + metadata to the current value of el's integrity content attribute.

    14. + +
    15. Set request's referrer + policy to the current state of el's referrerpolicy content attribute.

    16. + +
    17. Return request.

    18. +
    +

    User agents may opt to only try to fetch and process such resources when they are needed, instead of pro-actively fetching all the external resources that are not applied.

    @@ -25425,24 +25442,89 @@ document.body.appendChild(wbr); elements. This keyword creates an external resource link. This keyword is body-ok.

    -

    The preload keyword indicates that the user agent must +

    The preload keyword indicates that the user agent will preemptively fetch and cache the specified resource according to the potential destination given by the as attribute (and the priority associated with the corresponding destination), as it is highly likely that the user - will require this resource for the current navigation. User agents must implement - the processing model of the preload keyword described in - Preload, as well as in this specification's fetch and process the linked - resource algorithm.

    + will require this resource for the current navigation.

    There is no default type for resources given by the preload keyword.

    -

    The linked resource fetch setup steps for this type of linked resource, given a - link element el and request - request, are:

    +

    A Document has a map of preloaded resources, which is a + map, initially empty.

    + +

    A preload key is a struct. It has the following items:

    + +
    +
    URL +
    A URL + +
    destination +
    integrity metadata +
    A string + +
    mode +
    A request mode, either + same-origin", "cors", or + "no-cors" + +
    credentials mode +
    A credentials mode +
    + +

    A preload entry is a struct. It has the following items:

    + +
    +
    response +
    Null or a response + +
    on response available +
    Null, or an algorithm accepting a response or null +
    + +

    To consume a preloaded resource for Window window, + given a URL url, a string destination, a string + mode, a string credentialsMode, a string integrityMetadata, and + onResponseAvailable, which is an algorithm accepting a response:

    + +
      +
    1. Let key be a preload key whose URL is url, destination is + destination, integrity metadata is + integrityMetadata, mode is mode, and + credentials mode is + credentialsMode.

    2. + +
    3. Let preloads be window's associated Document's map of + preloaded resources.

    4. + +
    5. If key does not exist in preloads, + then return false.

    6. + +
    7. Let entry be preloads[key].

    8. + +
    9. Remove preloads[key].

    10. + +
    11. If entry response is null, then set + entry's on response available to + onResponseAvailable.

    12. + +
    13. Otherwise, call onResponseAvailable with entry's response.

    14. + +
    15. Return true.

    16. +
    + +

    The fetch and process the linked resource steps for this type of linked resource, + given a link element el, are:

    1. Let as be the current state of el's

    2. If as does not represent a state, return false.

    3. -
    4. Set request's destination to - the result of translating - as.

    5. +
    6. Let request be the result of creating a link element request given el and the result + of translating + as.

    7. + +
    8. If request is null, then return.

    9. If as is "image", then:

      @@ -25473,7 +25558,58 @@ document.body.appendChild(wbr);
  • -
  • Return true.

  • +
  • Let preloadKey be a preload key whose URL is request's URL, destination is request's destination, integrity metadata is request's integrity metadata, mode is request's mode, and + credentials mode is request's credentials mode.

  • + +
  • Let preloadEntry be a new preload entry.

  • + +
  • Set el's node document's map + of preloaded resources[preloadKey] to preloadEntry.

  • + +
  • +

    Fetch request, with processResponseEndOfBody set to the following steps given + response response and null or byte + sequence bytesOrNull:

    + +
      +
    1. +

      If bytesOrNull is a byte sequence, then set response's + body to the first return value of safely extracting bytesOrNull.

      + +

      By using processResponseEndOfBody, we + have extracted the entire body. 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.

      +
    2. + +
    3. Otherwise, set response to a network error.

    4. + +
    5. Finalize and report timing with response, given el's + relevant global object and "link".

    6. + +
    7. Fire an event named load at el.

    8. + +
    9. If preloadEntry's on response + available is null, then set preloadEntry's response to response.

    10. + +
    11. Otherwise, call preloadEntry's on response available with response.

    12. +
    +