Solving Parallelize Downloads Across Hostnames

Article By Digamber Rawat on
Web browsers have a limit on the total number of connections which are concurrent that is required to make a host. A backlog of several waiting resources is formed when you download too many resources together. The browser will hence try to create several connections simultaneously and Parallelize downloads across Hostnames so that it can download the resources.

The total time that is required for a web connection to make sure that the blocking and reducing time is reduced can consequently lead to faster page loading.

You can easily check out in the diagram below how much time resources take before they start downloading.

Why it happens?

This happens because of a free connection. Resources wait for a free connection to start the downloading process.

Parallelize Downloads Across Hostnames

Here is a brilliant way to implement it on WordPress.

To work accordingly, subdomains or hostnames’s structure has to be same. Ex:

example.com/wp-content/uploads/2018/12/heroimage.jpg
static1.example.com/wp-content/uploads/2018/12/heroimage.jpg
static2.example.com/wp-content/uploads/2018/12/heroimage.jpg

Include in functions.php

function parallelizeHostnames($url, $id) {
$hostname = par_get_hostname($url);
$url =  str_replace(parse_url(get_bloginfo('url'), PHP_URL_HOST), $hostname, $url);
return $url;
}
// Include sub-domain in the below code.
function par_get_hostname($name) {
$subdomains = array('static1.mydomain.com','static2.mydomain.com');
$host = abs(crc32(basename($name)) % count($subdomains));
$hostname = $subdomains[$host];
return $hostname;
}
add_filter('wp_get_attachment_url', 'parallelizeHostnames', 10, 2);
Digamber Rawat

Feel free to contact me, If you are looking for a freelance full stack Developer, with the following skills: Mongo DB, Express JS, Angualr 2+, Node JS, Loopback JS, Firebase, Ionic Framework and WordPress. I also offer remote contracting service to the clients across the globe. Hire Me