wp enqueue script using scripts from cdn with a safety callback

The accepted answer is completely wrong. I suspect a gross misunderstanding of the OP’S question, since anyone with even a little experience with programming will never recommend something like “cache your own copy of the script, and perform get_url calls every 20 minutes”.

@Paul G. says there is no point in loading from the CDN On every page load.

That’s the whole point of using CDN’S.
If you don’t load from the CDN and instead load your cached copy, the client has to download it from your server. You might be in Auckland New Zealand and your client might be in London. Your server will never be able to match a CDN located closer to the client.

Furthermore, CDN’S use things like NGINX/Varnish, with massive caches that don’t even touch servers. Your server may or may not be using a reverse proxy like NGINX with load balancing, it may or not have a large cache serving static files, so in order to load your copy of the files, it will have to go through the entire stack, which can be nowhere near the speed you get from loading cached static files.

Whatever you do, use CDN’S as far as possible, and yes on each and every page load.

Finally you’re worried about a CDN going down. If that happens, a simple workaround would be to mention 2 or 3 other CDN’S as fallbacks, and if the primary CDN fails to load, you could easily hook up a listener and load from the others.

In the event that all the major CDN’s are down, yours will not be the only site in the world that fails to work, you can add things like Facebook, Imgur, Flckr and a billion other websites that will fail with you. At that point in time, it might be better to go outside and look at the sky , and maybe you will find the UFO’s that have caused the first worldwide datacenter failure in the history of mankind.

Short Answer:
Use the Cdn.

Example requirejs script for fallback url’s.

   requirejs.config({
   enforceDefine: true,
   paths: {
       jquery: [
        '//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min',
        //If the CDN location fails, load from this location
        //xyz.com/jquery.min.js
      ]
   }
   });

Leave a Comment