2

我正在将我的 apache 站点(在 bluehost 上)移动到 node.js(在 heroku 上),并且注意到它的运行速度要慢一些。我想知道这是缓存问题还是我做错了什么。

这是heroku上的网站:http: //ak-web-prod.herokuapp.com/

这是 bluehost 上的网站:http: //ardentkid.com

如果您注意到,页面在加载过程中有时会在导航站点时闪烁白色(这就是为什么我认为这可能是缓存问题)。我已经为:

app.enable('view cache');

似乎没有任何改变。有人有想法么?

这是我的应用程序配置

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());
});
4

3 回答 3

1

很难说,但我认为可能有两件事会影响加载时间

  1. If you are using the free version of Heroku, it could be causing the slowness.
  2. The Headers being sent aren't allowing the browser to cache, requiring it to re-download everything. Ex.

curl -XHEAD -v http://ak-web-prod.herokuapp.com/assets/img/logo.png

< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: public, max-age=0
< Content-length: 11264
< Content-Type: image/png
< Date: Sun, 31 Mar 2013 03:41:33 GMT
< Etag: "11264-1364696076000"
< Last-Modified: Sun, 31 Mar 2013 02:14:36 GMT
< X-Powered-By: Express
< Connection: keep-alive

To have your static assets served up with Cache-Control set to something besides zero, take a look at the static middleware - http://www.senchalabs.org/connect/static.html, it allows you to set the max-age. This will affect images, JS, and CSS.

于 2013-03-31T03:53:54.537 回答
1

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!

于 2013-04-03T01:18:10.263 回答
1

You should add this :

swig.setDefaults({
    cache: false,
});
于 2016-09-13T08:17:36.937 回答