How to failover to a local file if a CDN fails with RequireJS


To use a CDN is recommended for speed and a better performance but for example, what happens if a CDN fails or you want to enable offline mode?

If you are using RequireJS there is a way to do this, let’s see a simple example.

Failover system

Just add another route to the same object

  enforceDefine: true, 
  paths: { 
    jquery: [ 

So if the first route fails, RequireJS will try to load the second one (and so on…).

Using it

There is no change about handling the variables

require(['jquery'], function($) { 
  // Use $ normally... 

Read more about config fallbacks
More information RequireJS API

David Burgos

Read more posts by this author.