3

I'm using require.js and I load a library that handles tracking. However, I have some users that block it from loading.

Since its not a critical part of my app, I would like everything to still work, even when my tracking library fails to load.

I've looked at the documentation for handling errors via errbacks, config fallbacks, and the global onError function.

I was thinking of something like:

requirejs.onError = function (err) {
     var modules = err.requireModules;

     for (var i = 0; i < modules.length; i++) {
          if (modules[i] == 'tracking-lib') {
               // Would be great if I could do something like define('modules[i]', [], null)
          }
     }
};

Similar questions (that don't solve my problem):

requireJS optional dependency

Null dependencies in RequireJS when ajax returns a 404

Community
  • 1
  • 1
gxc
  • 4,946
  • 6
  • 37
  • 55
  • another question that probably doesn't solve your problem but seems related: http://stackoverflow.com/questions/17599082/poll-for-resource-available-with-requirejs – explunit Sep 25 '13 at 14:49
  • I've added a solution for your exact problem in another similar question. Check http://stackoverflow.com/a/27422370/80779 I don't know what is Stackoverflow's policy on duplicated answers, so I'm not copying to whole answer content to this question. – LordOfThePigs Dec 11 '14 at 12:13
  • *"hat don't solve my problem"* isn't a proper problem description. Why does it not solve your similar issue, what is the trouble you're facing with it..? – T J Dec 15 '14 at 10:15

1 Answers1

3

I have created a little Require plugin (code in GitHub) that can lazy-load AMD modules, e.g. like this:

define(["lazy!myModule"], function(myModule) {
    myModule.get().then( // get() returns a promise
        function(m) {
            // handle success, module is in m argument
        },
        function(e) {
            // handle error
        }
    );
});

You could use it as is. Alternatively, you could create a similar plugin, e.g. optional, without depending on require-lazy. The optional plugin could be used as:

define(["optional!myModule"], function(myModule) {
    // code as above
    // or there may be a way to make optional! return null, if loading failed

The code from my plugin might be of help and of course the docs for the plugin API.

Still a simpler - but IMHO dirtier - way could be to require the optional module inside the client module, using the global require function:

define([], function() { // USE THE GLOBAL require!!!
    require(["myOptionalModule"],
        function(myOptionalModule) {
            // loaded successfully
        },
        function(error) {
            // load failed
        }
    );
});

(Also take a look here - related to the last code)

In conclusion, I don't think there is a way to load a module optionally with the plain API. You will have to implement it yourself somehow and handle the asynchronicity with callbacks, as above, either inside the application code or in the plugin.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • Did you test this? In the docs you linked they say: _"Note: errbacks only work with callback-style require calls, not define() calls. define() is only for declaring modules."_ – fiatjaf Dec 04 '13 at 18:31
  • `try..catch` wrapping a `require('module')` inside `define` also don't work. – fiatjaf Dec 04 '13 at 18:32
  • You are right the 3rd example needed brackets around the module name and of course a comma between the arguments! Corrected. Other than that, codes (1) and (3) are working. (Code (2) is dummy.) `try..catch` is guaranteed not to work as expected, both in the synchronous - `require("moduleName")` - and the asynchronous case. – Nikos Paraskevopoulos Dec 04 '13 at 22:22
  • Oh, that is strange. Thanks. I'll test it. – fiatjaf Dec 05 '13 at 03:48
  • Why does `try..catch` doesn't work? Is it because the code is not executed, but parsed? – fiatjaf Dec 05 '13 at 03:49
  • For async calls: The "exception" is thrown later, so the `catch` is never entered. For sync calls: If the module is already loaded, the catch is never entered; if the module is not loaded the catch is always entered because sync `require` calls do not try to load the module. – Nikos Paraskevopoulos Dec 05 '13 at 08:37