Search code examples
node.jscachingexpressswig-template

Node.js express app caching / slow page loads (swig template engine)


I'm in the process of moving my apache site (on bluehost) to node.js (on heroku), and am noticing that it runs quite a bit slower. I'm wondering if it's a caching issue or what I could be doing wrong.

Here is the site on heroku: http://ak-web-prod.herokuapp.com/

Here is the site on bluehost: http://ardentkid.com

If you notice, the page flashes white during load sometimes when navigating the site (which is why I think it might be a caching problem). I've set express config for:

app.enable('view cache');

doesn't seem to change anything. Anyone have any ideas?

Here is my app configuration

app.configure(function(){
    app.set('config', config);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'html');
    app.set('db', db);
    app.set('port', process.env.PORT || 3000);
    app.engine('.html', cons.swig);
    app.use(express.logger('dev'))
    app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
    app.use(express.cookieParser())
    app.use(express.bodyParser()) //enables req.body
    app.use(express.methodOverride()) //enables app.put and app.delete (can also just use app.post)
    app.use(express.session({
        secret: 'topsecret'
        , store: new RedisStore({
              client:db
            , secret:config.db.auth
            })
    }));
    app.use(passport.initialize()) // use passport session
    app.use(passport.session())
    app.use(app.router) // routes should be at the last
});

app.configure('development', function(){
    console.log('app in development mode');
    app.use('/assets', express.static(__dirname + '/public'));
    app.use('/', express.static(__dirname + '/'));
    swig.init({root: __dirname + '/views', allowErrors: true, cache: false});
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('stage', function(){
    console.log('app in development mode');
    app.use('/assets', express.static(__dirname + '/public', { maxAge: 86400000 }));
    app.use('/', express.static(__dirname + '/', { maxAge: 86400000 }));
    swig.init({root: __dirname + '/views', allowErrors: true, cache:true});
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
    app.enable('view cache');
    app.use('/assets', express.static(__dirname + '/public', { maxAge: 86400000 }));
    app.use('/', express.static(__dirname + '/', { maxAge: 86400000 }));
    swig.init({root: __dirname + '/views', cache:true});
    app.use(express.errorHandler());
});

Solution

  • Issue was due to my redis database (on RedisToGo) not connecting properly. I didn't think this would affect the page loads, but it definitely did. Now that it's fixed, the app is speedier than ever!