Exploring the usage of prefetch headersLion Ralfs — Posted on
On the web, prefetching describes the practice of downloading something before the user actually needs it. This is done to speed up the time it takes to display something when the user really needs it. This can be anything from images, scripts or stylesheets to HTML-documents itself. Especially for documents, it might be wise to somehow indicate that a certain request is not a "real" user request, but instead a prefetch request. This can in turn instruct advertising or analytics software to filter out those requests, which are otherwise indifferentiable. This post explores approaches of how requests are marked as prefetch requests.
Prefetch links look like this:
<link rel="prefetch" href="/about.html" /> and are a standardized way to tell browsers about a resource that might be required soon. The browser uses its own heuristics (preferably when it's idling) of when to send the request. The response is then stored in a cache for later usage. Unfortunately, browsers use different methods of tagging prefetch requests.
To the best of my knowledge, Firefox was the first browser to document their use of a specific header that is sent along with a prefetch request. They chose to use
X-Moz: prefetch, at least since 2002. It has not changed since then.
In 2010, Safari added a
X-Purpose: prefetch header to all
<link rel="prefetch"> requests. They considered using Mozilla's already established
X-Moz: prefetch header, but decided against it. In the discussion, they also mention that the HTML5 working group should probably standardize and register the header.
However, only 3 months later, Safari dropped the
X- prefix, based on an IETF draft outlining the deprecation of
Side note: Who knew that
X- stands for eXperimental/eXtension? Not me. It makes sense to deprecate a syntax for experimental headers when servers either use them anyways or have to keep supporting them for backwards compatibility.
<link rel="prefetch"> is disabled by default in Safari, sitting behind a feature flag. When enabled, the
Purpose: prefetch header is used.
Google Chrome currently uses
Purpose: prefetch. This has been the case since 2018 as part of NoStatePrefetch, which also powers
<link rel="prerender">. Before that, just like Safari, Chrome used the
X-Purpose: prefetch header since 2010.
Because of CORS issues, when the "purpose" header is added, the Chrome Team is looking to revisit the name of this header, considering both "Sec-Purpose" and "Sec-Fetch-Purpose".
Since Edge is just a fancy Chromium these days, it also uses
To summarize, this is what browsers currently use:
|Browser||Prefetch Header||Prerender Header|
|Firefox||prerender not supported|
|Safari||prerender not supported|
I've added prerendering headers (originating from
<link rel="prerender">) to the table even though this post is about prefetching, to highlight that they generally use the same headers.
Chrome offers an "instant search" feature, where it preemptively fetches websites as a user types into the address bar. This, in combination with autocomplete and suggestions, offers a great user experience, as the website is already downloaded. For this feature, apparently a
X-Purpose: preview header is used. Safari's "Top Sites" also made use of this header.
Facebook's in-app browser/renderer is also capable of downloading HTML as a link appears in a user's viewport. They add an
X-Purpose: preview header, claiming it is "standard industry practice".
From what I've gathered, browsers paid attention to server-side tracking and gave developers a way to distinguish requests based on certain headers. Unfortunately, they are not standardized and therefore browsers have different opinions on which header to use.
A problem that you might encounter is the following: if you track web traffic, but filter out requests based on the prefetch headers, your "real" requests also disappear. Because when a resource is prefetched, and a user eventually uses that resource, no extra request is fired, so the server has no knowledge over the actual usage of prefetched resources. Ignoring the fact that tracking purely based on access logs screams "error-prone", Mozilla outlines an approach to avoid this issue:
- For a prefetch request, respond with a
- When a browser uses the prefetched resource, it is allowed to load it from cache, but it must send a validation request to the server. These are conditional requests, where the browser sends a
If-None-Matchheader (along with an ETag), to which the server can respond differently (200 vs. 304 status code for example).
- If the validation request results in a 304, use the prefetched version. If not, use the response from the validation request.
This adds a bit of extra delay, as validation requests have to use the network. Also it is not always feasible to add must-revalidate to HTTP-Requests. However, they show up in our logs and allow us to continue using server-side traffic analysis.
From my own testing, it looks like Safari and Firefox follow this approach. Chrome/Edge seem to not send a validation request and serve straight from the (prefetch) cache.
Just a few things to consider when employing prefetching in conjunction with server-side traffic analysis.
Also, if you're interested in adding prefetching to your site, quicklink is a fantastic little library, for automatic prefetching of links in a users viewport.
Update Nov. 28, 2021: Added prerender headers.
-  https://html.spec.whatwg.org/#link-type-prefetch
-  https://w3c.github.io/resource-hints/#prefetch
-  https://bugzilla.mozilla.org/show_bug.cgi?id=179983#c7
-  https://bugs.webkit.org/show_bug.cgi?id=46529
-  https://trac.webkit.org/changeset/69420/webkit
-  https://bugs.webkit.org/show_bug.cgi?id=3652#c25
-  https://bugs.webkit.org/show_bug.cgi?id=3652#c28
-  Rename X-Purpose to Purpose
-  P. Saint-Andre, "X-" Considered Harmful, 2010
-  https://bugs.chromium.org/p/chromium/issues/detail?id=86175#c65
-  Katie Hempenius, Introducing NoState Prefetch, 2018
-  https://chromium.googlesource.com/chromium/src/+/e3bf203cc09ac0161c339afc642c320c24abb0e6
-  https://www.chromestatus.com/feature/6247959677108224
-  https://github.com/w3c/resource-hints/issues/74
-  caniuse: link rel=prerender
-  Colin Mackay, Google Instant Browsing, 2011
-  Martin Sutherland, How to detect a page request from Safari 4's Top Sites feature, 2010
-  Facebook for Business - Help Center, Understanding prefetching and how Facebook uses prefetching
-  MDN: Do prefetched requests contain a Referer: header?
-  https://github.com/GoogleChromeLabs/quicklink