Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an AJAX request utilize link prefetching?

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?

like image 533
John Weisz Avatar asked May 06 '15 22:05

John Weisz


People also ask

What kind of information can be received by an AJAX request?

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 do AJAX requests work?

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.

What is HTTP request in AJAX?

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.

Does AJAX request send 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.


1 Answers

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

enter image description here

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

like image 182
MaanooAk Avatar answered Sep 18 '22 14:09

MaanooAk