Node.js表示应用程序caching/缓慢页面加载(swig模板引擎)

我正在将我的apache站点(在bluehost上)转移到node.js(在heroku上),并且注意到它运行速度比较慢。 我想知道这是一个caching问题,或者我可能做错了什么。

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

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

如果你注意到,页面在浏览网站时会闪烁白色(这就是为什么我认为这可能是一个caching问题)。 我为以下设置了快速configuration:

app.enable('view cache'); 

似乎没有改变任何东西。 有人有主意吗?

这是我的应用程序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()); }); 

问题是由于我的Redis数据库(在RedisToGo上)没有正确连接。 我不认为这会影响页面加载,但它确实做到了。 现在它已经修复了,应用程序比以前更快了!

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

  1. 如果您使用的是Heroku的免费版本,可能会导致速度缓慢。
  2. 正在发送的标题不允许浏览器caching,要求重新下载所有内容。 防爆。

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 

要让你的静态资产在Cache-Control中被设置为零以外的东西,看看静态中间件 – http://www.senchalabs.org/connect/static.html ,它允许你设置最大年龄。 这将影响图像,JS和CSS。

你应该添加这个:

 swig.setDefaults({ cache: false, });