As most of you know, HTML5 introduced a standardized browser mechanism called link prefetching, one that allows preloading the content of select URLs in the background, if the browser determines there is no network activity. It's used by adding the following to the head:
<link rel="prefetch" href="http://www.example.com/">
I'm curious if this mechanism works reliably for XMLHttpRequest as well -- in the sense that if I specify a link prefetch, and then sometime later on that very same page initiate an AJAX request, would the XHR be a HTTP byte-range request, or ask for the entire page, effectively ignoring a partially preloaded page?
In english: would the AJAX request benefit from the preloaded or partially preloaded content as well?
AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.
How AJAX Calls Work. AJAX uses both a browser built-in XMLHttpRequest object to get data from the web server and JavaScript and HTML DOM to display that content to the user. Despite the name “AJAX” these calls can also transport data as plain text or JSON instead of XML.
An AJAX request is a request made by an AJAX application. Typically, it is an HTTP request made by (browser-resident) Javascript that uses XML to encode the request data and/or response data.
ajax() method allows you to send asynchronous http requests to submit or retrieve data from the server without reloading the whole page. $. ajax() can be used to send http GET, POST, PUT, DELETE etc. request.
Sort answer YES.
As long as you are performing GET requests (or HEAD requests) the XHR will use the downloaded data from the prefetch.
If at the time of the XHR the prefetched file is still downloading, the download will take standard priority and the XHR will return when the download finishes (this is not always the case)
You can observe all these (at least) on the Network tab at Crome Developer Tools
The first zip is from the prefetch and the second from an XHR request and green is wait, blue is download.
You can actually see the XHR waits for the prefetch to end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With