chrisblakley / Nebula

Nebula is a WordPress theme framework that focuses on enhancing development. The core features of Nebula make it a powerful tool for designing, developing, and analyzing WordPress websites consistently, yet its deliberately uncomplicated code syntax also serves as a learning resource for programmers themselves.
https://nebula.gearside.com
GNU General Public License v2.0
142 stars 36 forks source link

More prefetch notes and improvements #1799

Closed chrisblakley closed 5 years ago

chrisblakley commented 5 years ago

Prefetch can be used more often and can also be triggered via JavaScript so do it a little more often (but not too aggressive).

Maybe make a function that can be called to prefetch a resource (and only does it for 4G effective connection type) and adds the <link rel="prefetch" href="https://whatever.com" /> to the DOM.

On the PHP side, come up with a way to predictively fetch common next-steps. https://github.com/chrisblakley/Nebula/issues/1798 will help, but I want some core Nebula improvements too so we aren't 100% relying on third-party scripts.

chrisblakley commented 5 years ago

Also look into priority hints too.

chrisblakley commented 5 years ago

I've noted some of these before, but others are new:

https://developers.google.com/analytics/devguides/reporting/core/v4/ https://predictjs.firebaseapp.com/ https://github.com/guess-js/guess

chrisblakley commented 5 years ago

Here's what I came up with to actually do the prefetch in JS:

//Prefetch a resource
function nebulaPrefetch(url, callback){
    if ( navigator.connection.effectiveType.toString().indexOf('2g') >= 0 || navigator.connection.saveData ){ //If network connection is 2G or Save Data is enabled
        return false;
    }

    if ( url ){
        window.requestIdleCallback(function(){ //Wait until the browser is idle before prefetching
            jQuery('<link rel="prefetch" href="' + url + '">').on('load', callback).appendTo('head');
        });
    }
}

It can be called like this:

    nebulaPrefetch('get-started/recommendations/', function(){
        //...do something after prefetch loads (optional)
    });
chrisblakley commented 5 years ago

I modified the Service Worker addToCache() function too. Now if using a SW it'll fetch like normal, but otherwise it will attempt to prefetch the resource rather than doing nothing.

chrisblakley commented 5 years ago

Elements to consider prefetching on window load:

I think I already prefetch these (via predictive cache) but double check them:

May decide to defer some of these to child themes main.js... Not sure yet.

chrisblakley commented 5 years ago

I'm going to close this now as I've got everything added that I want. Will re-open or make new tickets for future resource hints.

chrisblakley commented 5 years ago

Found this after I implemented the above, but still a great read: https://calendar.perfplanet.com/2018/all-about-prefetching/

I like the list/names of each prefetch strategy:

We use a combination of all of these in the Nebula predictive (pre)fetching.