RequireJS: Local fallback for if CDN fails

前端 未结 2 1044
执念已碎
执念已碎 2020-12-22 20:26

In my backbone app, I need to provide a fallback for each required file, in the case that the CDN that delivers them fails.

I have tried overwriting require.on

2条回答
  •  青春惊慌失措
    2020-12-22 21:19

    I have found a solution to the problem provided in RequireJS 2.x.x. There was a demand for this solution, so in turn, RequireJS added a paths object to their config. This provides fallback functionality for CDNs, should they fail.

    It should also be noted that the order! plugin has been deprecated in Require 2.0, so I also needed to make use of the shim object to define dependencies. It's actually a pretty interesting idea.

    Here is my new require.config:

    require.config({
        urlArgs: "ts="+new Date().getTime(), // disable caching - remove in production
        paths: {
            jquery: [
                "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min",
                "libs/jquery"
            ],
            jqueryui: [
                "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min",
                "libs/jqueryui"
            ],
            underscore: [
                "http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min",
                "libs/underscore"
            ],
            backbone: [
                "http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min",
                "libs/backbone"
            ]
        },
        shim: {
            'jqueryui': ['jquery'],
            'underscore': ['jquery'],
            'backbone': ['underscore'],
            'core/core': ['underscore'],
            'core/errors': ['core/core'],
            'core/constants': ['core/core']
        }
    });
    

提交回复
热议问题