Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a CDN with RequireJS optimiser

Tags:

requirejs

RequireJS allows one to load libraries from a CDN. In case the CDN is down, one can also have a backup option where the file can be located some where else (in this case, we assume it is located locally). This is all done in the paths object. For example, in order to load JQuery from a CDN and then locally if the CDN is down, do this:

    paths : {
    jquery : [ 
        'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
        'lib/jquery',
    ]}

The above example would first try find query from the CDN, then from lib/jquery.

Question I am under the impression that when using the requirejs optimiser, things get minified and obfuscated into one large file. How would the a backup option work in the optimiser? Would it be included in the minified file? If so, then there is absolutely no advantage to using a CDN. But if the backup option is not included in the optimised version, then how will optimised code cope if the CDN goes down?

Thanks in advance for the answers.

like image 695
Barry Steyn Avatar asked Mar 27 '13 15:03

Barry Steyn


1 Answers

You can have different configuration settings for your regular site vs. what you use to feed the optimizer, so in this case you would use an "empty" config in your optimizer version so that it doesn't include jQuery in the combined/minified version:

paths: {
    jquery: "empty:"
}

See the documentation for further details: http://requirejs.org/docs/optimization.html#empty

like image 169
explunit Avatar answered Nov 19 '22 17:11

explunit