Would it benefit to pre-compile jade templates on production in express

南笙酒味 提交于 2019-12-30 08:41:09

问题


When using jade-lang on production, would I benefit from having some form of a middleware that pre-compiles all the .jade views and then uses them in res.render? Or does that automatically happen when you do NODE_ENV=production?

I'm simply exploring options on how to speed-up jade rendering on production.


回答1:


When Jade compiles the template, the template is cached. In production environment if you warm up the cache, then there is no need to pre-compile template. Even if you don't, the template will be cached after its first compilation.

I recommend you to have a look Jade's source code to better understand how it works.

exports.render = function(str, options, fn){
  // ...
  var path = options.filename;
  var tmpl = options.cache
    ? exports.cache[path] || (exports.cache[path] = exports.compile(str, options))
    : exports.compile(str, options);
  return tmpl(options);
};

Source: https://github.com/visionmedia/jade/blob/1.3.0/lib/jade.js#L255-L259

exports.renderFile = function(path, options, fn){
  // ...
  options.filename = path;
  var str = options.cache
    ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
    : fs.readFileSync(path, 'utf8');
  return exports.render(str, options);
};

Source: https://github.com/visionmedia/jade/blob/1.3.0/lib/jade.js#L291-L295



来源:https://stackoverflow.com/questions/22220839/would-it-benefit-to-pre-compile-jade-templates-on-production-in-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!