millermedeiros / requirejs-plugins

RequireJS Plugins
MIT License
931 stars 216 forks source link

Add handler for offline mode #20

Open anzorb opened 11 years ago

anzorb commented 11 years ago

Hello, great plugins, although I'm having issues when my Google Feeds HTML5 Application is in offline mode. Here's the error I get: GET http://www.google.com/jsapi?callback=__mm_asynch_req__1360698411216 Network unavailable

As in any other offline news reader, I want to ignore the API if there's no connection. I've tried adding if statements to either goog.js or async.js (if(!navigator.onLine) return), but to no luck - I find that my application cannot initialize because of the following error:

Error: Load timeout for modules: goog!feeds_unnormalized2,goog!feeds

What's the quickest way to check if internet connection is available, and if not, continue application initialization?

Thanks, A.

orientalperil commented 11 years ago

@anzorb Did you find any solution to your problem? I'm having the same issue. I want to be able to run my unit tests offline, but this seems not doable if the async plugin is used.

oniric85 commented 11 years ago

I'm also interested to this. I need to have some sort of fallback on a mobile device if network is turned off.

tysonnero commented 11 years ago

I'm using the async plugin and can degrade gracefully when using Google Maps in offline mode. All you have to do is implement the standard RequireJS error handling such as this http://requirejs.org/docs/api.html#errbacks.

ghost commented 11 years ago

tysonnero, can you provide an example of how you are accomplishing this? Thanks!

tysonnero commented 11 years ago

Firstly, I implemented something close to what Joe Zim did here for lazy loading with RequireJS: http://www.joezimjs.com/javascript/lazy-loading-javascript-with-requirejs/

I'm using AngularJs, so the implementation is a little different. The important thing to note is that the lazy-loader (which I've named ModuleLoader) takes advantage of RequireJS's error callbacks. If the script doesn't load, it will fall into the error condition.

Next, rather than referencing the async plugin within the module dependency array, I use the ModuleLoader to attempt to load the script in the function definition. If the script doesn't load, I gracefully handle the error.

var loader = new ModuleLoader();
loader.get('async!http://maps.google.com/maps/api/js?v=3&sensor=false').then(function () {
  initialize();
}, function (err) {
     handleError(err);
});

Lastly, I've recently been looking at ways to detect being offline prior to loading the script. This seems to be the best way to handle things as I've noticed there are further issues with RequireJS such as caching the failed load.

Take a look the the window.navigator.onLine property as well as other options as trying to ping an online service.