MiroHibler / meteor-preloader

A Meteor "lazy-loader" for external .js and .css libraries
75 stars 3 forks source link

Not all the js files are loaded #2

Closed suriyabaskaran closed 10 years ago

suriyabaskaran commented 10 years ago

[Preloader - StyleSheet] Loading initiated... route_controller.js:64 [Preloader - StyleSheet] Loading 1/5: /css/style.css route_controller.js:69 [Preloader - StyleSheet] Loading 2/5: http://fonts.googleapis.com/css?family=Open+Sans:400,700,300,600 route_controller.js:69 [Preloader - StyleSheet] Loading 3/5: http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css route_controller.js:69 [Preloader - StyleSheet] Loading 4/5: /vendors/jquery.loadingbar/loadingbar.css route_controller.js:69 [Preloader - StyleSheet] Loading 5/5: /vendors/jquery.growl/stylesheets/jquery.growl.css route_controller.js:69 [Preloader - StyleSheet] Loading finished. route_controller.js:74 [Preloader - JavaScript] Loading initiated... route_controller.js:81 [Preloader - JavaScript] Loading 1/12: http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js route_controller.js:108

MiroHibler commented 10 years ago

Sorry, I don't see from that log what doesn't get loaded...

suriyabaskaran commented 10 years ago

The first js file was loaded, but it wasnt load the rest of the 11 js files. It stopped after Loading 1/12: .... I solved this by including the preloadHandlers hook which returns true always. But the document says it is an optional ?

MiroHibler commented 10 years ago

It is optional, meaning that if it's not defined at all, the loading loop will load all libraries without checking.

If you define just one check, ie.:

preloadHandler  : function ( filePath ) {
    var file = filePath.replace( /\?.*$/,"" ).replace( /.*\//,"" );

    if ( file == 'modernizr.js' ) {
        try {
            return !!Modernizr;
        } catch ( error ) {
            return false;
        }
    }
}

Other checks will fail because they will return undefined.

You should take into account all other checks, as in the example:

preloadHandler  : function ( filePath ) {
    var file = filePath.replace( /\?.*$/,"" ).replace( /.*\//,"" );

    switch ( file ) {
        case 'modernizr.js':
                try {
                    return !!Modernizr;
                } catch ( error ) {
                    return false;
                }
            break;
        default:
            return true;     // <= Let others to load as well
    }
}

So, you should check all or none.

suriyabaskaran commented 10 years ago

Sorry if i am wrong. Here is my router.js

Router.configure({
    layoutTemplate: 'layout',
    preloadFiles: {
        // Use these on *ALL* pages
        'common': {
            js: ['http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js',
                'http://cdnjs.cloudflare.com/ajax/libs/jQuery-slimScroll/1.3.1/jquery.slimscroll.min.js',
                'http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.1/js/bootstrap.min.js',
                'http://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.3/js/foundation.min.js',
                'http://cdnjs.cloudflare.com/ajax/libs/velocity/0.0.22/jquery.velocity.min.js',
                'http://cdnjs.cloudflare.com/ajax/libs/velocity/0.0.22/velocity.ui.js'
            ],
            css: [
                'http://fonts.googleapis.com/css?family=Open+Sans:400,700,300,600',
                'http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css'
            ]
        }
    },
    yieldTemplates: {
        'header': {
            to: 'header'
        },
        'leftSidebar': {
            to: 'leftSidebar'
        },
        'footer': {
            to: 'footer'
        }
    }
});

Router.map(function () {
    this.route('index', {
        path: "/",
        controller: PreloadController,
        waitOn: function () {
            return this.preload({});
        }
    });
});

This code didnt work until i add the following to the router config

preloadHandler: function ( filePath ) { console.log(filePath); return true; }