I'm creating an app that requests a lot of content from the server. I installed timers from click to render throughout the app at each stage, AJAX request, DOM Injection, and Rendering. I found that the requests are completely unpredictable, and on average I've recorded they can take up 43% of the click-to-render pipeline.
I wanted to minimize these requests to optimize the UX, and my solution was to create a hash table using a JS variable. I've also tried using localStorage in the same manner.
MyRequest.prototype.sync = function(r) {
if (this.storage[r.path]) {
return this.storage[r.path];
} else {
this.req = new XMLHttpRequest();
this.req.open('GET', r.path, false);
this.req.send(null);
if (this.req.status === 200) {
this.storage[r.path] = this.req.responseText;
return this.req.responseText;
} else {
return null;
}
}
};
Other solutions I've thought of is keeping content in the DOM tree as DOM elements shoved off the viewport or display:none to not affect the render tree. And grabbing the content from the DOM when needed.
** What is the best practice for prefetching content, preferably on the initial HTTP request of the html page, that will eventually be requested? Is this method a decent solution?