This specification defines the dns-prefetch, preconnect, prefetch, and prerender relationships of the HTML Link Element (``). These primitives enable the developer, and the server generating or delivering the resources, to assist the user agent in the decision process of which origins it should connect to, and which resources it should fetch and preprocess to improve page performance.

Introduction

Modern browsers leverage a wide variety of speculative optimization techniques to anticipate user input and intent, which allows them to hide some of the networking, processing, and rendering latencies: preconnects, prefetching of resources, and prerendering of resources for subsequent navigation.

The decision to initiate one or more of the above optimizations is typically based on heuristic rules based on document markup and structure, navigation history, and context of the user - e.g., type of device, available compute and memory resources, network connectivity, user preferences, and so on. These techniques have proven to be successful, but can be further improved by leveraging the knowledge a developer has about the front-end and back-end generation and delivery of the resources of a web application.

For example, the application may provide the following resource hints to the user agent:

Many web applications already leverage a variety of prefetching techniques. This includes, but is not limited to, using `XMLHttpRequest` to fetch and cache assets before they are needed. However, these implementations are application specific, are not interoperable, and do not provide the same level of performance as the browser-provided primitives. Worse, these implementations sometimes conflict with the browser logic and result in delayed or unnecessary resource fetches that degrade overall page performance.

This specification defines the dns-prefetch, preconnect, prefetch, and prerender relationships of the HTML Link Element (``). These primitives enable the developer, and the server generating or delivering the resources, to assist the user agent in the decision process of which origins it should connect to, and which resources it should fetch and preprocess to improve page performance.

Resource Hints

DNS Prefetch

The dns-prefetch link relation type is used to indicate an origin that will be used to fetch required resources, and that the user agent SHOULD resolve as early as possible.

  <link rel="dns-prefetch" href="//example.com">
      

Preconnect

The preconnect link relation type is used to indicate an origin that will be used to fetch required resources. Initiating an early connection, which includes the DNS lookup, TCP handshake, and optional TLS negotiation, allows the user agent to mask the high latency costs of establishing a connection.

  <link rel="preconnect" href="//example.com">
  <link rel="preconnect" href="//cdn.example.com" crossorigin>
      

To initiate a preconnect, the user agent MUST run these steps:

  1. Resolve the URL given by the `href` attribute.
    1. If that is successful, let preconnect URL be the resulting absolute URL, and otherwise abort these steps.
    2. If preconnect URL's scheme is not one of "http" or "https" then abort these steps.
  2. Let origin be preconnect URL's origin.
  3. Let corsAttributeState be the current state of the element's `crossorigin` content attribute.
  4. Let credentials be a boolean value set to `true`.
  5. If corsAttributeState is `Anonymous` and origin is not equal to current Document's origin, set credentials to `false`.
  6. Attempt to obtain connection with origin and credentials.

The user agent SHOULD attempt to initiate a preconnect and perform the full connection handshake (DNS+TCP for HTTP, and DNS+TCP+TLS for HTTPS origins) whenever possible, but is allowed to elect to perform a partial handshake (DNS only for HTTP, and DNS or DNS+TCP for HTTPS origins), or skip it entirely, due to resource constraints or other reasons.

The optimal number of connections per origin is dependent on the negotiated protocol, users current connectivity profile, available device resources, global connection limits, and other context specific variables. As a result, the decision for how many connections should be opened is deferred to the user agent.

Prefetch

The prefetch link relation type is used to identify a resource that might be required by the next navigation, and that the user agent SHOULD fetch, such that the user agent can deliver a faster response once the resource is requested in the future.

  <link rel="prefetch" href="//example.com/next-page.html" as="html" crossorigin="use-credentials">
  <link rel="prefetch" href="/library.js" as="script">
      
The `as` attribute can be used by the application to communicate the resource destination context, such that the user agent can optimize the fetching process - e.g. set appropriate request headers, transport priority, and so on.

Prerender

The prerender link relation type is used to identify a resource that might be required by the next navigation, and that the user agent SHOULD fetch and execute, such that the user agent can deliver a faster response once the resource is requested in the future.

  <link rel="prerender" href="//example.com/next-page.html">
      

The user agent MAY preprocess the HTML response by also fetching the necessary subresources and executing them (i.e. prerender the page). The decision for which prerendering steps are performed is deferred to the user agent. The user agent MAY:

When prerendering a document the user agent MUST set the document's visibilityState ([[!PAGE-VISIBILITY]]) value to `prerender`.

The prerender hint can be used by the application to indicate the next likely HTML navigation target: the user agent will fetch and process the specified resource as an HTML response. To fetch other content-types with appropriate request headers, or if HTML preprocessing is not desired, the application can use the prefetch hint.
To ensure compatibility and improve the success rate of prerendering requests the target page can use the Page Visibility API to determine the visibility state of the page as it is being rendered and implement appropriate logic to avoid actions that may cause the prerender to be abandoned (e.g. non-idempotent requests), or unwanted side-effects from being triggered (e.g. analytics beacons firing prior to the page being displayed).

Process

Hint probability ("pr") attribute

In addition to specifying the hint type, the application MAY indicate the expected probability that the specified resource hint will be used.

  <link rel="dns-prefetch" href="//widget.com" pr="0.75">
  <link rel="preconnect" href="//cdn.example.com" pr="0.42">
  <link rel="prefetch" href="//example.com/next-page.html" pr="0.75">
  <link rel="prerender" href="//example.com/thankyou.html" pr="0.25">
      

The pr attribute is a float value in the [0.0-1.0] range that MAY be used in the following cases:

If the pr attribute is omitted for above cases, the user agent MAY assign a default probability value based on own heuristics, past navigation data, and other signals.

A hint probability of `1.0` does not guarantee that the hint will be executed by the user agent. Specified hints MAY be processed on a best effort basis by the user agent, and decision to process the hint MAY be based on the runtime context of the user agent — availability of CPU, GPU, memory, and networking resources — developer specified probability indicating the likelihood of that resource being used, specified user preferences, and other variables. The user agent MAY decide to:

To optimize the overall user experience the user agent should account for local context and specified probability of a speculative hint. For example, on a resource constrained device the user agent may decide to only execute high probability hints. Alternatively, it may decide to perform partial processing of the hint, such as downgrading speculative fetch to a preconnect, performing partial preprocessing, and so on. Performing a partial optimization allows the user agent to improve performance even if a full optimization cannot be performed. Conversely, on a device with sufficient resources, the user agent may execute all of the specified hints as far as possible.

Link element interface extensions

        partial interface HTMLLinkElement {
          attribute DOMString pr;
        };
        

The value of this element's pr attribute. The value of the attribute MUST be a float in the [0.0-1.0] range. Invalid values MUST be ignored by the user agent.

Fetching the resource hint link

The resource hint link's may be specified in the document markup, MAY be provided via the HTTP `Link` header, and MAY be dynamically added to and removed from the document.

  <link rel="dns-prefetch" href="//widget.com" pr="0.75">
  <link rel="preconnect" href="//cdn.example.com" pr="0.42">
  <link rel="prerender" href="//example.com/next-page.html" pr="0.75">
  <link rel="prefetch" href="//example.com/logo-hires.jpg" as="image">
      
  var hint = document.createElement("link");
  hint.rel = "prefetch";
  hint.as = "html";
  hint.href = "/article/part3.html";
  document.head.appendChild(hint);
      

The appropriate times to connect to a host, or obtain the specified resource are:

The user agent MAY delay the fetch of speculative fetch to minimize resource contention for the current navigation context.

The decision to cancel a preconnect or speculative fetch is deferred to the user agent, which MAY use local context and other signals to determine if and when the optimization should be aborted:

Resource fetches that may be required for the next navigation can negatively impact the performance of the current navigation context due to additional contention for the CPU, GPU, memory, and network resources. To address this, the user agent SHOULD implement logic to reduce and eliminate such contention:

Load and error events

The user agent MUST NOT delay the `load` event of the document due to outstanding preconnect or speculative fetch initiated requests.

The decision on whether a resource hint is executed, and if so, whether full or partial processing is applied is deferred to the user agent. As a result, element-level `load` and `error` JavaScripts events are not guaranteed to fire, and if they do, do not guarantee that full processing was applied. However, the user agent SHOULD fire the appropriate `load` and `error` events when possible, to allow the application to track which hints were executed and when.

Use cases

Dynamic request URL (preconnect)

The full resource URL may not be known until the page is being constructed by the user agent - e.g. conditional loading logic, UA adaptation, etc. However, the origin from which one or more of these resources will be fetched is often known ahead of time by the developer or the server generating the response. In such cases, a preconnect hint can be used to initiate an early connection handshake such that when the resource URL is determined, the user agent can dispatch the request without first blocking on connection negotiation.

Anonymizing redirect (preconnect)

Many sites rely on redirect services for analytics, malware protection, and to anonymize the referrer before sending the user to the final destination. Because the destination is known ahead of time, a preconnect hint can be used to initiate the connection handshake with the destination origin (without revealing any private information) in parallel with the processing of the redirect - this masks the redirect latency and reduces navigation time to final destination.

Speculative resource prefetching (prefetch)

The prefetch hint can be used to implement a prefetch strategy that leverages app-specific knowledge about the next navigation based on content, structure, analytics, or other signals - e.g. high-likelihood search results, paginated content or step-driven flows, aggregated analytics or per-user behavior, and so on.

For example, an image gallery may have knowledge about the likelihood of the next photo or page that may be requested by the user. To provide an improved experience the application can ask the user agent to begin fetching required resources (individual photos, critical resources, or the full page) before the next navigation is triggered.

To achieve the above behavior the application can specify one or more prefetch relations and optionally specify the probability (via pr attribute) of each resource being used.

Reactive resource prefetching (prefetch)

The prefetch can be used to implement a "reactive prefetch strategy" that leverages the knowledge of where the user is heading next and enables the application to begin prefetching critical resources in parallel with the navigation request.

To achieve the above behavior the application can listen for click, or other user and application generated events, and dynamically insert relevant prefetch relations for critical resources required by the next navigation. In turn, the user agent can fetch the hinted resources in parallel with the navigation request, making the critical resources available sooner.

The enabling feature of this strategy is that requests initiated via prefetch relation may be allowed to persist across navigations.

Prerendering (prerender)

The prerender hint can be used to prerender the destination page, enabling an instant navigation experience once the user triggers the navigation.

To deliver an instant navigation experience the application can specify one or more prerender relations, each of which points to a destination page (an HTML resource). In turn, the user agent may fetch and process the HTML document, fetch its subresources, and perform other work to deliver an instant navigation - i.e. "prerender" the page.

Finally, because prerendering may require a lot of resources, the application can specify the probability (via pr attribute) of each target to help the user agent deliver the best user experience.

Security and Privacy

The use of resource hints can provide new means to expose information about both the current page and user's activity on it. For example, observing the content of prefetched or prerendered resources, or observing the DNS, IP, and hostname information exposed during connection establishment, both for secure and plain-text connections, can reveal information about the page initiating such requests, and where dynamic resource hints are used, user's activity on it.

To mitigate some of the above risks, the user agent MUST:

Additionally, the user agent MAY provide user setting to control the use of resource hints.

The site author SHOULD take necessary precautions to specify the relevant [[!MIXED-CONTENT]], [[!CSP3]], and [[!REFERRER-POLICY]] policies to allow the user agent to register and apply them prior to processing a resource hint link:

There is only one class of product that can claim conformance to this specification: a user agent.

IANA Considerations

The link relation types below have been registered by IANA per Section 6.2.1 of [[!RFC5988]]:

dns-prefetch Link Relation Type

Relation Name:
dns-prefetch
Description:
Used to indicate an origin that will be used to fetch required resources for the link context, and that the user agent ought to resolve as early as possible.
Reference:
W3C Resource Hints Specification (https://www.w3.org/TR/resource-hints/)

preconnect Link Relation Type

Relation Name:
preconnect
Description:
Used to indicate an origin that will be used to fetch required resources for the link context. Initiating an early connection, which includes the DNS lookup, TCP handshake, and optional TLS negotiation, allows the user agent to mask the high latency costs of establishing a connection.
Reference:
W3C Resource Hints Specification (https://www.w3.org/TR/resource-hints/)

prefetch Link Relation Type

Relation Name:
prefetch
Description:
The prefetch link relation type is used to identify a resource that might be required by the next navigation from the link context, and that the user agent ought to fetch, such that the user agent can deliver a faster response once the resource is requested in the future.
Reference:
W3C Resource Hints Specification (https://www.w3.org/TR/resource-hints/)

prerender Link Relation Type

Relation Name:
prerender
Description:
Used to identify a resource that might be required by the next navigation from the link context, and that the user agent ought to fetch and execute, such that the user agent can deliver a faster response once the resource is requested in the future.
Reference:
W3C Resource Hints Specification (https://www.w3.org/TR/resource-hints/)

Acknowledgments

This document reuses text from the [[!HTML]] specification, edited by Ian Hickson, as permitted by the license of that specification.