Digitally Tailored

Dynamically Preloading Links to Boost Website Speed

#5349

This small script monitors mouse movement and preloads content that a user is likely to click on.

Results from http://instantclick.io/click-test

This means that between hover and click there were between 260-400 unused milliseconds which could have been used to fetch the HTML content.

This simple script aims to utilize that time buy fetching the HTML content and preventing the click action with an event that replaces the current page content.

				
					        //var is used for older device support but can be replaced with let if this is not a concern
        
        var pagesStore  = [];
        var pagesQueued = [];

        document.addEventListener("mouseover", function (event) {
            if (typeof event.target.href != "undefined") {
                console.log(event.target.href);
                fetchURL(event.target.href)
            } else {
                if (typeof event.target.parentElement != "undefined" || event.target.parentElement != null) {
                    if (typeof event.target.parentElement.href != "undefined") {
                        console.log(event.target.parentElement.href);
                        fetchURL(event.target.parentElement.href)
                    }
                }
            }
        }, false);

        document.addEventListener("click", function (event) {
            if (typeof event.target.href != "undefined") {
                console.log(event.target.href);
                loadFetchedPage(event.target.href)
            } else {
                if (typeof event.target.parentElement != "undefined" || event.target.parentElement != null) {
                    if (typeof event.target.parentElement.href != "undefined") {
                        console.log(event.target.parentElement.href);
                        loadFetchedPage(event.target.parentElement.href)
                    }
                }
            }

        }, false);

        function fetchURL(urlToFetch) {
            if (typeof pagesQueued[urlToFetch] == "undefined") {
                pagesQueued[urlToFetch] = true;
                console.log("Fetching " + urlToFetch);
                console.log(this);
                var xmlhttp;
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                } else { // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                        pagesStore[urlToFetch] = xmlhttp.responseText;
                    }
                };
                xmlhttp.open("GET", urlToFetch, true);
                xmlhttp.send();
            }
        }

        function loadFetchedPage(urlToLoad) {
            if (typeof pagesStore[urlToLoad] == "undefined") {
                //console.log('Loading ' + urlToLoad);
                //window.location = urlToLoad;
            } else {
                event.preventDefault();
                history.pushState('', urlToLoad, urlToLoad);
                console.log('Using cached ' + urlToLoad);
                document.open();
                document.write(pagesStore[urlToLoad]);
                document.close();
            }
        }
				
			
Files

No files attached.

Work With Digitally Tailored

If you would like to implement, discuss or add to this project, please contact us.

License

Copyright 2019 - 2020 Digitally Tailored LTD

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.