meteormeteorite

Is loading client side code on demand possible in latest version (0.9.x) of meteor?


I am looking at using Meteor to build a real-time data application, since Meteor does so much real-time updating and other related stuff itself. The only thing that is stopping me is that Meteor downloads everything file (except files on the public and server folders) on to client. Let's say, I am developing a very large application containing so many modules. Only certain modules will be available for certain users. So all code related to other modules will download unnecessarily. The initial page load is also going to take a long time. May be there will be some security issues as well.

I tried using IRLibloader plugin to load the JS files by placing them in public folder. So is there a way to configure Meteor in such a way that, it downloads only the code that is required and later download the code for other modules when necessary? If it is not possible, then can you please guide me to other good practices using Meteor.


Solution

  • It is possible. A complete solution is the anti:modules package. First add it to the app:

    meteor add anti:modules
    

    Then create /layers folder inside your project and place your optional files in its subfolder:

    /
      layers
        fancyModule
          someFile.module.js
          anotherFile.module.js
          ...
    

    Then in your code create a global module:

    theApp = Module('$global').as('myApp');
    

    and load it when necessary:

    theApp.require('fancyModule', function () {
      console.log('fancyModule code loaded');
    });